zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    MyEclipse 快捷键
    Oracle使用split和splitstr函数批量分隔字符串
    linux解压 tar命令
    Crontab的用法
    .net 连接ORACLE中文显示乱码解决方案
    python 列表
    关于ios 11 的问题
    初步了解会计学
    iOS关于直播的链接
    知识链接中.....
  • 原文地址:https://www.cnblogs.com/zkwarrior/p/4963434.html
Copyright © 2011-2022 走看看