当前位置: 移动技术网 > IT编程>开发语言>.net > Emgucv使用中常用函数总结

Emgucv使用中常用函数总结

2019年01月05日  | 移动技术网IT编程  | 我要评论

临沂汽车,始祖家族第三季,家电控制器

emgucv常用函数总结:
读取图片
mat scr = new mat(form1.path, emgu.cv.cvenum.loadimagetype.anycolor);
//根据路径创建指定的灰度图片
mat scr = new mat(form1.path, emgu.cv.cvenum.loadimagetype.grayscale);
获取灰度    //图像类型转换, bgr 转成 gray 类型。mat bw = new mat
cvinvoke.cvtcolor(scr, bw, emgu.cv.cvenum.colorconversion.bgr2gray);
//相当于二值化图 --黑白 根据大小10判断为0还是255
cvinvoke.threshold(bw,bw,10,255,emgu.cv.cvenum.thresholdtype.binaryinv);
//获取指定区域图片 scr为mat类型
rectangle rectangle = new rectangle(10,10,10,10);
scr = scr.toimage<bgr, byte>().getsubrect(rectangle).mat;
//将mat类型转换为image类型
image<bgr, byte> su = scr.toimage<bgr, byte>();
image<bgr, byte> img = new image<bgr, byte>(new bitmap(""));//路径声明
image<bgr, byte> sub = scr.toimage<bgr, byte>().getsubrect(rectangle);//指定范围
//指定参数获得结构元素
mat struct_element = cvinvoke.getstructuringelement(emgu.cv.cvenum.elementshape.cross, new size(3, 3), new point(-1, -1));
//膨胀
cvinvoke.dilate(bw, bw, struct_element, new point(1,1),3,emgu.cv.cvenum.bordertype.default, new mcvscalar(0, 0, 0));
//腐蚀 当struct_element模型创建不合理或者膨胀腐蚀次数较大时可能图像会发生偏移
cvinvoke.erode(bw, bw, struct_element, new point(-1, -1), 3,emgu.cv.cvenum.bordertype.default, new mcvscalar(0, 0, 0));
//轮廓提取
vectorofvectorofpoint contours = new vectorofvectorofpoint();
//筛选后
cvinvoke.findcontours(bw, contours, null, emgu.cv.cvenum.retrtype.list, emgu.cv.cvenum.chainapproxmethod.chainapproxsimple);
int ksize = contours.size;//获取连通区域的个数。     
vectorofpoint contour = contours[i];//获取独立的连通轮廓   
rectangle rect = cvinvoke.boundingrectangle(contour);//提取最外部矩形。
double length = cvinvoke.arclength(contour, false);//计算连通轮廓的周长。
//画出轮廓
mat mask = bw.toimage<bgr, byte>().copyblank().mat;
//获取一张背景为黑色的图像, 大小与 scr 的大小一样, 类型为 bgr。
cvinvoke.drawcontours(mask, contours, -1, new mcvscalar(0, 0, 255));
image<ycc, byte> ycc_img = bgr_img.convert<ycc, byte>();//把 bgr颜色图片转成ycbcr类型。
ycc min = new ycc(152, 38, 118);//最小值的颜色。
ycc max = new ycc(94, 43, 118);//最大值得颜色。
image<gray, byte> result = ycc_img.inrange(min, max);//进行颜色提取。
image<bgr, byte> bgr_img = ma.toimage<bgr, byte>();//载入一张 bgr 类型的图片。
bgr min = new bgr(255, 255, 255);//白色的最小值, 允许一定154的误差。
bgr max = new bgr(255, 255, 255);//白色的最大值, 允许一定的误差。
image<gray, byte> result = bgr_img.inrange(min, max);//进行颜色提取。
image<bgr, byte> imagesource = new image<bgr, byte>(scr.bitmap);
image<hsv, byte> imagehsv = imagesource.convert<hsv, byte>();//将色彩空间从bgr转换到hsv
image<gray, byte>[] imageshsv = imagehsv.split();//分解成h、s、v三部分
cvinvoke.absdiff(ma1, ma2, ma); // 返回两幅图片或此图与某个yanse像素的差的绝对值的图片
cvinvoke.add(ma1, ma2, ma); // 返回这张图片与图片或颜色直接相加的图片(矩阵加法)  (适应两种效果)
//cvinvoke.hconcat(ma1, ma2, ma); //返回与另一张图片横向链接的图片
//cvinvoke.vconcat(ma1, ma2, ma);//返回与另一张图片纵向链接的图片

