基于区域分割是将图像按照相似性准则分成不同的区域,主要包括区域增长,区域分裂合并和分水岭等几种类型。

OpenCV提供了 分水岭算法函数watershed 和 GrabCut算法函数grabCut,可以快速实现图像的分割。本文是讲述分水岭算法watershed的实例,代码为OpenCV官方文档里。

图像分割知识请移步:Opencv(11)——分水岭算法实例_僚机武士的博客-_opencv分水岭算法 官方例子

/**
 * @brief Sample code showing how to segment overlapping objects using Laplacian filtering, in addition to Watershed and Distance Transformation
 * @author OpenCV Team
 */
//示例代码显示了如何使用拉普拉斯滤波以及分水岭和距离变换分割重叠对象
#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_image]
	// 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);
	//! [load_image]

	//! [black_bg]
	// 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);
	//mask已经提取出背景了 即原图中白色区域
	imshow("mask", mask);
	//mask为非0值的区域(背景),在原图中置成黑色,完成去除背景操作
	src.setTo(Scalar(0, 0, 0), mask);
	 
	 
	// Show output image
	imshow("Black Background Image", src);
	//! [black_bg]


	//! [sharp]
	// 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
	//二阶导数的近似值,一个相当强的核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,所以卷积为负的像素值就会截断
	Mat imgLaplacian;
	filter2D(src, imgLaplacian, CV_32F, kernel);
	//原图与内核做卷积运算,运算结果imgLaplacian的类型为CV_32F
	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);
	//! [sharp]

	//! [bin]
	// 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);
	//! [bin]

	//! [dist]
	// Perform the distance transform algorithm 
	// 执行距离变换算法
	Mat dist;
	distanceTransform(bw, dist, DIST_L2, 3);
	 
	// Normalize the distance image for range = {0.0, 1.0} 规范化距离图像,使其范围={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]

	//! [peaks]
	// Threshold to obtain the peaks  获得峰值的阈值
	// This will be the markers for the foreground objects  这(峰值)将是前景对象的标记
	threshold(dist, dist, 0.4, 1.0, THRESH_BINARY);
	imshow("threshold Distance Transform Image", dist);

	// Dilate a bit the dist image
	Mat kernel1 = Mat::ones(3, 3, CV_8U);
	dilate(dist, dist, kernel1);
	//morphologyEx(dist,dist,MORPH_CLOSE,kernel1); 自己添加的语句测试,发现效果没啥变化,但这样可以避免峰值出现粘连
	imshow("Peaks", dist);
	//! [peaks]

	//! [seeds]
	// Create the CV_8U version of the distance image 
	//创建距离图像的CV_8U版本
	// It is needed for findContours() 查找轮廓需要距离图像的CV_8U版本
	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);
		//-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);
	//! [seeds]

	//! [watershed]
	// Perform the watershed algorithm 执行分水岭算法
	watershed(imgResult, markers);


	Mat mark;
	markers.convertTo(mark, CV_8U);
	//漫水填充效果图,背景被填充,前景置0
	imshow("mark1", mark);
	bitwise_not(mark, mark);//反转

 
	// uncomment this if you want to see how the mark image looks like at that point2
	//如果您想查看标记图像在该点上的外观,请取消对此的注释
	//imshow("Markers_v2", mark); 

	// 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);
	//! [watershed]

	waitKey();
	return 0;
}