Search code examples
c++opencvinitqmake

OpenCV compile Ubuntu


i have succesfully installed OpenCv & Qt in Ubuntu..i can debug and run some sample codes but when i create QtConsole Application i cant build it... here is my sample code to randomly white some pixels in image

opencv2/core/core.hpp
opencv2/highgui/highgui.hpp
void salt(cv::Mat &image, int n)
{
    for(int k=0; k<n; k++)
    {
        int i=rand()%image.cols;
        int j=rand()%image.rows;

        if(image.channels() == 1)
        {
            image.at<uchar>(j,i)=255;
        }
        else if(image.channels() == 3)
        {
            image.at<cv::Vec3b>(j,i)[0]=255;
            image.at<cv::Vec3b>(j,i)[1]=255;
            image.at<cv::Vec3b>(j,i)[2]=255;
        }
    }
}

int main()
{
    cv::Mat resim = cv::imread("boldt.jpg");
    salt(resim,3000);
    cv::namedWindow("Cerceve");
    cv::imshow("Cerceve",resim);
}

I have added my libraires in .pro file like

INCLUDEPATH += /usr/local/include/opencv2

LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_features2d -lopencv_calib3d

when I try to run program from Terminal it says :

init done

opengl support available

and nothing happens.. there is no error while debugging


Solution

  • Add a cv::waitKey(..) at the end of main()

    cv::imshow("Cerceve",resim);
    cv::waitKey(0);
    return 0;
    

    Calling cv::waitKey(param) is crucial because it is what processes the event loop in the window you have opened (apart from returning the key the user pressed during the wait time). Here param is the number of milliseconds to wait before the function returns. Use 0 for infinite wait till some key is pressed.