OpenCV4.9的基于距离变换和分水岭算法的图像分割(66)

79 篇文章 0 订阅
78 篇文章 0 订阅
 返回:OpenCV系列文章目录(持续更新中......)
上一篇:OpenCV4.9的点多边形测试(65)
下一篇 :OpenCV4.9失焦去模糊滤镜(67)

目标

在本教程中,您将学习如何:

  • 使用 OpenCV 函数 cv::filter2D 执行一些拉普拉斯滤波以进行图像锐化
  • 使用 OpenCV 函数 cv::d istanceTransform 获取二进制图像的派生表示,其中每个像素的值被替换为其到最近背景像素的距离
  • 使用 OpenCV 函数 cv::watershed 将图像中的对象与背景隔离开来

OpenCV中基于距离变换和分水岭算法的图像分割可以通过以下步骤实现:

1. 对输入图像进行预处理,如去噪、二值化等。

2. 计算输入图像中每个像素的距离变换。距离变换可以指定基于欧式距离或者基于曼哈顿距离。

3. 根据计算的距离变换,确定输入图像中的每个像素点属于哪个区域。这可以通过分水岭算法实现。

4. 对图像进行分割并显示结果。

距离变换:

距离变换是一种用于图像分割的技术。它旨在确定每个像素点距离其最近非零点的距离,也称为距离图。距离变换的计算基于距离变换算子,可以使用不同的距离测量方法,例如欧几里得距离和曼哈顿距离等。

分水岭算法:

分水岭算法是一种基于图像处理的无监督分割技术,它是由Vincent和Soille于1991年提出的,并且在OpenCV中被广泛使用。其基本思想是把图像看成一个地形,将灰度值看作海拔。将图像中的某些像素点看成是水源,然后让水从这些点开始向外流,最终形成累积数、改变水流方向的水坝。

算法步骤:

(1) 对输入图像进行预处理操作。

(2) 计算输入图像的距离变换。

(3) 将距离变换作为分水岭算法的标记,标记初始化设置为0。

(4) 将标记与水坝合并,水从被选中的种子像素点开始向外流动,直到被周围区域的水坝拦住。

(5) 标记每个像素点所在的水坝并输出结果。

距离变换和分水岭算法结合起来,可以在图像分割方面提供有效的结果。这种技术可以用于医学图像处理、图像识别和计算机视觉等领域。

C++代码
 

本教程代码如下所示。您也可以从这里下载。

 
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
 
using namespace std;
using namespace cv;
 
int main(int argc, char *argv[])
{
 // Load the image
 CommandLineParser parser( argc, argv, "{@input | cards.png | input image}" );
 Mat src = imread( samples::findFile( parser.get<String>( "@input" ) ) );
 if( src.empty() )
 {
 cout << "Could not open or find the image!\n" << endl;
 cout << "Usage: " << argv[0] << " <Input image>" << endl;
 return -1;
 }
 
 // Show the 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
 Mat mask;
 inRange(src, Scalar(255, 255, 255), Scalar(255, 255, 255), mask);
 src.setTo(Scalar(0, 0, 0), mask);
 
 // Show output image
 imshow("Black Background Image", src);
 
 // Create a kernel that we will use to sharpen 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
 Mat imgLaplacian;
 filter2D(src, imgLaplacian, CV_32F, kernel);
 Mat sharp;
 src.convertTo(sharp, CV_32F);
 Mat imgResult = sharp - imgLaplacian;
 
 // convert back to 8bits gray scale
 imgResult.convertTo(imgResult, CV_8UC3);
 imgLaplacian.convertTo(imgLaplacian, CV_8UC3);
 
 // imshow( "Laplace Filtered Image", imgLaplacian );
 imshow( "New Sharped Image", imgResult );
 
 // Create binary image from source image
 Mat bw;
 cvtColor(imgResult, bw, COLOR_BGR2GRAY);
 threshold(bw, bw, 40, 255, THRESH_BINARY | THRESH_OTSU);
 imshow("Binary Image", bw);
 
 // Perform the distance transform algorithm
 Mat dist;
 distanceTransform(bw, dist, DIST_L2, 3);
 
 // Normalize the distance image for range = {0.0, 1.0}
 // so we can visualize and threshold it
 normalize(dist, dist, 0, 1.0, NORM_MINMAX);
 imshow("Distance Transform Image", dist);
 
 // Threshold to obtain the peaks
 // This will be the markers for the foreground objects
 threshold(dist, dist, 0.4, 1.0, THRESH_BINARY);
 
 // Dilate a bit the dist image
 Mat kernel1 = Mat::ones(3, 3, CV_8U);
 dilate(dist, dist, kernel1);
 imshow("Peaks", dist);
 
 // Create the CV_8U version of the distance image
 // It is needed for findContours()
 Mat dist_8u;
 dist.convertTo(dist_8u, CV_8U);
 
 // Find total markers
 vector<vector<Point> > contours;
 findContours(dist_8u, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
 
 // Create the marker image for the watershed algorithm
 Mat markers = Mat::zeros(dist.size(), CV_32S);
 
 // Draw the foreground markers
 for (size_t i = 0; i < contours.size(); i++)
 {
 drawContours(markers, contours, static_cast<int>(i), Scalar(static_cast<int>(i)+1), -1);
 }
 
 // Draw the background marker
 circle(markers, Point(5,5), 3, Scalar(255), -1);
 Mat markers8u;
 markers.convertTo(markers8u, CV_8U, 10);
 imshow("Markers", markers8u);
 
 // Perform the watershed algorithm
 watershed(imgResult, markers);
 
 Mat mark;
 markers.convertTo(mark, CV_8U);
 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, 256);
 int g = theRNG().uniform(0, 256);
 int r = theRNG().uniform(0, 256);
 
 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];
 }
 }
 }
 
 // Visualize the final image
 imshow("Final Result", dst);
 
 waitKey();
 return 0;
}

