Wednesday, June 29, 2016

Load and Display an Image with OpenCV in Qt

After we have shown you how to configure OpenCV library to work with Qt and display a simple text in previous tutorial, we will provide another example of how to load and display image with OpenCV library in Qt in this tutorial.

In case, you have known how to install OpenCV with Qt Creator MinGW on window. Please read it first!

Loading and displaying image plays a crucial role in Computer Vision and Pattern Recognition, as well as image processing application. In this tutorial you will learn as follows:
  • Load an image using imread
  • Display an image using imshow
Please create Qt project with either mainwidow or widget and configure the .pro file as shown in previous tutorial.

#include "mainwindow.h"
#include <QApplication>
#include "opencv2/opencv.hpp"


using namespace cv;
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
 

    // Read image
    Mat image= imread("AngkorWat.jpg", CV_LOAD_IMAGE_COLOR);
 
    
    // Create a named OpenCV window

    namedWindow("Angkor", 1);
 

    if(!image.empty())
    {
       // display the image that we have read
       imshow("Angkor", image);
 
       //wait for the user to press any key:
       waitKey(1000);

    }
    else{
      w.show();
    }
 
    return a.exec();
}

Don't forget to copy image to the current folder of your Qt application. Otherwise, you will see the error message.




No comments:

Post a Comment