OPENCV去除小连通区域,去除孔洞的实例讲解
程序员文章站
2022-07-22 20:25:20
一、对于二值图,0代表黑色,255代表白色。去除小连通区域与孔洞,小连通区域用8邻域,孔洞用4邻域。
函数名字为:void removesmallregion(mat...
一、对于二值图,0代表黑色,255代表白色。去除小连通区域与孔洞,小连通区域用8邻域,孔洞用4邻域。
函数名字为:void removesmallregion(mat &src, mat &dst,int arealimit, int checkmode, int neihbormode)
checkmode: 0代表去除黑区域,1代表去除白区域; neihbormode:0代表4邻域,1代表8邻域;
如果去除小连通区域checkmode=1,neihbormode=1去除孔洞checkmode=0,neihbormode=0
记录每个像素点检验状态的标签,0代表未检查,1代表正在检查,2代表检查不合格(需要反转颜色),3代表检查合格或不需检查 。
1.先对整个图像扫描,如果是去除小连通区域,则将黑色的背景图作为合格,像素值标记为3,如果是去除孔洞,则将白色的色素点作为合格,像素值标记为3。
2.扫面整个图像,对图像进行处理。
void removesmallregion(mat &src, mat &dst,int arealimit, int checkmode, int neihbormode) { int removecount = 0; //新建一幅标签图像初始化为0像素点,为了记录每个像素点检验状态的标签,0代表未检查,1代表正在检查,2代表检查不合格(需要反转颜色),3代表检查合格或不需检查 //初始化的图像全部为0,未检查 mat pointlabel = mat::zeros(src.size(), cv_8uc1); if (checkmode == 1)//去除小连通区域的白色点 { cout << "去除小连通域."; for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++) { if (src.at<uchar>(i, j) < 10) { pointlabel.at<uchar>(i, j) = 3;//将背景黑色点标记为合格,像素为3 } } } } else//去除孔洞,黑色点像素 { cout << "去除孔洞"; for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++) { if (src.at<uchar>(i, j) > 10) { pointlabel.at<uchar>(i, j) = 3;//如果原图是白色区域,标记为合格,像素为3 } } } } vector<point2i>neihborpos;//将邻域压进容器 neihborpos.push_back(point2i(-1, 0)); neihborpos.push_back(point2i(1, 0)); neihborpos.push_back(point2i(0, -1)); neihborpos.push_back(point2i(0, 1)); if (neihbormode == 1) { cout << "neighbor mode: 8邻域." << endl; neihborpos.push_back(point2i(-1, -1)); neihborpos.push_back(point2i(-1, 1)); neihborpos.push_back(point2i(1, -1)); neihborpos.push_back(point2i(1, 1)); } else cout << "neighbor mode: 4邻域." << endl; int neihborcount = 4 + 4 * neihbormode; int currx = 0, curry = 0; //开始检测 for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++) { if (pointlabel.at<uchar>(i, j) == 0)//标签图像像素点为0,表示还未检查的不合格点 { //开始检查 vector<point2i>growbuffer;//记录检查像素点的个数 growbuffer.push_back(point2i(j, i)); pointlabel.at<uchar>(i, j) = 1;//标记为正在检查 int checkresult = 0; for (int z = 0; z < growbuffer.size(); z++) { for (int q = 0; q < neihborcount; q++) { currx = growbuffer.at(z).x + neihborpos.at(q).x; curry = growbuffer.at(z).y + neihborpos.at(q).y; if (currx >= 0 && currx<src.cols&&curry >= 0 && curry<src.rows) //防止越界 { if (pointlabel.at<uchar>(curry, currx) == 0) { growbuffer.push_back(point2i(currx, curry)); //邻域点加入buffer pointlabel.at<uchar>(curry, currx) = 1; //更新邻域点的检查标签,避免重复检查 } } } } if (growbuffer.size()>arealimit) //判断结果(是否超出限定的大小),1为未超出,2为超出 checkresult = 2; else { checkresult = 1; removecount++;//记录有多少区域被去除 } for (int z = 0; z < growbuffer.size(); z++) { currx = growbuffer.at(z).x; curry = growbuffer.at(z).y; pointlabel.at<uchar>(curry,currx)+=checkresult;//标记不合格的像素点,像素值为2 } //********结束该点处的检查********** } } } checkmode = 255 * (1 - checkmode); //开始反转面积过小的区域 for (int i = 0; i < src.rows; ++i) { for (int j = 0; j < src.cols; ++j) { if (pointlabel.at<uchar>(i,j)==2) { dst.at<uchar>(i, j) = checkmode; } else if (pointlabel.at<uchar>(i, j) == 3) { dst.at<uchar>(i, j) = src.at<uchar>(i, j); } } } cout << removecount << " objects removed." << endl; }
调用函数:dst是原来的二值图。
mat erzhi1 = mat::zeros(srcimage.rows, srcimage.cols, cv_8uc1); removesmallregion(dst, erzhi,100, 1, 1); removesmallregion(erzhi, erzhi,100, 0, 0); imshow("erzhi1", erzhi);
和之前的图像相比
以上这篇opencv去除小连通区域,去除孔洞的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: 现在和大家说说*和田大枣子
下一篇: 海带有什么功效,可别只用了煮汤