【发布时间】:2013-03-30 14:26:09
【问题描述】:
我已经被这个问题困扰了好几天了;
我创建了一个 Qt 控制台项目,将其与 OpenCV 连接,一切正常;
我创建了一个 Qt Gui 项目,添加了一个按钮并从按钮槽中复制了之前项目中的相同代码,我得到了一个 windows segFault 并且程序以代码 -1073741819 退出。
于是我使用调试器检测问题,结果发现是在使用函数cv::threshold。
我更改了它并改为使用 cv::Canny 但后来我遇到了与 cv::findContours 相同的问题!
奇怪的是,当我调用按钮的 'MainWindow::on_pushButton_clicked()' 在 Windows 的构造函数中它起作用了!!!
这里是调试器输出:
0 cv::thresh_8u(cv::Mat const&, cv::Mat&, unsigned char, unsigned char, int) C:\OpenCV2.4\OpenMinGw\install\bin\libopencv_imgproc240.dll 0 0x62b2c624 1 cv::_InputArray::getMat(int) 常量 C:\OpenCV2.4\OpenMinGw\install\bin\libopencv_core240.dll 0 0x65c1a838 2 ?? 0 0x00000000这是我得到错误的函数(我从 OpenCV 教程中得到的):
void MainWindow::on_pushButton_clicked(){
Mat src; Mat src_gray;
int thresh = 100;
RNG rng(12345);
Mat canny_output;
vector<vector<Point> > contours;
/// Load source image and convert it to gray
src = imread( "hat-10.bmp", 1 );
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
/// Detect edges using canny
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
qDebug("Ok 1");
/// Find contours
if(cv::sum(src_gray).val[0] > 0.0){
qDebug("Ok 2");
cv::findContours( src_gray, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE );
/// Draw contours
Mat drawing = Mat::zeros( src_gray.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, i, color, 2);//, 8, hierarchy, 0, Point() );
}
/// Show in a window
imshow( "Contours", drawing);
}
使用:
Windows 7 x64
使用 mingw 4.1.0 编译的 OpenCV 2.4.0
Qt Creator 2.0.0 基于 Qt 4.7.0(32 位)
编辑:
这是我的代码的较短版本:
void MainWindow::on_toolButton_clicked(){
std::vector<std::vector<cv::Point> > contours;
/// Load source image and convert it to gray
Mat src = imread( "C:/Users/poste/L3 ISIL/PFE Licence/new bmp/hat-10.bmp", 1);
// my image is already a binary one
Mat canny_output(src.size(), src.type());
Canny(src,canny_output,100,200,3);
imshow("Source", canny_output); // here image is displayed before crash
waitKey(500);
/// Find contours
findContours(canny_output, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE );
}
在控制台模式下,没有问题。从 GUI 应用程序构造函数调用时也没有问题。 只有在实际点击按钮时才会崩溃。
编辑:
我截图了![这里]http://i.stack.imgur.com/1LyeF.png
显示canny_output,表示图片已加载。
上传项目here
【问题讨论】: