日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#和c++的opencv位图数据参数互换问题解决方法

發布時間:2023/12/18 C# 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#和c++的opencv位图数据参数互换问题解决方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.******************************

C#調用vc++ dll 傳遞參數的問題(Bitmap 轉換為 opencv mat ),保存后圖片不一樣。

vc++ 代碼

bool Recognize(Point_2F *arr,uchar* b) {Mat src=cv::Mat(415,770,CV_8UC3,b);/*for (int i=0;i<src.rows;i++) { memcpy(src.ptr(i),b+i*src.cols,src.cols); }*/ cv::imwrite("D:\\my_2_testfiles\\111_before.png",src);// 保存的照片和原來的照片不一樣而且大小也不一樣?}

C#代碼

[DllImport("rectanglepoints.dll", EntryPoint = "Recognize", CallingConvention = CallingConvention.Cdecl)]static extern byte Recognize(IntPtr arr, IntPtr b);

{ // Create a new bitmap.Bitmap bmp = new Bitmap("D:\\my_2_testfiles\\111.png");// Lock the bitmap's bits. Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);System.Drawing.Imaging.BitmapData bmpData =bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,bmp.PixelFormat);// Get the address of the first line.IntPtr ptrImg = bmpData.Scan0; bool ret;ret = Convert.ToBoolean(Recognize(IntPtr.Zero, ptrImg)); // Unlock the bits.bmp.UnlockBits(bmpData);Console.Read();}

照片附加

原圖:

?

調用vc++ dll后保存的圖片

2.******************************************************************

C#Bitmap和C++ opencv Mat相互轉換 ?C#調用C++編譯成的dll,這個dll中包含Opencv個的Mat到c#的Bitmap轉換,具體代碼如下: ?C++部分: ?首先創建win32應用程序,選擇類庫模板 ? ?DLL_APIuchar * _stdcall run1(char* filename, int&width, int&height, int&step) { ? IplImage* uu = cvLoadImage(filename); ? IplImage * dst1 = cvCreateImage(cvSize(uu->width, uu->height), 8, 1); cvCvtColor(uu, dst1, CV_RGB2GRAY); Matss = cvarrToMat(uu); ? uchar * data = new uchar[uu->width*uu->height * 3]; data = ss.data; ? width = ss.size().width; height = ss.size().height; step = ss.step; ?return data; } ?C#中調用這個dll ?[DllImport(@"E:\Files\BarcodeProject\Code\testCode\OpenCvAssemblies\Debug\OpenCvAssemblies.dll" )] ?publicstaticexternIntPtr run1(string a, outint width, outint height, outint step); ?至此完成C++ Mat到Bitmap的轉換 ?下面是Bitmap 到 Mat的轉換 C# 部分 ?publicstaticBitmapInfoGetImagePixel(Bitmap Source) { byte[] result; int step; ?intiWidth = Source.Width; intiHeight = Source.Height; ?Rectanglerect = newRectangle(0, 0, iWidth, iHeight); ?System.Drawing.Imaging.BitmapDatabmpData = Source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, Source.PixelFormat); IntPtriPtr = bmpData.Scan0; ?intiBytes = iWidth * iHeight * 3; //根據通道數進行設置 byte[] PixelValues = newbyte[iBytes]; ? ?//int time = Environment.TickCount; ?System.Runtime.InteropServices.Marshal .Copy(iPtr, PixelValues, 0, iBytes); //time = Environment.TickCount - time; //Console.WriteLine(time.ToString() + "ms"); Source.UnlockBits(bmpData); step = bmpData.Stride; result = PixelValues; // return result; // step = 0; ?BitmapInfo bi = newBitmapInfo{ Result=result, Step=step }; return bi; } } ?///<summary> /// Bitmap信息類 ///</summary> ?publicclassBitmapInfo { ?publicbyte[] Result { get; ?set; } publicint Step { get; set; } ?} ?Step 是掃描的步長,這個很重要,如果這個步長不是原來的值,就會造成圖像偏移,從而造成失真。 ? ?C++部分 ?DLL_APIvoid_stdcallshow(uchar* data,intwidth,intheight,intstep) { Matimage(height,width, CV_8UC3,data,step); //image.data = data; imshow("Image",image); ? ?//Mat(int rows, int cols, int type, void* data, size_t step = AUTO_STEP); } ?C# 中調用方式 ?BitmapInfo bi = GetImagePixel(bitmap); ?show(bi.Result,bitmap.Width,bitmap.Height,bi.Step); ?至此完成他們的相互轉換

總結

以上是生活随笔為你收集整理的c#和c++的opencv位图数据参数互换问题解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。