这篇文章主要为大家详细介绍了OpenCV实现二值图像的边缘光滑处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了OpenCV学习笔记之针对二值图像的边缘光滑处理(突出部消除),供大家参考,具体内容如下
处理代码分为两部分,第一部分用于去除边缘的突出部,第二部分用于边缘光滑。具体如下所示
1.去除边缘突出部
//去除二值图像边缘的突出部 //uthreshold、vthreshold分别表示突出部的宽度阈值和高度阈值 //type代表突出部的颜色,0表示黑色,1代表白色 void delete_jut(Mat& src, Mat& dst, int uthreshold, int vthreshold, int type) { int threshold<strong style="color:transparent">来源gao@daima#com搞(%代@#码网</strong>; src.copyTo(dst); int height = dst.rows; int width = dst.cols; int k; //用于循环计数传递到外部 for (int i = 0; i <height - 1; i++) { uchar* p=dst.ptr(i); for (int j = 0; j = width) { for (int k = j + 1; k <width; k++) p[k] = 255; } else { for (k = j + 2; k <= j + uthreshold; k++) { if (p[k] == 255) break; } if (p[k] == 255) { for (int h = j + 1; h = height) { for (k = j + width; k <j + (height - i)*width; k width) p[k]=255; } else { for (k=j 2 * width; = width) { for (int k = j + 1; k <width; k++) p[k] = 0; } else { for (k = j + 2; k <= j + uthreshold; k++) { if (p[k] == 0) break; } if (p[k] == 0) { for (int h = j + 1; h = height) { for (k = j + width; k <j + (height - i)*width; k width) p[k]=0; } else { for (k=j 2 * width; < vthreshold*width; if (p[k]== 0) break; (int h k; p[h]=0; }<pre></div><p>效果如下:</p><p style="text-align: center"></p><p style="text-align: center"></p><p><strong>2.边缘光滑处理</strong></p><div class="gaodaimacode"><pre class="prettyprint linenums"> //图片边缘光滑处理 //size表示取均值的窗口大小,threshold表示对均值图像进行二值化的阈值 void imageblur(Mat& src, Mat& dst, Size size, int threshold) { int height = src.rows; int width = src.cols; blur(src, dst, size); for (int i = 0; i <height; i++) { uchar* p = dst.ptr(i); for (int j = 0; j <width; j++) { if (p[j] <threshold) p[j] = 0; else p[j] = 255; } } imshow("Blur", dst); }
效果如下:
以上就是OpenCV实现二值图像的边缘光滑处理的详细内容,更多请关注gaodaima搞代码网其它相关文章!