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();

posted @   _海阔天空  阅读(618)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示