#include<iostream>
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>

int main()
{
//打开要识别字符的图像
IplImage *image = cvLoadImage("../../hello.png");
if (image == NULL)
{
std::cout << "错误:无法打开该图像文件!" << std::endl;
}
//转换到灰度图像
IplImage *img_gray = cvCreateImage(cvGetSize(image), image->depth, 1);
cvCvtColor(image, img_gray, CV_BGR2GRAY);
//二值化
IplImage *img_value = cvCreateImage(cvGetSize(img_gray), image->depth, 1);
cvThreshold(img_gray, img_value, 100, 255, CV_THRESH_BINARY_INV);
//寻找轮廓
CvMemStorage *pStorage = cvCreateMemStorage(0);
CvSeq *pConInner = NULL;
int num = cvFindContours(img_value, pStorage, &pConInner, sizeof(CvContour),
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
//根据轮廓坐标使用方框标出来
for (int i = 0; i < num; i++)
{

CvRect rc = cvBoundingRect(pConInner);
cvDrawRect(image, cvPoint(rc.x, rc.y), cvPoint(rc.x + rc.width, rc.y + rc.height), CV_RGB(255, 0, 0));
pConInner = pConInner->h_next;
}
//显示图像
cvNamedWindow("image", 0);
cvNamedWindow("image_gray", 0);
cvNamedWindow("img_value", 0);
cvShowImage("image", image);
cvShowImage("image_gray", img_gray);
cvShowImage("img_value", img_value);
cvWaitKey(0);
}

opencv轮廓提取_二值化