C# Emgu CV学习笔记二之图像读写的两种方法

http://blog.csdn.net/marvinhong/article/details/6800450

图像显示在控件loadPictureBox上

方法一

//读取图像001.jpg

IntPtr img = CvInvoke.cvLoadImage("001.jpg", Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR);

//IntPtr转换为Image,详细见IntPtr2Image方法

loadPictureBox.Image = IntPtr2Image(img);

//显示图像窗口

CvInvoke.cvShowImage("view", img);

//窗口保留2000毫秒,即2秒
CvInvoke.cvWaitKey(2000);

//关闭窗口
CvInvoke.cvDestroyWindow("view");

//保存图像
CvInvoke.cvSaveImage("002.jpg", img);

//释放
CvInvoke.cvReleaseImage(ref img);

[csharp] view plaincopy
 
  1. private Image IntPtr2Image(IntPtr src)  
  2.         {  
  3.             MIplImage img = (MIplImage)Marshal.PtrToStructure(src, typeof(MIplImage));  
  4.             Bitmap disp = new Bitmap(img.width, img.height, PixelFormat.Format24bppRgb);  
  5.             BitmapData bmp = disp.LockBits(new Rectangle(0, 0, img.width, img.height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);  
  6.             long linebytes = (img.width * 24 + 31) / 32 * 4;  
  7.   
  8.             unsafe  
  9.             {  
  10.                 byte* pixel = (byte*)bmp.Scan0.ToPointer();  
  11.                 if (img.nChannels == 3)  
  12.                 {  
  13.                     for (int i = 0; i < img.height; i++)  
  14.                     {  
  15.                         for (int j = 0, n = 0; j < img.width; j++, n++)  
  16.                         {  
  17.                             byte b = ((byte*)img.imageData + img.widthStep * i)[3 * j];  
  18.                             byte g = ((byte*)img.imageData + img.widthStep * i)[3 * j + 1];  
  19.                             byte r = ((byte*)img.imageData + img.widthStep * i)[3 * j + 2];  
  20.                             *(pixel + linebytes * (i) + n) = b;  
  21.                             n++;  
  22.                             *(pixel + linebytes * (i) + n) = g;  
  23.                             n++;  
  24.                             *(pixel + linebytes * (i) + n) = r;  
  25.                         }  
  26.                     }  
  27.                 }  
  28.                 else if (img.nChannels == 1)  
  29.                 {  
  30.                     for (int i = 0; i < img.height; i++)  
  31.                     {  
  32.                         for (int j = 0, n = 0; j < img.width; j++, n++)  
  33.                         {  
  34.                             byte g = ((byte*)img.imageData + img.widthStep * i)[j];  
  35.                             *(pixel + linebytes * (i) + n) = g;  
  36.                             n++;  
  37.                             *(pixel + linebytes * (i) + n) = g;  
  38.                             n++;  
  39.                             *(pixel + linebytes * (i) + n) = g;  
  40.                         }  
  41.                     }  
  42.                 }  
  43.                 else  
  44.                 {  
  45.                     return null;  
  46.                 }  
  47.             }  
  48.             disp.UnlockBits(bmp);  
  49.             return (Image)disp;  
  50.         }  

方法二

Image<Bgr, Byte> img = new Image<Bgr, byte>("001.jpg");

loadPictureBox.Image = img.ToBitmap();