Preface
Learning opencv has been around for a few days. I found that there are still many beginners in the group looking for information like me at the beginning, so I will share my study notes here, we hope that our friends who are interested in learning opencv for Computer Vision activities will be able to take less detours.
After reading materials from many aspects, I learned that the QT development platform supports opencv well, but there is very little information on the Internet, most of the graphic interaction designs that can be found are based on the data structure iplimage before opencv2.0, and opencv is now officially updated to version 2.4.9. Accidentally found a good book recently got a good book "opencv 2 computer vision application programming cookbook", download link for http://ishare.iask.sina.com.cn/f/20485520.html? Retcode = 0, published in May 2011. The book is based on opencv2.2 and adopts a new data structure. The book now has a Chinese version of opencv2 Computer Vision Programming Manual, which was published by the Science Publishing House in July 2013. I strongly recommend that you do not use the data structure of the old version by using C ++, which affects the development efficiency. As for the two widely used books, learning opencv and opencv tutorial-basics, which are most familiar to everyone, my opinion is far from satisfying the learning of the new version of opencv. In earlier versions, the inclusion format of opencv header files is:
#include "cv.h"#include "highgui.h"
Header files of Versions later than 2.2 include:
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>
To maintain compatibility with earlier versions, these header files can still be used, but we recommend that you use a new inclusion method.
Open and show pictures
In QT, signals and slots are the most important mechanism. Here we can create buttons, menus, and toolbar as the carrier for transmitting signals, use a slot function to receive the open action.
QPushButton OpenImageButton = new QPushButton( tr("Find Image") );OpenImageButton->setDefault( false );OpenImageButton->setEnabled( true );Slot function: You can use the QT class qfiledialog to open a file.
void qttest1::on_OpenImage_clicked(){ QString fileName = QFileDialog::getOpenFileName( this, tr( "Open Image" ), ".", tr("Image Files(*.png*.jpg*.jpeg*.bmp)")); image = cv::imread( fileName.toLocal8Bit().data()); cv::imshow("Source Image", image);}
The above function has enabled opening and displaying images. Add a button and define a function to operate the image.
void qttest1::on_Process_clicked(){ cv::flip(image, image, 1); cv::namedWindow("Output Image"); cv::imshow("Output Image", image);}
After all these work is done for beginners, do not forget to connect signals and slots.
connect(OpenImageButton,SIGNAL(clicked()),SLOT(on_OpenImage_clicked()));connect(ProcessButton,SIGNAL(clicked()),SLOT(on_Process_clicked()));
After the work is done, you can open and display the picture!
Of course, some may encounter the following situation: Remember to release the object in your destructor,
Cvdestroyallwindows ();
It is reasonable to say that opencv2 Automatically releases the object. The landlord hasn't figured it out yet. Please leave a message clearly. Thank you very much.
A little improvement
A window pops up and the display is always abrupt. There are many ways to display images in the following QT form. Different display methods have problems in efficiency and other aspects, here, we use qimage to convert to qpixmap, and then use qlabel: setpixmap () for display. Another common method is to directly draw in paintevent (). The specific difference is not explored.
Let's write a small function to implement:
void qttest1::displayMat(cv::Mat image){ cv::Mat rgb; QImage img; if(image.channels()==3) { //cvt Mat BGR 2 QImage RGB cv::cvtColor(image,rgb,CV_BGR2RGB); img =QImage((const unsigned char*)(rgb.data), rgb.cols,rgb.rows, rgb.cols*rgb.channels(), QImage::Format_RGB888); } else { img =QImage((const unsigned char*)(image.data), image.cols,image.rows, image.cols*image.channels(), QImage::Format_RGB888); } imagelabel->setPixmap(QPixmap::fromImage(img)); imagelabel->resize(imagelabel->pixmap()->size());}
The final result is as follows: is it concise and clear?