OpenCV实现二值图像的边缘光滑处理
萌哒萌哒 人气:1本文着重为大家仔细讲解了OpenCV实现二值图像的边缘光滑处理,文中代码实例讲解的非常细致,希望能够帮助到您,欢迎大家阅读和收藏
处理代码分为两部分,第一部分用于去除边缘的突出部,第二部分用于边缘光滑。具体如下所示
1.去除边缘突出部
//去除二值图像边缘的突出部 //uthreshold、vthreshold分别表示突出部的宽度阈值和高度阈值 //type代表突出部的颜色,0表示黑色,1代表白色 void delete_jut(Mat& src, Mat& dst, int uthreshold, int vthreshold, int type) { int threshold; 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<uchar>(i); for (int j = 0; j < width - 1; j++) { if (type == 0) { //行消除 if (p[j] == 255 && p[j + 1] == 0) { if (j + uthreshold >= 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 < k; h++) p[h] = 255; } } } //列消除 if (p[j] == 255 && p[j + width] == 0) { if (i + vthreshold >= height) { for (k = j + width; k < j + (height - i)*width; k += width) p[k] = 255; } else { for (k = j + 2 * width; k <= j + vthreshold*width; k += width) { if (p[k] == 255) break; } if (p[k] == 255) { for (int h = j + width; h < k; h += width) p[h] = 255; } } } } else //type = 1 { //行消除 if (p[j] == 0 && p[j + 1] == 255) { if (j + uthreshold >= 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 < k; h++) p[h] = 0; } } } //列消除 if (p[j] == 0 && p[j + width] == 255) { if (i + vthreshold >= height) { for (k = j + width; k < j + (height - i)*width; k += width) p[k] = 0; } else { for (k = j + 2 * width; k <= j + vthreshold*width; k += width) { if (p[k] == 0) break; } if (p[k] == 0) { for (int h = j + width; h < k; h += width) p[h] = 0; } } } } } } }
效果如下:
2.边缘光滑处理
//图片边缘光滑处理 //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<uchar>(i); for (int j = 0; j < width; j++) { if (p[j] < threshold) p[j] = 0; else p[j] = 255; } } imshow("Blur", dst); }
效果如下:
加载全部内容