Learn Image Processing With OpenCV and QT For Beginners
Learn Image Processing With OpenCV and QT For Beginners
www.robocet.com
Page 1
This tutorial is intended for very beginners of Qt and OpenCV who has a base knowledge of C/C++.
3. Knowledge of C++/C.
Qt creator
Qt creator is a development tool provided by Nokia. Using Qt creator you can create applications for Windows, Mac, Android (Qt Necessitas) and Symbian. Here we are dealing with windows only.
OpenCV
OpenCV is a set of image processing library developed by Intel. The code is available in internet. It has a lot of flexible functions to be used for image processing. Must read this book for OpenCV: http://www.cse.iitk.ac.in/users/vision/dipakmj/papers/OReilly%20Learning%20OpenCV.pdf
www.robocet.com
Page 2
First project:
The following project opens an image, displays it, converts it to grayscale and displays it in a new window. Steps. 1.Open Qt creator File->new project->Qt Widget Project->Qt Gui Application, click choose
www.robocet.com
Page 3
www.robocet.com
Page 4
-lcvaux210d\ -lcxcore210\ -lcxcore210d\ -lcxts210\ -lhighgui210\ -lhighgui210d\ -lml210\ -lml210d\ -lopencv_ffmpeg210\ -lopencv_ffmpeg210d\
www.robocet.com
Page 5
8. Type in the code here ////code for opening an image and to display it in a window ///save an image in E drive with name soccer.jpg
#include <QtGui/QApplication> #include "mainwindow.h" #include<C:\\OpenCV2.1\\include\\opencv\\highgui.h> #include<C:\\OpenCV2.1\\include\\opencv\\cv.h> IplImage* image1= cvLoadImage("E:\\soccer.jpg"); int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); cvNamedWindow( "window1", 1 ); cvShowImage( "window1", image1 ); return a.exec(); }
www.robocet.com
Page 6
Example 2:
The following is the code to capture video from webcam and to display it a window.
#include <QtGui/QApplication> #include "mainwindow.h" #include<C:\\OpenCV2.1\\include\\opencv\\highgui.h> #include<C:\\OpenCV2.1\\include\\opencv\\cv.h> IplImage* frame=0; //memory variable for image CvCapture* web_capture= cvCreateCameraCapture(0); //create a capture instinct, 0 refers to primary camera int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); cvNamedWindow("cam",0); while(1) { frame=cvQueryFrame(web_capture); // get frame from web_capture if(!frame) break; cvShowImage("cam",frame); // display image cvWaitKey(33); // wait 33 ms, assuming 30frames/sec } return a.exec(); }
www.robocet.com
Page 7
Example 3:
The following code is to open a file from hard disk and to replace the dark pixels with red. //// The max Blue-Green-Red (BGR) values of each pixel is 255 and minimum is 0. We assume pixels /////with BGR values less than 60 as dark and replace them with pure red ( B=0,G=0,R=255). /////Save a video in E drive as cr7.avi
#include <QtGui/QApplication> #include "mainwindow.h" #include<C:\\OpenCV2.1\\include\\opencv\\highgui.h> #include<C:\\OpenCV2.1\\include\\opencv\\cv.h> IplImage* vid=0; IplImage* modified=0; CvCapture* capture_file= cvCreateFileCapture("E:\\cr7.avi"); int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); cvNamedWindow("video",0); cvNamedWindow("modified",0); while(1) //loop for video { vid=cvQueryFrame(capture_file); if(!vid) break; cvShowImage("video",vid); modified=vid; int x,y; ///////////////////////////////////////loop for entire pixels for(y=0; y<modified->height; y++ ) { uchar* ptr1 = (uchar*) (modified->imageData + y * vid->widthStep); for(x=0; x<modified->width; x++ ) { if(ptr1[3*x]<60&&ptr1[3*x+1]<60&&ptr1[3*x+2]<60)//// if dark? { ////replace with red ptr1[3*x] = 0;// blue of BGRA ptr1[3*x+1] =0; //green of BGRA ptr1[3*x+2] =255; //red of BGRA } } } cvShowImage("modified",vid); cvWaitKey(33); } return a.exec(); }
www.robocet.com
Page 8