以前运行成功过一次,之后就一直是闪退,跪求解答,我是菜鸟——【加System("PAUSE")还有getchar()都不行】
代码如下
#include
#include
#include
#include
using namespace std;
using namespace cv;
// 移除过小或过大的轮廓
void getSizeContours(vector> &contours)
{
int cmin = 100; // 最小轮廓长度
int cmax = 1000; // 最大轮廓长度
vector>::const_iterator itc = contours.begin();
while(itc != contours.end())
{
if((itc->size()) < cmin || (itc->size()) > cmax)
{
itc = contours.erase(itc);
}
else ++ itc;
}
}
// 计算连通区域的轮廓,即二值图像中相连像素的形状
int main()
{
Mat image = imread(" C:\Users\Administrator\Desktop\论文\123.jpg",0);
if(!image.data)
{
cout << "Fail to load image" << endl;
return 0;
}
Mat imageShold;
threshold(image, imageShold, 100, 255, THRESH_BINARY); // 必须进行二值化
vector> contours;
//CV_CHAIN_APPROX_NONE 获取每个轮廓每个像素点
findContours(imageShold, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
getSizeContours(contours);
cout << contours.size() << endl;
Mat result(image.size(), CV_8U, Scalar(255));
drawContours(result, contours, -1, Scalar(0), 2); // -1 表示所有轮廓
namedWindow("result");
imshow("result", result);
namedWindow("image");
imshow("image", image);
waitKey(0);
return 0;
}