OpenCV4入门系列教程68:图像均值漂移分割(meanshift)

入门系列索引:OpenCV入门系列教程索引目录

上一篇:OpenCV4入门系列教程67:图像积分图算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 功能:代码 7-4 均值漂移图像分割
// 作者:朱伟 zhu1988wei@163.com
// 来源:《OpenCV图像处理编程实例》
// 博客:http://blog.csdn.net/zhuwei1988
// 更新:2016-8-1
// 说明:版权所有,引用或摘录请联系作者,并按照上面格式注明出处,谢谢。//
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
//颜色通道分离
static void MergeSeg(Mat& img
, const Scalar& colorDiff = Scalar::all(1))
{
CV_Assert(!img.empty());
RNG rng = theRNG();
// 定义掩码图像
Mat mask(img.rows + 2, img.cols + 2,
CV_8UC1, Scalar::all(0));
for (int y = 0; y < img.rows; y++)
{
for (int x = 0; x < img.cols; x++)
{
if (mask.at<uchar>(y + 1, x + 1) == 0)
{
// 颜色定义
Scalar newVal(rng(256), rng(256), rng(256));
// 泛洪合并
floodFill(img, mask, Point(x, y)
, newVal, 0, colorDiff, colorDiff);
}
}
}
}
int main(int argc, char** argv)
{
cv::Mat srcImg = imread("..\\images\\sea.jpg");
if (srcImg.empty())
return -1;
// 参数设定
int spatialRad = 20;
int colorRad = 20;
int maxPyrLevel = 6;
cv::Mat resImg;
// 均值漂移分割
pyrMeanShiftFiltering(srcImg, resImg,
spatialRad, colorRad, maxPyrLevel);
// 颜色通道分离合并
MergeSeg(resImg, Scalar::all(2));
cv::imshow("src", srcImg);
cv::imshow("resImg", resImg);
cv::waitKey();
return 0;
}

动态设置参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//--------------------------------------【程序说明】-------------------------------------------
// 程序说明:《OpenCV3编程入门》OpenCV2版书本附赠示例程序09
// 程序描述:MeanShift图像分割
// 2014年11月 Revised by @浅墨_毛星云
//------------------------------------------------------------------------------------------------

//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;

//--------------------------------------【help( )函数】--------------------------------------
// 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
static void help() {
cout << "\n\t此程序演示了OpenCV中MeanShift图像分割的使用。\n"
<< "\n\t程序运行后我们可以通过3个滑动条调节分割效果。调节滑动条后可能会有些许卡顿,请耐心"
"等待\n"
<< "\n\t3个滑动条代表的参数分别为空间窗的半径 "
"(spatialRad)、色彩窗的半径(colorRad)、最大图像金字塔级别(maxPyrLevel)\n"
<< endl;
}

// This colors the segmentations
static void floodFillPostprocess(Mat &img, const Scalar &colorDiff = Scalar::all(1)) {
CV_Assert(!img.empty());
RNG rng = theRNG();
Mat mask(img.rows + 2, img.cols + 2, CV_8UC1, Scalar::all(0));
for (int y = 0; y < img.rows; y++) {
for (int x = 0; x < img.cols; x++) {
if (mask.at<uchar>(y + 1, x + 1) == 0) {
Scalar newVal(rng(256), rng(256), rng(256));
floodFill(img, mask, Point(x, y), newVal, 0, colorDiff, colorDiff);
}
}
}
}

string winName = "meanshift";
int spatialRad, colorRad, maxPyrLevel;
Mat img, res;

static void meanShiftSegmentation(int, void *) {
cout << "spatialRad=" << spatialRad << "; "
<< "colorRad=" << colorRad << "; "
<< "maxPyrLevel=" << maxPyrLevel << endl;
pyrMeanShiftFiltering(img, res, spatialRad, colorRad, maxPyrLevel);
floodFillPostprocess(res, Scalar::all(2));
imshow(winName, res);
}

//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main(int argc, char **argv) {

help();

img = imread("1.jpg");
if (img.empty())
return -1;
imshow("原始图", img);
spatialRad = 10;
colorRad = 10;
maxPyrLevel = 1;

namedWindow(winName, WINDOW_AUTOSIZE);

createTrackbar("spatialRad", winName, &spatialRad, 80, meanShiftSegmentation);
createTrackbar("colorRad", winName, &colorRad, 60, meanShiftSegmentation);
createTrackbar("maxPyrLevel", winName, &maxPyrLevel, 5, meanShiftSegmentation);

meanShiftSegmentation(0, 0);
waitKey();
return 0;
}

下一篇:OpenCV4入门系列教程69:文件之XML读写