making separate IplImage to hold mirrored image. this avoids flickering
[ozzloy@gmail.com/oble] / hello-world.cpp
CommitLineData
a92408ab 1//use the camera to aid the decision to sleep.
2//Copyright 2009 Daniel Watson
3/*
4 This file is part of oble.
5
6 oble is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 oble is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with oble. If not, see <http://www.gnu.org/licenses/>.
18
19 */
b7e484d5 20#include <cv.h>
21#include <highgui.h>
22#include <stdio.h>
23
0d6b136c 24// argument for cvFlip(src, dest, FLIP_TYPE)
25#define VERTICAL_FLIP 1
26
64fb0216 27CvHaarClassifierCascade* load_object_detector(const char* cascade_path)
b7e484d5 28{
21323a92 29 return (CvHaarClassifierCascade*)cvLoad(cascade_path);
b7e484d5 30}
31
9cd4e78c 32void detect_and_draw_objects(IplImage* image,
b7e484d5 33 CvHaarClassifierCascade* cascade,
9cd4e78c 34 int do_pyramids)
b7e484d5 35{
36 IplImage* small_image = image;
37 CvMemStorage* storage = cvCreateMemStorage(0);
38 CvSeq* faces;
39 int i, scale = 1;
40
41 /* if the flag is specified, down-scale the input image to get a
42 performance boost w/o loosing quality (perhaps) */
21323a92 43 if(do_pyramids)
b7e484d5 44 {
21323a92 45 small_image = cvCreateImage(cvSize(image->width/2,image->height/2),
46 IPL_DEPTH_8U, 3);
47 cvPyrDown(image, small_image, CV_GAUSSIAN_5x5);
b7e484d5 48 scale = 2;
49 }
50
51 /* use the fastest variant */
21323a92 52 faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2,
53 CV_HAAR_DO_CANNY_PRUNING);
b7e484d5 54
55 /* draw all the rectangles */
21323a92 56 for(i = 0; i < faces->total; i++)
b7e484d5 57 {
58 /* extract the rectangles only */
21323a92 59 CvRect face = *(CvRect*)cvGetSeqElem(faces, i);
60 CvPoint upperLeft = cvPoint(face.x*scale,face.y*scale);
61 CvPoint bottomRight = cvPoint((face.x+face.width)*scale,
62 (face.y+face.height)*scale);
63 cvRectangle(image, upperLeft, bottomRight, CV_RGB(255,0,0), 3);
b7e484d5 64 }
65
21323a92 66 if(small_image != image)
67 cvReleaseImage(&small_image);
68 cvReleaseMemStorage(&storage);
b7e484d5 69}
70
96972357 71// A Simple Camera Capture Framework.
21323a92 72int main(int argc, char** argv)
b7e484d5 73{
74 CvHaarClassifierCascade* cascade = load_object_detector(argv[1]);
75
21323a92 76 CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
77 if(!capture) {
78 fprintf(stderr, "ERROR: capture is NULL \n");
b7e484d5 79 getchar();
80 return -1;
81 }
82
83 // Create a window in which the captured images will be presented
21323a92 84 cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
b7e484d5 85
0d6b136c 86 IplImage* frame = cvQueryFrame(capture);
87 if(!frame) {
88 fprintf(stderr, "ERROR: frame is null...\n");
89 getchar();
90 return -1;
91 }
92 IplImage* flipped =
93 cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
b7e484d5 94 // Show the image captured from the camera in the window and repeat
21323a92 95 while(1) {
b7e484d5 96 // Get one frame
0d6b136c 97 frame = cvQueryFrame(capture);
21323a92 98 if(!frame) {
99 fprintf(stderr, "ERROR: frame is null...\n");
b7e484d5 100 getchar();
101 break;
102 }
103
21323a92 104 //flip the image so displayed right/left corresponds to physical right/left
0d6b136c 105 cvFlip(frame, flipped, VERTICAL_FLIP);
64fb0216 106
0d6b136c 107 detect_and_draw_objects(flipped, cascade, 1);
108 cvShowImage("mywindow", flipped);
b7e484d5 109 // Do not release the frame!
110
111 //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
112 //remove higher bits using AND operator
21323a92 113 if((cvWaitKey(10) & 255) == 27) break;
b7e484d5 114 }
115
116 // Release the capture device housekeeping
21323a92 117 cvReleaseCapture(&capture);
118 cvDestroyWindow("mywindow");
0d6b136c 119 cvReleaseImage(&flipped);
b7e484d5 120 return 0;
121}