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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#调用c++(Opencv)dll的实例

發(fā)布時間:2023/12/18 C# 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#调用c++(Opencv)dll的实例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

*****************************************c++代碼

MYDLL int GetCenterPos(uchar* imageData,int step, int widthValue, int heightValue, int BinaryThreshold, int LineBlankThreshold, int* centerX, int* centerY)
{
?? ?
?? ?//Mat src(widthValue, heightValue, CV_8UC3);
?? ?//cvtColor(src, src, CV_RGB2GRAY);
?? ?//memcpy(imageData, src.data, sizeof(uchar)*src.rows*src.cols * 3);
?? ?Mat src = cv::Mat(heightValue, widthValue, CV_8UC4, imageData,step);//很重要的參數(shù):CV_8UC4
?? ?/*return src.cols;*/
?? ?/*return src.rows;*/
?? ?/*imshow("圖1", src);*/
?? ?/*return 99;*/
?? ?
?? ?int ImageWidth = src.cols;
?? ?int ImageHeight = src.rows;
?? ?/*Mat src ImageWidth= imread(path);*/
?? ?// 結(jié)果圖
?? ?Mat dst;
?? ?//灰度圖轉(zhuǎn)換
?? ?cvtColor(src, dst, COLOR_RGB2GRAY);
?? ?// 高斯模糊,主要用于降噪

?? ?GaussianBlur(dst, dst, Size(3, 3), 0);
?? ?
?? ?// 二值化圖,主要將灰色部分轉(zhuǎn)成白色,使內(nèi)容為黑色
?? ?threshold(dst, dst, BinaryThreshold, 255, THRESH_BINARY_INV);

?? ?// 中值濾波,同樣用于降噪
?? ?medianBlur(dst, dst, 3);
?? ?//blur(dst, dst, Size(3, 3));//平滑處理
?? ?
?? ?//***********獲取中心算法
?? ?/*imshow("圖2", dst);*/
?? ?//********************************************橫向遍歷
?? ?//map<int, int> horizonMap;//提取詞典
?? ?vector<int> vecMyHouse(0);
?? ?vector<int> HorizonMyHouse(0);
?? ?int height = dst.rows;
?? ?int width = dst.cols;
?? ?
?? ?for (int i = 0; i < height; i++) {
?? ??? ?if (i % LineBlankThreshold == 0)
?? ??? ?{

?? ??? ??? ?int num = 0;
?? ??? ??? ?vecMyHouse.clear();
?? ??? ??? ?
?? ??? ??? ?for (int j = 0; j < width; j++) {
?? ??? ??? ??? ?int index = i * width + j;
?? ??? ??? ??? ?//像素值?? ??? ?
?? ??? ??? ??? ?int data = (int)dst.data[index];
?? ??? ??? ??? ?
?? ??? ??? ??? ?if (data == 0)
?? ??? ??? ??? ?{?? ??? ?
?? ??? ??? ??? ??? ?/*mp.X = j;
?? ??? ??? ??? ??? ?mp.Y = i;*/
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?vecMyHouse.push_back(j);
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}

?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?//******
?? ??? ??? ?int tempX = 0;
?? ??? ??? ?int tempY = 0;
?? ??? ??? ?int count = vecMyHouse.size();
?? ??? ??? ?if (count > 50)
?? ??? ??? ?{
?? ??? ??? ??? ?int a = 0;
?? ??? ??? ?}
?? ??? ??? ?if (vecMyHouse.size() <3)continue;
?? ??? ??? ?if (vecMyHouse.size() > 20)continue;?? ??? ?
?? ??? ??? ?//sort(vecMyHouse.begin(), vecMyHouse.end());//大小排序
?? ??? ??? ?//最大值:?? ??? ??? ?
?? ??? ??? ?int max = *max_element(vecMyHouse.begin(), vecMyHouse.end());//過濾
?? ??? ??? ?int min = *min_element(vecMyHouse.begin(), vecMyHouse.end());//過濾
?? ??? ??? ?int tempCount = 0;
?? ??? ??? ?//利用keySet方法將map中鍵放入到Set中,可以使用 ?增強(qiáng)for
?? ??? ??? ?for (int k = 0; k < vecMyHouse.size(); k++) {
?? ??? ??? ??? ?if (vecMyHouse[k] != max&&vecMyHouse[k] != min)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?tempX += vecMyHouse[k];
?? ??? ??? ??? ??? ?tempCount++;
?? ??? ??? ??? ?}

?? ??? ??? ?}
?? ??? ??? ?int value = (int)(tempX*1.0 / tempCount);
?? ??? ??? ?
?? ??? ??? ?HorizonMyHouse.push_back(value);
?? ??? ??? ?num++;
?? ??? ?}
?? ?}
?? ?int tempX = 0;
?? ?//最大值:
?? ?int max = *max_element(HorizonMyHouse.begin(), HorizonMyHouse.end());//過濾
?? ?//最小值:
?? ?int min = max;//*min_element(HorizonMyHouse.begin(), HorizonMyHouse.end());//過濾
?? ?sort(HorizonMyHouse.begin(), HorizonMyHouse.end());//大小排序
?? ?int count = HorizonMyHouse.size();
?? ?for (int z = 0; z < HorizonMyHouse.size(); z++)
?? ?{
?? ??? ?if (min>HorizonMyHouse[z] && HorizonMyHouse[z] != 0)
?? ??? ?{
?? ??? ??? ?min = HorizonMyHouse[z];
?? ??? ?}
?? ??? ?tempX += HorizonMyHouse[z];
?? ?}
?? ?tempX = tempX - (max + min);
?? ?if (HorizonMyHouse.size() > 2)
?? ?{

?? ??? ?*centerX = (tempX *1.0 / (HorizonMyHouse.size() - 2));
?? ?}
?? ?else
?? ?{
?? ??? ?*centerX = (HorizonMyHouse[0] + HorizonMyHouse[1]) / 2;
?? ?}
?? ?*centerX += 1;

?? ?
#if 1
?? ?//********************************************縱向遍歷
?? ?//map<int, int> horizonMap;//提取詞典
?? ?vector<int> vecMyHouse2(0);
?? ?vector<int> verticalMyHouse(0);
?? ?height = dst.rows;
?? ?width = dst.cols;
?? ?for (int i = 0; i < width; i++) {
?? ??? ?if (i % LineBlankThreshold == 0)
?? ??? ?{
?? ??? ??? ?int num = 0;
?? ??? ??? ?vecMyHouse2.clear();

?? ??? ??? ?for (int j = 0; j < height; j++) {
?? ??? ??? ??? ?int index = j * width + i;
?? ??? ??? ??? ?//像素值?? ??? ?
?? ??? ??? ??? ?int data = (int)dst.data[index];
?? ??? ??? ??? ?if (data == 0)
?? ??? ??? ??? ?{
?? ??? ??? ??? ?/*?? ?mp.X = i;
?? ??? ??? ??? ??? ?mp.Y = j;*/

?? ??? ??? ??? ??? ?vecMyHouse2.push_back(j);

?? ??? ??? ??? ?}

?? ??? ??? ?}
?? ??? ??? ?//******
?? ??? ??? ?int tempX = 0;
?? ??? ??? ?int tempY = 0;
?? ??? ??? ?int count = vecMyHouse.size();
?? ??? ??? ?if (vecMyHouse2.size() <3)continue;
?? ??? ??? ?if (vecMyHouse2.size() > 20)continue;
?? ??? ??? ?sort(vecMyHouse2.begin(), vecMyHouse2.end());//大小排序
?? ??? ??? ?//最大值:
?? ??? ??? ?int max = *max_element(vecMyHouse2.begin(), vecMyHouse2.end());//過濾
?? ??? ??? ?int min = *min_element(vecMyHouse2.begin(), vecMyHouse2.end());//過濾
?? ??? ??? ?int tempCount = 0;
?? ??? ??? ?//利用keySet方法將map中鍵放入到Set中,可以使用 ?增強(qiáng)for
?? ??? ??? ?for (int k = 0; k < vecMyHouse2.size(); k++) {
?? ??? ??? ??? ?if (vecMyHouse2[k] != max&&vecMyHouse2[k] != min)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?tempX += vecMyHouse2[k];
?? ??? ??? ??? ??? ?tempCount++;
?? ??? ??? ??? ?}


?? ??? ??? ?}
?? ??? ??? ?int value = (int)(tempX / tempCount);
?? ??? ??? ?verticalMyHouse.push_back(value);
?? ??? ??? ?num++;
?? ??? ??? ?
?? ??? ?}
?? ?}
?? ?
?? ?int tempY = 0;
?? ?//最大值:
?? ?max = *max_element(verticalMyHouse.begin(), verticalMyHouse.end());//過濾
?? ?//最小值:
?? ?min = max; //*min_element(verticalMyHouse.begin(), verticalMyHouse.end());//過濾
?? ?for (int z = 0; z < verticalMyHouse.size(); z++)
?? ?{
?? ??? ?if (min>verticalMyHouse[z] && verticalMyHouse[z] != 0)
?? ??? ?{
?? ??? ??? ?min = verticalMyHouse[z];
?? ??? ?}
?? ??? ?tempY += verticalMyHouse[z];
?? ?}
?? ?
?? ?tempY = tempY - (max + min);
?? ?if (verticalMyHouse.size()>2)
?? ?{
?? ??? ?*centerY = tempY / (verticalMyHouse.size() - 2);
?? ?}
?? ?else
?? ?{
?? ??? ?*centerY = (verticalMyHouse[0] + verticalMyHouse[1]) / 2;
?? ?}

