opencv开发笔记(十二):查找并绘制轮廓

// 查找并绘制轮廓.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
using namespace cv;
using namespace std;
Mat src,gray;
int nth = 80;
int nthmax = 255;
RNG rng(12345);
Mat output;
vector >counts;
vectorvhi;
void ontresh(int, void*);
int main(int agrc,char** argv)
{
src = imread("E:\\Pictures\\1.jpg");
cvtColor(src, gray, CV_BGR2GRAY);  //转换为灰度图并降噪
blur(gray, gray, Size(3, 3));
imshow("原始图", src);
createTrackbar("canny阈值","原始图",&nth,nthmax,ontresh);  
ontresh(0,0);
waitKey(0);
    return 0;
}
void ontresh(int,void*)
{
Canny(gray,output,nth,nth*2,3);//边缘检测
findContours(output,counts,vhi,RETR_TREE,CHAIN_APPROX_SIMPLE,Point(0,0));  //查找轮廓 
Mat draw = Mat::zeros(output.size(),CV_8UC3);    //绘制轮廓
for (int i = 0; i < counts.size();i++) {
Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(draw,counts,i,color,2,8,vhi,0,Point());
}
imshow("xiaoguo", draw);
}

你可能感兴趣的:(opencv编程开发笔记)