代码如下:
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
//一、将源图片转成单通道的灰阶图片
Mat changeSourceImg(Mat img){
Mat great(img.size(), CV_8U);
cvtColor(img, great, CV_BGR2GRAY);
return great;
}
//二、对灰度图像进行阈值操作得到二值图像
void thresholdOperation(Mat img){
threshold(img, img, 100, 255, CV_THRESH_OTSU | CV_THRESH_BINARY_INV);
}
int main(){
system("color 5E");//改变cmd控制台的颜色
//一、得到灰度图
Mat src = imread("pic1.png", CV_LOAD_IMAGE_COLOR);
namedWindow("原图");
imshow("原图",src);
if (!src.data) return false;
Mat great = changeSourceImg(src);
namedWindow("灰度图");
imshow("灰度图",great);
//二、对灰度图像进行阈值操作得到二值图像
thresholdOperation(great);
namedWindow("二值图像");
imshow("二值图像", great);
//三、轮廓检测(只获取最外层的)
vector> contours;
vector hierarchy;//空间
findContours(great, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
namedWindow("轮廓检测");
imshow("轮廓检测",great);
cout << "共有外围轮廓:" << hierarchy.size() << "条" << endl;
//输出一些帮助信息
cout << "\t嗯。运行成功,请观察图像效果." << endl << "按下“q”键时,程序退出~!";
//轮询获取按键信息,若下q键,程序退出
while (char(waitKey(1)) != 'q') {}
return 0;
}
错误信息:
HEAP[ocr.exe]: Invalid address specified to RtlFreeHeap( 004A0000, 020DBBF8 )
ocr.exe 已触发了一个断点。