解释/结果

  • 加载源图像并检查它是否加载没有任何问题,然后显示它:
 // Load the image
 CommandLineParser parser( argc, argv, "{@input | cards.png | input image}" );
 Mat src = imread( samples::findFile( parser.get<String>( "@input" ) ) );
 if( src.empty() )
 {
 cout << "Could not open or find the image!\n" << endl;
 cout << "Usage: " << argv[0] << " <Input image>" << endl;
 return -1;
 }
 
 // Show the 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
 Mat mask;
 inRange(src, Scalar(255, 255, 255), Scalar(255, 255, 255), mask);
 src.setTo(Scalar(0, 0, 0), mask);
 
 // Show output image
 imshow("Black Background Image", src);

  • 之后,我们将锐化图像,以锐化前景物体的边缘。我们将应用一个具有相当强滤波器的拉普拉斯滤波器(二阶导数的近似值):
// Create a kernel that we will use to sharpen 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
 Mat imgLaplacian;
 filter2D(src, imgLaplacian, CV_32F, kernel);
 Mat sharp;
 src.convertTo(sharp, CV_32F);
 Mat imgResult = sharp - imgLaplacian;
 
 // convert back to 8bits gray scale
 imgResult.convertTo(imgResult, CV_8UC3);
 imgLaplacian.convertTo(imgLaplacian, CV_8UC3);
 
 // imshow( "Laplace Filtered Image", imgLaplacian );
 imshow( "New Sharped Image", imgResult );

  • 现在,我们将新的锐化源图像分别转换为灰度和二进制图像:
 // Create binary image from source image
 Mat bw;
 cvtColor(imgResult, bw, COLOR_BGR2GRAY);
 threshold(bw, bw, 40, 255, THRESH_BINARY | THRESH_OTSU);
 imshow("Binary Image", bw);

  • 现在,我们已准备好在二进制图像上应用距离变换。此外,我们对输出图像进行归一化,以便能够对结果进行可视化和阈值:
 // Perform the distance transform algorithm
 Mat dist;
 distanceTransform(bw, dist, DIST_L2, 3);
 
 // Normalize the distance image for range = {0.0, 1.0}
 // so we can visualize and threshold it
 normalize(dist, dist, 0, 1.0, NORM_MINMAX);
 imshow("Distance Transform Image", dist);

  • 我们对dist图像进行阈值设置阈值,然后执行一些形态操作(即扩张),以便从上图中提取峰:
 // Threshold to obtain the peaks
 // This will be the markers for the foreground objects
 threshold(dist, dist, 0.4, 1.0, THRESH_BINARY);
 
 // Dilate a bit the dist image
 Mat kernel1 = Mat::ones(3, 3, CV_8U);
 dilate(dist, dist, kernel1);
 imshow("Peaks", dist);

  • 然后,我们借助 cv::findContours 函数为分水岭算法创建种子/标记:
 // Create the CV_8U version of the distance image
 // It is needed for findContours()
 Mat dist_8u;
 dist.convertTo(dist_8u, CV_8U);
 
 // Find total markers
 vector<vector<Point> > contours;
 findContours(dist_8u, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
 
 // Create the marker image for the watershed algorithm
 Mat markers = Mat::zeros(dist.size(), CV_32S);
 
 // Draw the foreground markers
 for (size_t i = 0; i < contours.size(); i++)
 {
 drawContours(markers, contours, static_cast<int>(i), Scalar(static_cast<int>(i)+1), -1);
 }
 
 // Draw the background marker
 circle(markers, Point(5,5), 3, Scalar(255), -1);
 Mat markers8u;
 markers.convertTo(markers8u, CV_8U, 10);
 imshow("Markers", markers8u);

  • 最后,我们可以应用分水岭算法,并将结果可视化:
 // Perform the watershed algorithm
 watershed(imgResult, markers);
 
 Mat mark;
 markers.convertTo(mark, CV_8U);
 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, 256);
 int g = theRNG().uniform(0, 256);
 int r = theRNG().uniform(0, 256);
 
 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];
 }
 }
 }
 
 // Visualize the final image
 imshow("Final Result", dst);

参考文献:

1、《Image Segmentation with Distance Transform and Watershed Algorithm》----Theodore Tsesmelis

  • 44
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愚梦者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值