0% found this document useful (0 votes)
1K views8 pages

Learn Image Processing With OpenCV and QT For Beginners

This document is a tutorial for using OpenCV with Qt to process images and video. It contains 3 examples: 1. Opening an image, displaying it, converting it to grayscale, and displaying the result in a new window. 2. Capturing video from a webcam, displaying the frames in a window, and processing the video in real-time. 3. Opening a video file, replacing dark pixels with red to highlight them, and displaying the original and modified video side-by-side.

Uploaded by

Hameem C Hamza
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
1K views8 pages

Learn Image Processing With OpenCV and QT For Beginners

This document is a tutorial for using OpenCV with Qt to process images and video. It contains 3 examples: 1. Opening an image, displaying it, converting it to grayscale, and displaying the result in a new window. 2. Capturing video from a webcam, displaying the frames in a window, and processing the video in real-time. 3. Opening a video file, replacing dark pixels with red to highlight them, and displaying the original and modified video side-by-side.

Uploaded by

Hameem C Hamza
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Learn OpenCV with Qt -1

www.robocet.com College of Engineering Trivandrum

Prepared by, Hameem.C.Hamza ROBOCET member

www.robocet.com

Page 1

This tutorial is intended for very beginners of Qt and OpenCV who has a base knowledge of C/C++.

What you need?


1. Qt creator for c++. Here Qt Creator 2.4.1 is used. Its free to download from internet. 2. OpenCV 2.1. Its free to download from internet.

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

Lets start. 1.Install Qt Creator 2.Install OpenCV2.1

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

2. Select a file name(here tutorial_1), click next

www.robocet.com

Page 3

3.Select desktop, click next, finish

4. Double click on tutorial_1.pro 5. The following screen appears

6. Now type in the following code after last line


INCLUDE += C:\\OpenCV2.1\\include\\opencv LIBS += -LC:\\OpenCV2.1\\lib\ -lcv210\ -lcv210d\ -lcvaux210\

www.robocet.com

Page 4

-lcvaux210d\ -lcxcore210\ -lcxcore210d\ -lcxts210\ -lhighgui210\ -lhighgui210d\ -lml210\ -lml210d\ -lopencv_ffmpeg210\ -lopencv_ffmpeg210d\

It should look like-

7. Now double click on sources-> main.cpp

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

You might also like