图像分割与距离变换和流域算法
2017-04-10 09:23
246 查看
1.文章内容
使用OpenCV函数cv ::filter2D为了执行一些拉普拉斯过滤,来进行图像锐化使用OpenCV函数cv ::distanceTransform来获得二进制图像的导出表示,
其中每个像素的值被替换为最近的背景像素的距离
使用OpenCV函数cv ::watershed来隔离图像中的对象与背景
2.教程
This tutorial code's is shown lines below. You can also download it fromhere.
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; int main(int, char** argv) { // Load the image 加载图片 Mat src = imread(argv[1]); // Check if everything was fine 检查数据是否完好 if (!src.data) return -1; // Show source image 展示原图片 imshow("Source Image", src); // Change the background from white to black, since that will help later to extract //改变背景从白色到黑色,因为这将有助于以后提取 // better results during the use of Distance Transform //使用距离变换更好的效果 for( int x = 0; x < src.rows; x++ ) { for( int y = 0; y < src.cols; y++ ) { if ( src.at<Vec3b>(x, y) == Vec3b(255,255,255) ) { src.at<Vec3b>(x, y)[0] = 0; src.at<Vec3b>(x, y)[1] = 0; src.at<Vec3b>(x, y)[2] = 0; } } } // Show output image 展示输出图片 imshow("Black Background Image", src); // Create a kernel that we will use for accuting/sharpening our image //创建一个我们将用于核算/锐化我们的图像的内核 Mat kernel = (Mat_<float>(3,3) << 1, 1, 1, 1, -8, 1, 1, 1, 1); // an approximation of second derivative, a quite strong kernel //二阶导数近似值,一个相当强的内核 // do the laplacian filtering as it is // well, we need to convert everything in something more deeper then CV_8U // because the kernel has some negative values, // and we can expect in general to have a Laplacian image with negative values // BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255 // so the possible negative number will be truncated //执行拉普拉斯滤镜 //好了,我们需要将所有东西都转换成更深层次的东西,然后CV_8U //因为内核有一些负值, //我们可以期待一般来说具有负值的拉普拉斯图像 //但是一个8bits unsigned int(我们正在使用的)可以包含从0到255的值 //所以可能的负数将被截断 Mat imgLaplacian; Mat sharp = src; // copy source image to another temporary one filter2D(sharp, imgLaplacian, CV_32F, kernel); src.convertTo(sharp, CV_32F); Mat imgResult = sharp - imgLaplacian; // convert back to 8bits gray scale //转换为8位灰度 imgResult.convertTo(imgResult, CV_8UC3); imgLaplacian.convertTo(imgLaplacian, CV_8UC3); // imshow( "Laplace Filtered Image", imgLaplacian ); imshow( "New Sharped Image", imgResult ); src = imgResult; // copy back // Create binary image from source image //从源图像创建二进制图像 Mat bw; cvtColor(src, bw, CV_BGR2GRAY); threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); imshow("Binary Image", bw); // Perform the distance transform algorithm //执行距离变换算法 Mat dist; distanceTransform(bw, dist, CV_DIST_L2, 3); // Normalize the distance image for range = {0.0, 1.0} // so we can visualize and threshold it //范围= {0.0,1.0}的距离图像归一化 //所以我们可以可视化和限制它 normalize(dist, dist, 0, 1., NORM_MINMAX); imshow("Distance Transform Image", dist); // Threshold to obtain the peaks // This will be the markers for the foreground objects //获取峰值的阈值 //这将是前景对象的标记 threshold(dist, dist, .4, 1., CV_THRESH_BINARY); // Dilate a bit the dist image //稀释一点dist图像 Mat kernel1 = Mat::ones(3, 3, CV_8UC1); dilate(dist, dist, kernel1); imshow("Peaks", dist); // Create the CV_8U version of the distance image // It is needed for findContours() //创建CV_8U版本的距离图像 // findContours()需要 Mat dist_8u; dist.convertTo(dist_8u, CV_8U); // Find total markers //查找总标记 vector<vector<Point> > contours; findContours(dist_8u, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // Create the marker image for the watershed algorithm //为分水岭算法创建标记图像 Mat markers = Mat::zeros(dist.size(), CV_32SC1); // Draw the foreground markers //绘制前景标记 for (size_t i = 0; i < contours.size(); i++) drawContours(markers, contours, static_cast<int>(i), Scalar::all(static_cast<int>(i)+1), -1); // Draw the background marker //绘制背景标记 circle(markers, Point(5,5), 3, CV_RGB(255,255,255), -1); imshow("Markers", markers*10000); // Perform the watershed algorithm //执行分水岭算法 watershed(src, markers); Mat mark = Mat::zeros(markers.size(), CV_8UC1); markers.convertTo(mark, CV_8UC1); bitwise_not(mark, mark); // imshow("Markers_v2", mark); // uncomment this if you want to see how the mark // image looks like at that point //取消注释,如果你想看看如何标记 //图像看起来就像这样 // Generate random colors //生成随机颜色 vector<Vec3b> colors; for (size_t i = 0; i < contours.size(); i++) { int b = theRNG().uniform(0, 255); int g = theRNG().uniform(0, 255); int r = theRNG().uniform(0, 255); colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r)); } // Create the result image //创建结果图像 Mat dst = Mat::zeros(markers.size(), CV_8UC3); // Fill labeled objects with random colors //用随机颜色填充标签对象 for (int i = 0; i < markers.rows; i++) { for (int j = 0; j < markers.cols; j++) { int index = markers.at<int>(i,j); if (index > 0 && index <= static_cast<int>(contours.size())) dst.at<Vec3b>(i,j) = colors[index-1]; else dst.at<Vec3b>(i,j) = Vec3b(0,0,0); } } // Visualize the final image //可视化最终图像 imshow("Final Result", dst); waitKey(0); return 0; }
3.解释/结果
1.加载源图像并检查是否加载没有任何问题,然后显示
// Load the image 加载图片 Mat src = imread(argv[1]); // Check if everything was fine if (!src.data) return -1; // Show source image 展示图片 imshow("Source Image", src);
相关文章推荐
- 图像快速处理算法II 距离变换
- 图像处理之基于采样距离变换算法
- python OpenCV学习笔记(二十九):图像流域(分水岭)分割算法
- 图像基本变换---Canny边缘检测算法
- 图像分割基础算法及实现实例
- 图像分割方法deeplab以及Hole算法解析
- PhotoShop算法实现--图像的镜像变换(十六)
- 图像处理与识别的算法中若有非线性变换,那么请一定注意归一化的处理会影响结果!
- 数字图像处理基本算法实现(1)--section3.2基本灰度变换
- 图像匹配算法之初探尺度不变特征变换(SIFT)算法
- 基于梯度调整的矩不变自动阈值图像分割算法
- 有关图像分割算法的几个网站
- 距离变换distanceTransform应用——寻找图像的质心
- 霍夫的图像处理变换(线检测算法)
- 模式识别经典算法——Kmeans图像聚类分割(以最短的matlab程序实现)
- 转载:Contour Detection and Hierarchical Image Segmentation 图像边缘分割算法的学习与理解
- itk中的图像分割算法(二)
- itk中的图像分割算法(三)
- 图像标注 图像分割,特征提取和分类算法
- 模式识别经典算法——Kmeans图像聚类分割(以最短的matlab程序实现)