本文分享内容来自图书《学习OpenCV 4:基于Python的算法实战》,该书内容如下:
第1章 OpenCV快速入门;
第2章 图像读写模块imgcodecs;
第3章 核心库模块core;
第4章 图像处理模块imgproc(一);
第5章 图像处理模块imgproc(二);
第6章 可视化模块highgui;
第7章 视频处理模块videoio;
第8章 视频分析模块video;
第9章 照片处理模块photo;
第10章 2D特征模块features2d;
第11章 相机标定与三维重建模块calib3d;
第12章 传统目标检测模块objdetect;
第13章 机器学习模块ml;
第14章 深度神经网络模块dnn
欢迎关注图书《深度学习计算机视觉实战》与《学习OpenCV4:基于Python的算法实战》。
本节讲述分水岭算法,GrabCut算法和漫水填充算法将在下面两次分享中介绍,欢迎持续关注。
在没有背景模板可用的情况下,分水岭算法首先计算强度图像的梯度(如查找轮廓),形成的线条组成了山脉或者岭,没有纹理的地方形成盆地或者山谷,然后从指定的点向盆地灌水,当图像被灌满时,所有有标记的区域就被分割开了,这就是分水岭算法的分割思想。
OpenCV中提供了分水岭算法的函数watershed,函数定义如下:
markers = watershed(image, markers)
参数说明如下:
image,输入图像,需传入8-bit 3通道图像;
markers,和输入图像大小相同的标记图像(返回值)。
本案例使用的案例代码来源于OpenCV源码中的样例,位置为“samples/cpp/watershed.cpp”,为了便于理解,对源码做了一些调整如增加中文注释。案例中使用鼠标标记分割目标(标记点即为注水点),后使用分水岭算法进行图像分割,案例代码如下:
#include <opencv2/core/utility.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <cstdio>
#include <iostream>
using namespace cv;
using namespace std;
//帮助说明
static void help(char** argv)
{
//第一个参数传入的是输入图像,默认是fruits.jpg
cout << "\nThis program demonstrates the famous watershed segmentation algorithm in OpenCV: watershed()\n"
"Usage:\n" << argv[0] << " [image_name -- default is fruits.jpg]\n" << endl;
//操作键:鼠标左键拖动选取前景;ESC退出操作;r图像复原;w或者空格键执行操作
cout << "Hot keys: \n"
"\tESC - quit the program\n"
"\tr - restore the original image\n"
"\tw or SPACE - run watershed segmentation algorithm\n"
"\t\t(before running it, *roughly* mark the areas to segment on the image)\n"
"\t\t(before that, roughly outline several markers on the image)\n";
}
Mat markerMask, img;
Point prevPt(-1, -1);
//鼠标事件
static void onMouse(int event, int x, int y, int flags, void*)
{
//图像有问题或者鼠标当前点在图像外则返回不处理
if (x < 0 || x >= img.cols || y < 0 || y >= img.rows)
return;
//鼠标左键抬起
if (event == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON))
prevPt = Point(-1, -1);
//鼠标左键按下
else if (event == EVENT_LBUTTONDOWN)
prevPt = Point(x, y);
//鼠标移动
else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON))
{
Point pt(x, y);
if (prevPt.x < 0)
prevPt = pt;
//绘制标记线
line(markerMask, prevPt, pt, Scalar::all(255), 5, 8, 0);
line(img, prevPt, pt, Scalar::all(255), 5, 8, 0);
prevPt = pt;
imshow("image", img);
}
}
//执行主函数,程序入口
int main(int argc, char** argv)
{
//参数解析
cv::CommandLineParser parser(argc, argv, "{help h | | }{ @input | fruits.jpg | }");
if (parser.has("help"))
{
help(argv);
return 0;
}
//获取输入图像文件名
string filename = samples::findFile(parser.get<string>("@input"));
Mat img0 = imread(filename, 1), imgGray;
if (img0.empty())
{
cout << "Couldn't open image ";
help(argv);
return 0;
}
//创建图像窗口
namedWindow("image", 1);
img0.copyTo(img);
cvtColor(img, markerMask, COLOR_BGR2GRAY);
cvtColor(markerMask, imgGray, COLOR_GRAY2BGR);
markerMask = Scalar::all(0);
imshow("image", img);
//鼠标事件回调函数
setMouseCallback("image", onMouse, 0);
for (;;)
{
char c = (char)waitKey(0);
//ESC键退出
if (c == 27)
break;
//r键图像还原
if (c == 'r')
{
markerMask = Scalar::all(0);
img0.copyTo(img);
imshow("image", img);
}
//w或者空格键执行算法
if (c == 'w' || c == ' ')
{
int i, j, compCount = 0;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
//查找轮廓
findContours(markerMask, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
if (contours.empty())
continue;
Mat markers(markerMask.size(), CV_32S);
markers = Scalar::all(0);
int idx = 0;
//绘制轮廓
for (; idx >= 0; idx = hierarchy[idx][0], compCount++)
drawContours(markers, contours, idx, Scalar::all(compCount + 100), -1, 8, hierarchy, INT_MAX);
if (compCount == 0)
continue;
vector<Vec3b> colorTab;
for (i = 0; i < compCount; i++)
{
int b = theRNG().uniform(0, 255);
int g = theRNG().uniform(0, 255);
int r = theRNG().uniform(0, 255);
colorTab.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
}
double t = (double)getTickCount();
imwrite("markers.jpg", markers);
//执行分水岭算法
watershed(img0, markers);
t = (double)getTickCount() - t;
//打印处理时间
printf("execution time = %gms\n", t * 1000. / getTickFrequency());
Mat wshed(markers.size(), CV_8UC3);
cout << wshed.at<int>(0, 0) << endl;
// 绘制watershed图像,对不同分割区域着色
for (i = 0; i < markers.rows; i++) {
for (j = 0; j < markers.cols; j++)
{
int index = markers.at<int>(i, j);
if (index == -1)
wshed.at<Vec3b>(i, j) = Vec3b(255, 255, 255);
else if (index <= 0 || index > compCount)
wshed.at<Vec3b>(i, j) = Vec3b(0, 0, 0);
else
wshed.at<Vec3b>(i, j) = colorTab[index - 1];
}
}
//显示分水岭算法的分割线
imshow("watershed", wshed);
wshed = wshed * 0.5 + imgGray * 0.5;
//显示分水岭算法的结果
imshow("watershed_result", wshed);
}
}
return 0;
}
本案例使用的输入图像如图5.26所示。
图5.26
在图像中用鼠标左键标识待分割的目标,如图5.27所示。
图5.27
按下w或者空格键,执行分割算法,分割结果如图5.28所示。
图5.28
分割结果在输入图像中绘制的结果如图5.29所示。
图5.29