如何在c#中使⽤opencv函数库
这个demo⽤c#实现图⽚裁剪和半透明融合的功能演⽰程序。功能挺简单的,就是把⼀张固定⼤⼩的图⽚先做边缘⽻化,然后贴到⼀个圆形泡泡形状的底图上,最后把结果半透明融合到⼀张背景图上。
C#实现图像的⽻化、将图⽚裁剪复制到⼀个圆形图⽚这些都挺简单的,最后⼀步融合到背景图上需要⽤到opencv的seamlessClone⽅法。⽹上搜索c#使⽤opencv的⽅法有很多,⼀种是直接使⽤opencv的C#版本,⼀种是先把opencv的⽅法封装到⼀个dll然后⽤c#调⽤这个dll导出的⽅法。对于我这个需求,后⼀种⽅法最合算了。我只⽤到了⼀个⽅法⽽已。vc中怎么使⽤opencv就不说了,直接开始吧。
⽤VC创建⼀个win32项⽬,选择⽣成dll动态链接库。
添加⼀个cpp⽂件:
// opencvImage.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include "opencv2/photo.hpp"
#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(img0.size(), CV_8UC3);
Mat mask = Mat::zeros(img0.size(), 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);
}
由于使⽤了_stdcall,所以还需要⼀个def⽂件,否则导出函数名字前⾯有个下划线,_Blend
加⼀个def⽂件:
LIBRARY "opencvImage"
EXPORTS
Blend @1
PoissonBlend @2
C#调⽤⽅法:
1. 在cs⽂件开头添加
using System.Runtime.InteropServices;
2.在⽤到的地⽅添加
[DllImport("opencvImage.dll")]
unsafe public static extern void Blend(byte* src, int width, int height, int stride, int bitcount, byte* dst, int alpha);
3.调⽤dll⽅法
Bitmap bp = new Bitmap(srcImage);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(x, y, width, height);
BitmapData src = bp.LockBits(rect, ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
Bitmap dp = new Bitmap(destImage);
System.Drawing.Rectangle dstRect = new System.Drawing.Rectangle(dx, dy, width, height);
BitmapData dst = dp.LockBits(dstRect, ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
int nPitch = src.Stride;
int bitCount = 3;
byte* lpsrc = (byte*)src.Scan0;
byte* lpdst = (byte*)dst.Scan0;
byte alpha = 128;
Blend(lpsrc, width, height, nPitch, bitCount ,lpdst, alpha);
bp.UnlockBits(src);
dp.UnlockBits(dst);
return dp;
rectangle函数opencv最后说明⼀下
1.VC编译的dll需要放到C#可执⾏程序的⽬录下,也就是bin⽬录下的debug或release⽬录下,否则会提⽰⼀些错误。dll的依赖库也需要放进来,⽐如我⽤到了opencv_core331.dll opencv_imgproc331.dll opencv_photo331.dll三个库。
2.如果C#程序不需要⽤到opencv的对象,可以直接传递指针给dll,这样使⽤起来很⽅便。就像下⾯这样:
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.很多⼈说c#调⽤opencv封装的dll会遇到内存泄漏⽅⾯的问题,我觉得如果不⽤opencv的对象,内存申请和释放都在c#中完成,只是传递指针给opencv做处理,应该没问题。
以上就是如何在c#中使⽤opencv的详细内容,更多关于c#中使⽤opencv的资料请关注其它相关⽂章!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论