怎解决闪退问题,显示运行成功,可是那个黑框框一出来就自动退出了。【急急急】

以前运行成功过一次,之后就一直是闪退,跪求解答,我是菜鸟——【加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;

}

按F5,或者加System("PAUSE")

在return 0上一行加getchar(),或者直接按Ctrl+F5来运行程序

crtl+F5. 或者在命令行下运行。打开cmd,把目录切换到你的可执行程序下,然后运行。

添加system(“pause”);
或者是getchar();
就可以暂停程序了

#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);

scanf("%*c");

return 0;

}

system(“pause”);我就是用这个的

你代码原本就有问题,没执行完毕就退出了。和暂停没关系

因为程序的退出点(代码行return X;)不止一个。
在所有的return前面加上system("pause")或者是getchar();是一定可以中断程序的。
C++里面,这个规则是最常用的。