?? ?*centerY += 1;
#endif
?? ??? ?return 0;
}

**********************************C#代碼

?public class ImageDll
? ? {
? ? ? ?[DllImport(@"G:\whh\2020\projectTest\c++\ImageAnalysis\ImageAnalysisTest2020.1.3\ImageAnalysisTest\x64\Debug\CrossDiscern.dll", EntryPoint = "GetCenterPos", CallingConvention = CallingConvention.Cdecl)]
? ? ? ?public static extern int GetCenterPos(IntPtr imageData,int step, int widthValue, int heightValue, int BinaryThreshold, int LineBlankThreshold, ref int centerX, ref int centerY);

? ? ? ?[DllImport(@"CrossDiscern.dll", EntryPoint = "Sum", CallingConvention = CallingConvention.Cdecl)]
? ? ? ?public static extern int Sum(int a, int b);

? ? ? ?//**********圖片數(shù)據(jù)轉(zhuǎn)換
? ? ? ?public static int GetPositionResult_XY(Bitmap bitmap,int BinaryThreshold, int LineBlankThreshold, ref int centerX, ref int centerY)
? ? ? ?{
#if false
? ? ? ? ? ?BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
? ? ? ? ? ?IntPtr pSt = bmpdata.Scan0;
? ? ? ? ? ?int bytes = bitmap.Width * bitmap.Height * 3;
? ? ? ? ? ?byte[] rgbValues = new byte[bytes];
? ? ? ? ? ?Marshal.Copy(pSt, rgbValues, 0, bytes);
? ? ? ? ??
? ? ? ? ? ?int res = GetCenterPos(rgbValues, bmpdata.Stride, bitmap.Width, bitmap.Height, BinaryThreshold, LineBlankThreshold, ref centerX, ref ?centerY);
? ? ? ? ? ?bitmap.UnlockBits(bmpdata);
? ? ? ? ? ?return res;
#endif
#if true
? ? ? ? ? ?BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
? ? ? ? ? ?IntPtr pSt = bmpdata.Scan0;
? ? ? ? ? ?int bytes = bitmap.Width * bitmap.Height * 3;
? ? ? ? ? ?byte[] rgbValues = new byte[bytes];
? ? ? ? ? ?Marshal.Copy(pSt, rgbValues, 0, bytes);

? ? ? ? ? ?int res = GetCenterPos(pSt, bmpdata.Stride, bitmap.Width, bitmap.Height, BinaryThreshold, LineBlankThreshold, ref centerX, ref ?centerY);
? ? ? ? ? ?bitmap.UnlockBits(bmpdata);
? ? ? ? ? ?return res;
#endif
? ? ? ?}

? ? }

