#include<opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
#define WINDOW_NAME "【程序窗口1】"
Mat g_maskImage, g_srcImage;
Point prevPt(-1, -1);
static void ShowHelpText();
static void on_Mouse(int event, int x, int y, int flags, void *);
int main()
{
system("color 1F");
ShowHelpText();
g_srcImage = imread("E://Pec//shan.jpg");
imshow(WINDOW_NAME, g_srcImage);
Mat srcImage, grayImage;
g_srcImage.copyTo(srcImage);
//从RBG和BGR颜色空间转换到灰度空间
cvtColor(g_srcImage, g_maskImage, COLOR_BGR2GRAY);
//imshow("g_maskImage", g_maskImage);
//从灰度空间转换到RGB和BGR颜色空间
cvtColor(g_maskImage, grayImage, COLOR_GRAY2BGR);
//imshow("grayImage", grayImage);
g_maskImage = Scalar::all(0);
//设置鼠标回调函数
setMouseCallback(WINDOW_NAME, on_Mouse, 0);
while (1)
{
int c = waitKey(0);
if ((char)c == 27)
break;
//按键2按下时,恢复源图
if ((char)c == 2)
{
g_maskImage = Scalar::all(0);
srcImage.copyTo(g_srcImage);
imshow("image", g_srcImage);
}
//若检测到按键为1或者空格,则进行处理
if ((char)c == '1' || (char)c == ' ')
{
int i, j, compCount = 0;
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
//寻找轮廓
findContours(g_maskImage, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//若是轮廓为空
if (contours.empty())
continue;
//复制掩膜
Mat maskImage(g_maskImage.size(), CV_32S);
maskImage = Scalar::all(0);
//循环绘制轮廓
for (int index = 0; index >= 0; index = hierarchy[index][0], compCount++)
drawContours(maskImage, contours, index, Scalar::all(compCount + 1), -1, 8, hierarchy, INT_MAX);
//compCOunt为0时
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 dTime = (double)getTickCount();
watershed(srcImage, maskImage);
dTime = (double)getTickCount() - dTime;
printf("\t处理时间 =%gms\n", dTime*100. / getTickFrequency());
//双层循环,将分水岭图像遍历存入watershedImage中
Mat watershedImage(maskImage.size(), CV_8UC3);
for(i=0;i<maskImage.rows;i++)
for (j = 0; j < maskImage.cols; j++)
{
int index = maskImage.at<int>(i, j);
if (index == -1)
watershedImage.at<Vec3b>(i, j) = Vec3b(255, 255, 255);
else if (index <= 0 || index > compCount)
watershedImage.at<Vec3b>(i, j) = Vec3b(0, 0, 0);
else
watershedImage.at<Vec3b>(i, j) = colorTab[index-1];
}
//混合灰度图和分水岭效果图并显示最终的窗口
watershedImage = watershedImage * 0.5 + grayImage * 0.5;
imshow("watershed tandsform", watershedImage);
}
}
return 0;
}
static void on_Mouse(int event, int x, int y, int flags, void *)
{
//处理鼠标步骤窗口中的情况
if (x < 0 || x >= g_srcImage.cols || y < 0 || y >= g_srcImage.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(g_maskImage, prevPt, pt, Scalar::all(255), 5, 8, 0);
line(g_srcImage, prevPt, pt, Scalar::all(255), 5, 8, 0);
prevPt = pt;
imshow(WINDOW_NAME, g_srcImage);
}
}
static void ShowHelpText()
{
cout << "\n\t欢迎来到【分水岭算法】示例程序" << endl;
cout << "\t\t请先用鼠标在窗口标记出大致的区域" << endl;
cout << "\t\t然后按键【1】或者【空格】启动算法" << endl;
cout << "\t\t按键操作如下:" << endl;
cout << "\t\t\t按下按键【1】或者【空格】--运行分水岭分割算法" << endl;
cout << "\t\t\t按下按键【2】--恢复原始图片" << endl;
cout << "\t\t\t按下按键【ESC】--退出程序" << endl;
}