一直没怎么用过c#做开发,加上这次一共用c#做过两个程序,第一个是关于修改网页的,用C#结合com组件还是很方便的。这次帮别人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
#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;
最后说明一下
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#基本是个菜鸟,什么托管之类的没用到过,所以c++和c#联合编程里面对于托管的处理方式也不会。
4#很多人说c#调用opencv封装的dll会遇到内存泄漏方面的问题,我觉得如果不用opencv的对象,内存申请和释放都在c#中完成,只是传递指针给opencv做处理,应该没问题。