程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# OpenCV學習筆記二之圖像讀寫的兩種方法

C# OpenCV學習筆記二之圖像讀寫的兩種方法

編輯:C#入門知識

圖像顯示在控件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);

 

private Image IntPtr2Image(IntPtr src)
        {
            MIplImage img = (MIplImage)Marshal.PtrToStructure(src, typeof(MIplImage));
            Bitmap disp = new Bitmap(img.width, img.height, PixelFormat.Format24bppRgb);
            BitmapData bmp = disp.LockBits(new Rectangle(0, 0, img.width, img.height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
            long linebytes = (img.width * 24 + 31) / 32 * 4;

            unsafe
            {
                byte* pixel = (byte*)bmp.Scan0.ToPointer();
                if (img.nChannels == 3)
                {
                    for (int i = 0; i < img.height; i++)
                    {
                        for (int j = 0, n = 0; j < img.width; j++, n++)
                        {
                            byte b = ((byte*)img.imageData + img.widthStep * i)[3 * j];
                            byte g = ((byte*)img.imageData + img.widthStep * i)[3 * j + 1];
                            byte r = ((byte*)img.imageData + img.widthStep * i)[3 * j + 2];
                            *(pixel + linebytes * (i) + n) = b;
                            n++;
                            *(pixel + linebytes * (i) + n) = g;
                            n++;
                            *(pixel + linebytes * (i) + n) = r;
                        }
                    }
                }
                else if (img.nChannels == 1)
                {
                    for (int i = 0; i < img.height; i++)
                    {
                        for (int j = 0, n = 0; j < img.width; j++, n++)
                        {
                            byte g = ((byte*)img.imageData + img.widthStep * i)[j];
                            *(pixel + linebytes * (i) + n) = g;
                            n++;
                            *(pixel + linebytes * (i) + n) = g;
                            n++;
                            *(pixel + linebytes * (i) + n) = g;
                        }
                    }
                }
                else
                {
                    return null;
                }
            }
            disp.UnlockBits(bmp);
            return (Image)disp;
        }
 
 

方法二

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

loadPictureBox.Image = img.ToBitmap();

 作者“邊城駱駝 -- 每天進步百分之一”
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved