0

I am new using Qt and OpenCV. I am trying to read an image in my HD and show it. It is not a specific image, the program could read any image that user select. My code:

QString Imagename = QFileDialog::getOpenFileName(
                this,
                tr("Open Images"),
                "C://",
                tr("Tiff Files (*.tif);; Raw file (*.raw)"));

 if ( Imagename.isNull())
    {
         QMessageBox::warning(this,"Error!","Image not valid!");
    }


    cv::Mat src(filename);

Mat configuration is: Mat imread(const string& filename, int flags=1 )

How can I solve this?

2 Answers2

3

cv::Mat doesn't have a contructor that accepts a string. Use imread instead. Since imread accepts std::string, not QString, just do:

cv::Mat yourImage = cv::imread(filename.toStdString());
Miki
  • 40,887
  • 13
  • 123
  • 202
  • same error at: namedWindow("Original Image", CV_WINDOW_AUTOSIZE); imshow("Original Image", srcimage); – Renato Feijo Jul 07 '15 at 18:04
  • 1
    @RenatoFeijo do you link against opencv_highgui[xxx]? And #include ? – Miki Jul 07 '15 at 18:06
  • yes. I link: #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/core/core.hpp" #include "opencv2/opencv.hpp" #include #include #include – Renato Feijo Jul 07 '15 at 18:27
  • @RenatoFeijo but seems a linking error, am I right? Are you linking to opencv_core[xxx], etc, and *opencv_highgui[xxx]*? – Miki Jul 07 '15 at 18:33
  • in PRO file, I use: INCLUDEPATH += \\C++\\opencv\\opencv\\build\\include LIBS += -LC:\\C++\\opencv\\opencv\\build\x64\\vc12\\lib \ -lopencv_highgui2410d \ -lopencv_imgproc2410d \ -lopencv_core2410d – Renato Feijo Jul 07 '15 at 18:44
  • Does the code compile and link without all imread, imshow code, but just with a Mat1b a(5,5,uchar(0)); ? – Miki Jul 07 '15 at 18:46
  • Also, have you Qt compiled with msvc12 or mingw? – Miki Jul 07 '15 at 18:54
  • problems occur with all imread, imshow, namedWindow. I am using mingw. – Renato Feijo Jul 07 '15 at 19:08
  • you are linking with libraries compiled with msvc.. that won't work! Either recompile opencv with mingw, or set Qt to use msvc... – Miki Jul 07 '15 at 19:15
  • http://stackoverflow.com/questions/20013903/opencv-on-qt-imread-namedwindow-imshow-does-not-working – Miki Jul 07 '15 at 19:17
0

You cannot use cv::Mat variable as you used.To solve this problem you should use "imread" function.I think following code will help you about this issue. You have to include following libraries.

    #include<QFileDialog>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>

    int main (){
    // Gets file name with QFileDialog
    QString file_name=QFileDialog::getOpenFileName(this,"Open Image File","C://","Image File (*.jpg *.tiff *.png *.bmp)"); 

    // Read image with Color Image Parameter and store on image variable which type is cv::Mat
    // You should convert file name from QString to StdString to use in imread function
    cv::Mat image = cv::imread(file_name.toStdString(),CV_LOAD_IMAGE_COLOR); 

   if(!image.data){   // Checks whether the image was read successfully 
      qDebug()<< "Could not open or find the image";  
      return -1;
                  }

    cv::namedWindow("Original Image",WINDOW_AUTOSIZE); // Creates a window which will display image 
    cv::imshow("Original Image",image);     // Shows image on created window

    cv::waitKey(0);    // Waits for a keystroke in the window
    return 0;  // if you created console app in qt you should use return a.exec() instead of this.
}
O.AYYILDIZ
  • 86
  • 8