SoFunction
Updated on 2025-03-01

How to use opencv function library in c#

This demo uses C# to implement the functional demonstration program for image cropping and translucent fusion. The function is quite simple, which is to first feather the edges of a fixed-sized picture, then paste it onto a round bubble-shaped base map, and finally blend the result translucently onto a background image.

C# implements the image feathering and cropping and copying the image to a circular image are quite simple. The last step to integrate it into the background image requires the opencv's seamlessClone method. There are many ways to search online for c# to use opencv. One is to directly use the C# version of opencv, and the other is to first encapsulate the opencv method into a dll and then use c# to call the method exported by this dll. For my needs, the latter method is the most cost-effective. I only used one method. I won’t talk about how to use opencv in vc, just start.

Create a win32 project with VC and select Generate DLL dynamic link library.

Add a cpp file:

//  : Defines the exported functions for the DLL application.
//
 
#include ""
#include <opencv2/core/>
#include "opencv2/"
 
#ifdef _DEBUG
#pragma comment(lib,"opencv_core331d.lib")
#pragma comment(lib,"opencv_imgproc331d.lib")
#pragma comment(lib,"opencv_photo331d.lib")
#else
#pragma comment(lib,"opencv_core331.lib")
#pragma comment(lib,"opencv_imgproc331.lib")
#pragma comment(lib,"opencv_photo331.lib")
#endif
 
#define Export_API extern "C" _declspec(dllexport)
 
using namespace cv;
 
Export_API void _stdcall Blend(unsigned char * src, int width, int height, int stride,
							 int bitcount, unsigned char *dst, int alpha)
{
	double beta,alpha1 = alpha / 255.0 ;
	Mat src1(width, height, CV_8UC3, src, stride);
	Mat src2(width, height, CV_8UC3, dst, stride);
	Mat dst1(width, height, CV_8UC3, dst, stride);
 
	beta = (1.0 - alpha1);
 
	addWeighted(src1, alpha1, src2, beta, 0.0, dst1);
}
 
Export_API void _stdcall PoissonBlend(unsigned char * src, int width, int height, int stride,
									 int bitcount, unsigned char *dst, int flags)
{
	int cx = width >> 1;
	int cy = height >> 1;
	int numpts = 5;
	Point point(cx, cy);
	Point pts[] = { { 80, 40 }, { 30, height - 80 }, { cx, height - 20 }, { width - 30, height - 80 }, { width - 80, 40 } };
	Mat img0(width, height, CV_8UC3, src, stride);
	Mat dst0(width, height, CV_8UC3, dst, stride);
	Mat final = Mat::zeros((), CV_8UC3);
	Mat mask = Mat::zeros((), CV_8UC1);
	const Point* pts4[1] = { &pts[0] };
	fillPoly(mask, pts4, &numpts, 1, Scalar(255, 255, 255), 8, 0);
//	circle(mask, point, cx - 30, Scalar(255, 255, 255),-1);
	bitwise_and(img0, img0, final, mask);
	seamlessClone(img0, dst0, mask, point, dst0, flags);
}

Since _stdcall is used, a def file is also needed, otherwise there is an underscore in front of the export function name, _Blend

Add a def file:

LIBRARY "opencvImage"
 
EXPORTS
 
Blend @1
 
PoissonBlend @2

C# call method:

1. Add at the beginning of the cs file

using ;

2. Add it where it is used

[DllImport("")]
unsafe public static extern void Blend(byte* src, int width, int height, int stride, int bitcount, byte* dst, int alpha);

3. Call the dll method

Bitmap bp = new Bitmap(srcImage);
 
 rect = new (x, y, width, height);
 
BitmapData src = (rect, ,PixelFormat.Format24bppRgb);
Bitmap dp = new Bitmap(destImage);
 dstRect = new (dx, dy, width, height);
BitmapData dst = (dstRect, ,PixelFormat.Format24bppRgb);
 
int nPitch = ;
int bitCount = 3;
byte* lpsrc = (byte*)src.Scan0;
byte* lpdst = (byte*)dst.Scan0;
byte alpha = 128;
Blend(lpsrc, width, height, nPitch, bitCount ,lpdst, alpha);
(src);
(dst);
return dp;

Let me explain it last

The compiled dll needs to be placed in the directory of the C# executable program, that is, the debug or release directory in the bin directory, otherwise some errors will be prompted. The dependency library of dll also needs to be put in. For example, I used three libraries: opencv_core331.dll opencv_imgproc331.dll opencv_photo331.dll.

2. If the C# program does not need to use opencv objects, you can directly pass the pointer to the dll, which is very convenient to use. Just like this:

Export_API void _stdcall Blend(unsigned char * src, int width, int height, int stride,
							 int bitcount, unsigned char *dst, int alpha)
{
	double beta,alpha1 = alpha / 255.0 ;
	Mat src1(width, height, CV_8UC3, src, stride);
	Mat src2(width, height, CV_8UC3, dst, stride);
	Mat dst1(width, height, CV_8UC3, dst, stride);
 
	beta = (1.0 - alpha1);
 
	addWeighted(src1, alpha1, src2, beta, 0.0, dst1);
}

3. Many people say that calling opencv encapsulated dlls in c# will encounter memory leak problems. I think if you do not use opencv objects, the memory application and release are completed in c#, and just passing pointers to opencv for processing, there should be no problem.

The above is the detailed content of how to use opencv in C#. For more information about using opencv in C#, please follow my other related articles!