#include<opencv2\opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
using namespace std;
Mat src, temp, dst;
int threshold_value = 100;
int threshold_max = 255;
char *output = "output image";
RNG rng;
void Coutours_Demo(int, void*);
int main()
{
//加载图像
src = imread("E:\\vs2015\\opencvstudy\\27police.jpg");
if (!src.data)
{
cout << "could not load image!" << endl;
return -1;
}
imshow("inputImage", src);
cvtColor(src, src, CV_BGR2GRAY);
imshow("grayImage", src);
namedWindow(output, CV_WINDOW_AUTOSIZE);
createTrackbar("Size", output, &threshold_value, threshold_max, Coutours_Demo);
Coutours_Demo(0, 0);
waitKey(0);
return 0;
}
void Coutours_Demo(int, void*)
{
Mat canny_output;
Canny(src, canny_output, threshold_value, threshold_value * 2, 3, false);
vector<vector<Point>> contours; //数组..全部发现的轮廓对象
vector<Vec4i> hierachy;//图的拓步结构
findContours(canny_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
dst = Mat::zeros(src.size(), src.type());
RNG rng(12345);
for (size_t i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(dst, contours, i, color, 2, 8, hierachy, 0, Point(0, 0));
}
imshow(output, dst);
}