//清除小于平均顶点10的二值图
point[] po = { new point(0, 0), new point(res.width, 0), new point(res.width, minavg - gets.fges[1] + 52), new point(0, minavg - gets.fges[1] + 52) };
vectorofpoint vp = new vectorofpoint(po);
//cvinvoke.drawcontours(res, vp, -1, new mcvscalar(0, 0, 255));
cvinvoke.fillconvexpoly(res,vp,new mcvscalar(0),linetype.eightconnected);//填充指定区域

/// <summary>
/// 灰度直方图计算  手动计算、/获取百分比的阀值  0.95
/// </summary>
public static void getdensehistogram95(ref int huidu, mat ma)
{
            densehistogram dense = new densehistogram(256, new rangef(0, 255));
            dense.calculate(new image<gray, byte>[] { ma.toimage<gray, byte>() }, true, null);
            //计算直方图数据。
            float[] data = dense.getbinvalues();
            float[] data2 = dense.getbinvalues();
            //获得直方图数据。
            /*** 进行数据归一化到[0,256]区域内并且绘制直方图***/
            float max = data[0]; //最大值
            for (int j = 1; j < data.length; j++)
            {
                if (data[j] > max)
                {
                    max = data[j];
                }
            }
            float sum = data2.tolist().sum();
            float flocount = 0;
            for (int k = 0; k < data.length; k++)
            {
                data[k] = data[k] * 256 / max;
                flocount += data2[k];
                if (flocount / sum >= 0.95)
                {
                    huidu = k;
                    break;
                }
            }}

//各种颜色空间 hsv/rgb/hls/xyz/ycc/gray
public static image<hsv, byte> imagehsv=new image<hsv, byte>(mat.bitmap);
public static image<rgb, byte> rgbimg = new image<rgb, byte>(mat.bitmap);
public static image<hls, byte> hlsimg = new image<hls, byte>(mat.bitmap);
public static image<xyz, byte> xyzimg = new image<xyz, byte>(mat.bitmap);
public static image<ycc, byte> yccimg = new image<ycc, byte>(mat.bitmap);
public static image<gray, byte> grayimg = new image<gray, byte>(mat.bitmap);
image<gray, byte>[] imageshsvs = imagehsv.split();//分解成h、s、v三部分其他相同
//高斯滤波实现
cvinvoke.gaussianblur(ma, ma, new size(5, 5), 4);
//形态学闭运算,先膨胀后腐蚀  others.matwithphi(by)自定义模型
cvinvoke.morphologyex(ma, ma, emgu.cv.cvenum.morphop.close, others.matwithphi(by), new point(-1, -1), 3, emgu.cv.cvenum.bordertype.default, new mcvscalar(0, 0, 0));
cvinvoke.medianblur(ma, ma, 5);//中值滤波实现
cvinvoke.puttext(ma05, "g num: 1", new point(10, 100), fontface.hersheycomplex, 0.5, new mcvscalar(255)); //指定坐标(10, 100)显示文字,中文乱码,
vectorofpoint vp = new vectorofpoint();
cvinvoke.convexhull(pointof, vp);////查找最小外接矩形cvinpaint
double dou = cvinvoke.contourarea(vp, false);  //计算面积
vectorofpoint vect = new vectorofpoint();
cvinvoke.findnonzero(ma, vect); //获取非0的点
mat masave1 = ma5.clone();//备份 保留原有图片
cvinvoke.adaptivethreshold(ma, mas, 255, adaptivethresholdtype.gaussianc, emgu.cv.cvenum.thresholdtype.binary, 3, 0);//查找最适合二值图

收集的emgucv的整理书籍资料和emgucv动态调试器

本文代码下载:下载

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网