前两篇博文分别介绍了图像的边缘检测和轮廓检测,本文接着介绍图像的轮廓检测和轮廓外接矩形:
一、代码部分:
// extract_contours.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//load src image
string img_name="..\\image_norm\\71253.jpg";
Mat image=imread(img_name);
imshow("src_image",image);
cvWaitKey(0);
//convert into gray image
Mat gray(image.size(),CV_8U);
cvtColor(image,gray,CV_BGR2GRAY);
imshow("gray",gray);
cvWaitKey(0);
//convert into bin image
threshold(gray,gray,128,255,THRESH_BINARY);//转换成2值图像
imshow("binary",gray);
cvWaitKey(0);
// Detecting contours
vector> contours; //定义轮廓集合
vector