c#調(diào)用

?private void button_GetCenterPos_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Bitmap map = new Bitmap(sourceImage);
? ? ? ? ? ? int x=0;
? ? ? ? ? ? int y=0;
? ? ? ? ? ?if( ImageDll.GetPositionResult_XY(map, 80, 20, ref x, ref y)==0)
? ? ? ? ? ?{
? ? ? ? ? ? ?
? ? ? ? ? ? ? ?//MessageBox.Show("識別成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
? ? ? ? ? ? ? ?string content = "";//"圖像大小:" + "\r\n";
? ? ? ? ? ? ? ?content += "起點(diǎn):X=0 Y=0"+"\r\n";
? ? ? ? ? ? ? ?content +=( "終點(diǎn):X=" + map.Width.ToString()+" Y="+map.Height.ToString()+"\r\n");
? ? ? ? ? ? ? ?content += "中線點(diǎn):X=" + x.ToString() + " Y=" + y.ToString() + "\r\n";
? ? ? ? ? ? ? ?label4.Text = content;
? ? ? ? ? ? ? ?//*********繪制中心點(diǎn)
? ? ? ? ? ? ? ?Graphics graphics = panel3.CreateGraphics();
? ? ? ? ? ? ? ?Font font = new Font("宋體",16,FontStyle.Bold/*|FontStyle.Italic|FontStyle.Underline*/,GraphicsUnit.Point);//其中宋體是字體,24是字號,FontStyle的幾個是字的樣式,最后的GraphicsUnit是字的度量單位(可以看一下上邊的表)
? ? ? ? ? ? ? ?graphics.DrawString("識別成功!\r\n" + content, font, Brushes.Blue, new Point(panel3.Width / 2 - 72, 25));//繪制字體,font是上邊定義的字體,Brushes.Brown是顏色,Point是字從哪個位置開始繪制
? ? ? ? ??
? ? ? ? ? ? ? ?Brush b = new SolidBrush(Color.Red);//聲明的畫刷

? ? ? ? ? ? ? ?int margin = 20;
? ? ? ? ? ? ? ?int tempx = Convert.ToInt32(x * 1.0 / map.Width * (panel3.Width - 2 * margin) + margin);
? ? ? ? ? ? ? ?int tempy = Convert.ToInt32(y * 1.0 / map.Height * (panel3.Height - 2 * margin) + margin);
? ? ? ? ? ? ? ?graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//平滑繪制
? ? ? ? ? ? ? ?graphics.FillEllipse(b, new Rectangle(tempx-5,tempy-5, 10,10)); ? ? ? ? ? ??
? ? ? ? ? ? ? ?graphics.Dispose();
? ? ? ? ? ?}
? ? ? ? ? ?else
? ? ? ? ? ?{
? ? ? ? ? ? ? ?MessageBox.Show("識別失敗!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
? ? ? ? ? ?}
? ? ? ? }

?

總結(jié)

以上是生活随笔為你收集整理的c#调用c++(Opencv)dll的实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。