namedWindow() causes crash in opencv 2.3.1? (Eclip

2019-01-28 15:10发布

问题:

I've finally managed to get opencv 2.3.1 to work with eclipse, mingw and 32 bit XP. I'll put up a howto when everzthing works, as I couldn't find one for this toolchain.

The trouble is that any attempt to show the results in a window causes an unhandled exception. The images save fine, and I was able to run an Sobel kernel on them so everything else seems fine. It seems to crash when namedWindow is called.

I'm including libopencv_core231, libopencv_highgui231, libopencv_imgproc231 and libopencv_legacy231 with the linker.

Here is the code:

            #include <cv.h>
            #include <highgui.h>
            #include <iostream>

            using namespace cv;

            int main(int argc, char **argv)
            {
                std::cout<<"Hello"<<std::endl;

                //Create image
                Mat lena, lenasobel;

                //Load lena image
                lena = imread("C:\\lena.jpeg");

                if(lena.data)
                std::cout<<"File has "<<lena.cols<<" rows and "<<lena.rows<<" columns. "<<lena.channels()<<" channels."<<std::endl;

                if(!lena.data)
                std::cout<<"File Not Read."<<std::endl;

                lenasobel=lena.clone();
                Sobel(lena,lenasobel,lenasobel.depth(),1,1,3);

                std::vector<int> jpg_type;
                jpg_type.push_back(100);
                jpg_type.push_back(CV_IMWRITE_JPEG_QUALITY);

                imwrite("C:\\lenaout.jpeg",lenasobel,jpg_type);

                //Save works!

                namedWindow( "lena" , CV_WINDOW_AUTOSIZE);
                imshow( "lena" ,lena);

                return 0;
            }

Does anyone have any ideas? I'm not sure what to do next!

回答1:

This is probably due to this bug that has been hanging around. I actually had to set BUILD_TYPE=Debug and disable all SSE optimizations to get OpenCV to work with Eclipse + MinGW.



回答2:

You don't need to call namedWindow. It gets created when you call imshow anyway.

Also, use lena.empty() to check if lena is properly in the memory. It is a good practice and sometimes lena.data can be a stale pointer due to refcount (say, if you turn on massive optimizations... and then you'll be in trouble identifying it, it will work fine only in debug mode...).