在emugcv中,由于有的函数没有封装,需要使用的时候,只能通过调用c++写的opencv库,以下是方法。
1、写C++的库,具体可以先参考这个VS2019下配置Opencv4.5的方法步骤
在c++中写两个文件,分别是RqPolar.h和RqPolar.cpp
RqPolar.h内容如下
#pragma once
#include <iostream>
#include<opencv2\opencv.hpp>
//#include<opencv2\core\base.hpp>
using namespace cv;
extern "C" __declspec(dllexport) void RqWarpPolar(Mat * theSrc8C1, Mat * theDst8C1,
int theDstWid, int theDstHei,
int theCentX, int theCentY,
int theMaxR, int theFlags);
extern "C" __declspec(dllexport) void RqThreshold(Mat * theSrc8C1, Mat * theDst8C1, int yuzhi);
extern "C" __declspec(dllexport) int Add(int a, int b);
RqPolar.cpp中内容如下
#include "pch.h"
#include "RqPolar.h"
void RqWarpPolar(Mat *theSrc8C1, Mat *theDst8C1,
int theDstWid, int theDstHei,
int theCentX, int theCentY,
int theMaxR, int theFlags)
{
int flag = INTER_LINEAR + WARP_POLAR_LINEAR;
cv::warpPolar(*theSrc8C1, *theDst8C1, Size(theDstWid, theDstHei), Point2f(theCentX, theCentY), theMaxR, theFlags);
}
void RqThreshold(Mat* theSrc8C1, Mat* theDst8C1, int yuzhi)
{
cv::threshold(*theSrc8C1, *theDst8C1, yuzhi, 255, 0);
}
int Add(int a, int b)
{
return a + b;
}
编译成C++库,
2、在C#中的调用程序如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
namespace CallOpencv
{
public partial class Form1 : Form
{
//----------------------------------------------------------------------------------------------
[DllImport("PolarDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
extern static void RqWarpPolar(IntPtr theSrc8C1, IntPtr theDst8C1, int theDstWid, int theDstHei, int theCentX, int theCentY, int theMaxR, int theFlags);
[DllImport("PolarDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
extern static void RqThreshold(IntPtr theSrc8C1, IntPtr theDst8C1, int yuzhi);
[DllImport("PolarDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
extern static int Add(int a, int b);
public Form1()
{
InitializeComponent();
//================================
int aa = Add(1, 5);
Mat glMain8C1 = new Mat(@"C:\Users\lhyra\Desktop\1111\011.bmp", Emgu.CV.CvEnum.ImreadModes.Grayscale);
//===============二值化
Mat erzhi8C1 = new Mat();
RqThreshold(glMain8C1, erzhi8C1, 80);
//=============针对进行极坐标变换
Mat thePolar8C1 = new Mat(new Size(60, 360), Emgu.CV.CvEnum.DepthType.Cv8U, 1);
RqWarpPolar(glMain8C1, thePolar8C1, thePolar8C1.Width, thePolar8C1.Height, glMain8C1.Width / 2, glMain8C1.Height / 2, 60, 1);
imageBox1.Image = glMain8C1;
imageBox2.Image = thePolar8C1;
CvInvoke.Imwrite("D:\\333.bmp", thePolar8C1);
int jjj = 0;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
3、最后运行画面如下,主要是极坐标变换,用来找圆
4、最后的程序在下面连接中程序在CSDN中下载