I have video, when the program is run the video's first frame is taken as an image and the user is allowed to draw a rectangle on the image, after the rectangle is drawn, the user must right click on the image to confirm the rectangle. When the mouse the right-clicked the image disappears and the video starts to play with the drawn rectangle on it.
I am able to draw the rectangle perfectly but I can't set that rectangle as ROI.
What I want to do is to set that rectangle as Region of Interest (ROI) to do some image processing on that ROI. I am unable to set the rectangle which I draw as ROI.
I am using OpenCV with Visual Studio 2010. Later on I will try to integrate this program in QT creator.
Any help would be appreciated.
Thanks in advance.
My full code is as follows:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include<opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>
#include <opencv\cvaux.h>
using namespace cv;
using namespace std;
void my_mouse_callback( int event, int x, int y, int flags, void* param );
bool destroy=false;
CvRect box;
IplImage* image;
IplImage* frame2;
bool drawing_box = false;
void draw_box( IplImage* img, CvRect rect)
{
cvRectangle( img, cvPoint(box.x, box.y), cvPoint(box.x+box.width,box.y+box.height),
cvScalar(0,0,255) ,2);
CvRect rect2=cvRect(box.x,box.y,box.width,box.height);
//cvSetImageROI(image, rect2); //here I wanted to set the drawn rect as ROI
}
// Implement mouse callback
void my_mouse_callback( int event, int x, int y, int flags, void* param ){
IplImage* image = (IplImage*) param;
switch( event ){
case CV_EVENT_MOUSEMOVE:
if( drawing_box )
{
box.width = x-box.x;
box.height = y-box.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
box = cvRect( x, y, 0, 0 );
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if( box.width < 0 )
{
box.x += box.width;
box.width *= -1;
}
if( box.height < 0 )
{
box.y += box.height;
box.height *= -1;
}
draw_box( image, box);
break;
case CV_EVENT_RBUTTONUP:
destroy=true;
}
}
int main()
{
const char* name = "Box Example";
cvNamedWindow( name );
box = cvRect(0,0,1,1);
CvCapture* capture = cvCreateFileCapture( "C:\\video.mp4" );
image = cvQueryFrame( capture );
IplImage* temp = cvCloneImage( image );
// Set up the callback
cvSetMouseCallback( name, my_mouse_callback, (void*) image);
//IplImage *img2 = cvCreateImage(cvGetSize(temp),temp->depth,temp->nChannels);
//cvNot(temp,temp);
/* copy subimage */
//cvCopy(temp, temp, NULL);
// Main loop
while( 1 )
{
if(destroy) {cvDestroyWindow(name); break;}
cvCopyImage( image, temp );
if( drawing_box )
draw_box( temp, box );
cvMoveWindow(name, 200, 100);
cvShowImage( name, temp );
if( cvWaitKey( 15 )==27 )
break;
}
//cvReleaseImage( &image );
cvReleaseImage( &temp );
cvDestroyWindow( name );
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
cvMoveWindow("Example2", 150, 150);
while(1)
{
frame2 = cvQueryFrame( capture );
draw_box(frame2,box);
if( !frame2 ) break;
cvShowImage( "Example2", frame2 );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
return 0;
}
You were almost there. One problem though: case CV_EVENT_RBUTTONUP needs to break, and I would also add a break on default case.
The following code sets the ROI, performs a simple grayscale processing on it and then copies the processed ROI back to the original image.
For testing purposes I changed your code to use my camera instead of loading a file.
Output:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cv.h>
#include <highgui.h>
using namespace cv;
using namespace std;
void my_mouse_callback( int event, int x, int y, int flags, void* param );
bool destroy=false;
CvRect box;
bool drawing_box = false;
void draw_box(IplImage* img, CvRect rect)
{
cvRectangle(img, cvPoint(box.x, box.y), cvPoint(box.x+box.width,box.y+box.height),
cvScalar(0,0,255) ,2);
CvRect rect2=cvRect(box.x,box.y,box.width,box.height);
//cvSetImageROI(image, rect2); //here I wanted to set the drawn rect as ROI
}
// Implement mouse callback
void my_mouse_callback( int event, int x, int y, int flags, void* param )
{
IplImage* frame = (IplImage*) param;
switch( event )
{
case CV_EVENT_MOUSEMOVE:
{
if( drawing_box )
{
box.width = x-box.x;
box.height = y-box.y;
}
}
break;
case CV_EVENT_LBUTTONDOWN:
{
drawing_box = true;
box = cvRect( x, y, 0, 0 );
}
break;
case CV_EVENT_LBUTTONUP:
{
drawing_box = false;
if( box.width < 0 )
{
box.x += box.width;
box.width *= -1;
}
if( box.height < 0 )
{
box.y += box.height;
box.height *= -1;
}
draw_box(frame, box);
}
break;
case CV_EVENT_RBUTTONUP:
{
destroy=true;
}
break;
default:
break;
}
}
int main()
{
const char* name = "Box Example";
cvNamedWindow( name );
box = cvRect(0,0,1,1);
CvCapture* capture = cvCaptureFromCAM(0);
if (!capture)
{
printf("!!! Failed cvCaptureFromCAM\n");
return 1;
}
IplImage* image = cvQueryFrame(capture);
if (!image)
{
printf("!!! Failed cvQueryFrame #1\n");
return 2;
}
IplImage* temp = cvCloneImage(image);
// Set up the callback
cvSetMouseCallback(name, my_mouse_callback, (void*) image);
// Main loop
while( 1 )
{
if (destroy)
{
cvDestroyWindow(name); break;
}
cvCopyImage(image, temp);
if (drawing_box)
draw_box(temp, box);
cvMoveWindow(name, 200, 100);
cvShowImage(name, temp);
if (cvWaitKey(15) == 27)
break;
}
cvReleaseImage(&temp);
cvDestroyWindow(name);
cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
cvMoveWindow("Example2", 150, 150);
// Retrieve a single frame from the device and set the ROI
IplImage* vid_frame = cvQueryFrame(capture);
if (!vid_frame)
{
printf("!!! Failed cvQueryFrame #2\n");
return 2;
}
cvSetImageROI(vid_frame, box);
// Allocate space for a single-channel ROI (to store grayscale frames)
IplImage* gray_roi = cvCreateImage(cvSize(box.width, box.height), IPL_DEPTH_8U, 1);
IplImage* rgb_roi = cvCreateImage(cvSize(box.width, box.height), IPL_DEPTH_8U, 3);
while(1)
{
if (!vid_frame)
{
vid_frame = cvQueryFrame(capture);
if (!vid_frame)
{
printf("!!! Failed cvQueryFrame #3\n");
break;
}
}
draw_box(vid_frame, box);
// Set ROI and perform some processing (in this case, converting the ROI to grayscale)
cvSetImageROI(vid_frame, box);
cvCvtColor(vid_frame, gray_roi, CV_BGR2GRAY);
//cvShowImage("Example2", gray_roi);
/* At this point gray_roi has the size of thei ROI and contains the processed image.
* For fun, we copy the processed image back to the original image and display it on the screen!
*/
cvCvtColor(gray_roi, rgb_roi, CV_GRAY2BGR);
// As the ROI is still set, cvCopy is affected by it
cvCopy(rgb_roi, vid_frame, NULL);
// Now reset the ROI so cvShowImage displays the full image
cvResetImageROI(vid_frame);
cvShowImage("Example2", vid_frame);
char c = cvWaitKey(33);
if( c == 27 ) break;
vid_frame = NULL;
}
cvSaveImage("processed.jpg", vid_frame);
cvReleaseImage(&gray_roi);
cvReleaseImage(&rgb_roi);
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
return 0;
}
Related
I am trying to code a program on opencv to decide whether a human has approached ahead the camera. After I run the execution file, I get the captured video for few seconds and encounter the segmentation fault error.
The code is like this
Here are headers:
#include "opencv2/objdetect.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
//define static variable
static int cApp = 0;//number of approached frame
static double last = 0;
//define functions
void detectAndDisplay( Mat frame );
bool computeArea( double width, double height, double lastArea);
double runningAverage(int M);
//define opencv function and classifier
String upperbody_cascade_name = "home/pi/opencv- 3.0.0/data/haarcascades/haarcascade_upperbody.xml";
CascadeClassifier upper_cascade;
String window_name = "Capture - upper body detection";
Here is the main function:
int main( void )
{
//define variable
VideoCapture capture;
Mat frame;
//-- 1. Load the cascades
upper_cascade.load("/home/pi/opencv-3.0.0/data/haarcascades/haarcascade_upperbody.xml");
//-- 2. Read the video stream
capture.open( -1 );
if ( ! capture.isOpened() ) { printf("--(!)Error opening video capture\n"); return -1; }
while ( capture.read(frame) )
{
if( frame.empty() )
{
printf(" --(!) No captured frame -- Break!");
break;
}
//-- 3. Apply the classifier to the frame
detectAndDisplay( frame );
char c = (char)waitKey(10);
if( c == 27 ) { break; } // escape
}
capture.release();
return 0;
}
Here is the detectAndDisplay function:
void detectAndDisplay( Mat frame )
{
std::vector<Rect> upperbodys;
Mat frame_gray;
cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect upperbodys
upper_cascade.detectMultiScale( frame_gray, upperbodys, 1.05, 3, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );
Point center( upperbodys[0].x + upperbodys[0].width/2, upperbodys[0].y + upperbodys[0].height/2 );
ellipse( frame, center, Size( upperbodys[0].width/2, upperbodys[0].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
bool ifApproached = computeArea(upperbodys[0].width/2, upperbodys[0].height/2, last);
if (ifApproached == true) {
cApp++;
}
if (cApp == 3) {
cout << "have approached" << endl;
cApp = cApp - 3;
}
//-- Show what you got
imshow( window_name, frame );
}
Here is the computeArea function:
bool computeArea( double width, double height, double lastArea) {
double newArea = width * height;
bool ifApproached = false;
//double presentArea = newArea;
double presentArea = runningAverage(newArea);
double DifferenceBewteenAreas = presentArea - lastArea;
if (DifferenceBewteenAreas > 1) {//threshold
ifApproached = true;
}
last = presentArea;
return ifApproached;
}
Here is runningAverage function:
double runningAverage(int M) {
//M is measurement
//#define LM_SIZE 5
static int LM[5];
static int index =0;
static long sum = 0;
static int count =0;
//keep sum updated to improve speed
sum = sum - LM[index];
LM[index] = M;
sum = sum + LM[index];
index++;
index = index % 5;
if (count < 5) {
count++;
}
return (double)(sum / (double)count);
}
I have searched many opencv segmentation fault questions, some said this segmentation fault was caused by wrong array used, but my case has little use of array. Others said misused of function characters could also cause this kind of errors, I agree with this, some of my characters could be wrong here.
Actually I found that I should not use upperbodys[0] in the code, because sometimes there are no object being detected at all,so there could be some memory read error happens, I used upperbodys[i] instead and it works well then.
void detectAndDisplay( Mat frame )
{
std::vector<Rect> upperbodys;
Mat frame_gray;
cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
equalizeHist( frame_gray, frame_gray );
//-- Detect upperbodys
upper_cascade.detectMultiScale( frame_gray, upperbodys, 1.05, 3, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );
int size = upperbodys.size();
double newArea = -1;
for (int i = 0 ; i < size; i++) {
Point center( upperbodys[i].x + upperbodys[i].width/2, upperbodys[i].y + upperbodys[i].height/2 );
ellipse( frame, center, Size( upperbodys[i].width/2, upperbodys[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
//bool ifApproached = computeArea(upperbodys[i].width/2, upperbodys[i].height/2, last);
//////////////////////////////////////////
newArea = upperbodys[i].width/2 * upperbodys[i].height/2;
if (newArea != -1) {
cout << "UpperBodys has value, index = " << i << endl;
break;
}
}
bool ifApproached = false;
//double presentArea = runningAverage(newArea);
double presentArea = newArea;
double DifferenceBewteenAreas = presentArea - last;
if (DifferenceBewteenAreas > 1) {//threshold
ifApproached = true;
}
last = presentArea;
//////////////////////////////////////////
if (ifApproached == true) {
cApp++;
}
if (cApp == 3) {
cout << "have approached" << endl;
cApp = cApp - 3;
}
//-- Show what you got
imshow( window_name, frame );
}
So I have been experimenting with OpenCV and I don't understand the update rules in regards to namedWindow. I would expect to be able to place the lines as either:
cv::namedWindow( "Example-in", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example-out", cv::WINDOW_AUTOSIZE );
g_cap.open( string(argv[1]) );
or
g_cap.open( string(argv[1]) );
cv::namedWindow( "Example-in", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example-out", cv::WINDOW_AUTOSIZE );
The first way works fine and updates my windows. However, the second way only produces a still image with and without the filter.
Full code here:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace std;
void example2_5( const cv::Mat & image ) {
// Create some windows to show the input
// and output images in.
//
//cv::namedWindow( "Example2_5-in", cv::WINDOW_AUTOSIZE );
//cv::namedWindow( "Example2_5-out", cv::WINDOW_AUTOSIZE );
// Create a window to show our input image
//
cv::imshow( "Example2_5-in", image );
// Create an image to hold the smoothed output
//
cv::Mat out;
// Do the smoothing
// ( Note: Could use GaussianBlur(), blur(), medianBlur() or bilateralFilter(). )
//
cv::GaussianBlur( image, out, cv::Size(5,5), 3, 3);
cv::GaussianBlur( out, out, cv::Size(5,5), 3, 3);
// Show the smoothed image in the output window
//
cv::imshow( "Example2_5-out", out );
// Wait for the user to hit a key, windows will self destruct
//
cv::waitKey( 10 );
}
int g_run = 1, g_dontset = 0; //start out in single step mode
cv::Size size(640,360);
cv::VideoCapture g_cap;
void onTrackbarSlide( int pos, void *) {
g_cap.set( cv::CAP_PROP_POS_FRAMES, pos );
if( !g_dontset )
g_run = 1;
g_dontset = 0;
}
int main( int argc, char** argv ) {
cout<<"g_run :"<<g_run<<"\n g_dontset"<<g_dontset<<endl;
cv::namedWindow( "Example2_5-in", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example2_5-out", cv::WINDOW_AUTOSIZE );
g_cap.open( string(argv[1]) );
g_cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
g_cap.set(CV_CAP_PROP_FRAME_HEIGHT, 360);
cv::Mat frame;
for(;;) {
cout<<"g_run :"<<g_run<<"\n g_dontset"<<g_dontset<<endl;
if( g_run != 0 ) {
g_cap >> frame; if(frame.empty()) break;
cv::resize(frame,frame,size,0,0,CV_INTER_CUBIC);
example2_5(frame);
g_dontset = 1;
g_run-=1;
}
char c = (char) cv::waitKey(10);
if( c == 's' ) // single step
{g_run = 1; cout << "Single step, run = " << g_run << endl;}
if( c == 'r' ) // run mode
{g_run = -1; cout << "Run mode, run = " << g_run <<endl;}
if( c == 27 )
break;
}
return(0);
}
Apologies for ugliness
How can I add roi-based selection in lkdemo.pp( klt optical flow tracker opencv example) source code?
I want select roi in the first frame and track feature point that selected in roi.
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
static void help()
{
// print a welcome message, and the OpenCV version
cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
"Using OpenCV version " << CV_VERSION << endl;
}
Point2f point;
bool addRemovePt = false;
static void onMouse( int event, int x, int y, int , void* )
{
if( event == CV_EVENT_LBUTTONDOWN )
{
point = Point2f((float)x, (float)y);
addRemovePt = true;
}
}
int main( int argc, char** argv )
{
help();
VideoCapture cap(CV_CAP_ANY);
TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
Size subPixWinSize(10,10), winSize(61,61);
const int MAX_COUNT = 500;
bool needToInit = false;
bool nightMode = false;
//if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
//cap.open(argc == 2 ? argv[1][0] - '0' : 0);
//else if( argc == 2 )
//cap.open(argv[1]);
if( !cap.isOpened() )
{
cout << "Could not initialize capturing...\n";
return 0;
}
namedWindow( "LK Demo", 1 );
setMouseCallback( "LK Demo", onMouse, 0 );
Mat gray, prevGray, image;
vector<Point2f> points[2];
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() )
break;
frame.copyTo(image);
cvtColor(image, gray, COLOR_RGB2GRAY);
if( nightMode )
image = Scalar::all(0);
if( needToInit )
{
// automatic initialization
goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
addRemovePt = false;
}
else if( !points[0].empty() )
{
vector<uchar> status;
vector<float> err;
if(prevGray.empty())
gray.copyTo(prevGray);
calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,10, termcrit, 0, 0.001);
size_t i, k;
for( i = k = 0; i < points[1].size(); i++ )
{
if( addRemovePt )
{
if( norm(point - points[1][i]) <= 5 )
{
addRemovePt = false;
continue;
}
}
if( !status[i] )
continue;
points[1][k++] = points[1][i];
circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
}
points[1].resize(k);
}
if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
{
vector<Point2f> tmp;
tmp.push_back(point);
cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
points[1].push_back(tmp[0]);
addRemovePt = false;
}
needToInit = false;
imshow("LK Demo", image);
char c = (char)waitKey(10);
if( c == 27 )
break;
switch( c )
{
case 'r':
needToInit = true;
break;
case 'c':
points[0].clear();
points[1].clear();
break;
case 'n':
nightMode = !nightMode;
break;
}
std::swap(points[1], points[0]);
cv::swap(prevGray, gray);
}
return 0;
}
Here is what I use in those cases :
void SelectNewTemplate(int event, int posx, int posy, int flags, void* userdata)
{
if( event == EVENT_MBUTTONDOWN )
{
waitKey();
}
if( event == CV_EVENT_LBUTTONDOWN )
{
x1pt = posx;
y1pt = posy;
}
if( event == CV_EVENT_LBUTTONUP )
{
x2pt = posx;
y2pt = posy;
Rect newTemp(x1pt, y1pt, (x2pt - x1pt), (y2pt - y1pt));
Mat imgROI = frame(newTemp);
}
}
Usage : pausing the video with middle mouse button, then left clic, drag and let go, press any key to keep going.
After that you can compute your features on your new ROI image : imgROI.
Hope that helps,
Thomas
I'm very new to OpenCV (c++), my lecturer ask me to make a simple slider that every position in slider have different place in the window. My code below can move the object in window based on slider position but when i moved the slider the old position still there. So it looks like a duplicate not move. Can anyone help me on this problem?? Are there a way to solve this problem or I must change the code completely??
#include "stdafx.h"
#include "cv.h"
#include "ml.h"
#include "cxcore.h"
#include "highgui.h"
int g_switch_value = 0;
int colorInt = 0;
void switch_callback( int position ){
if( position == 0 ){
colorInt = 0;
}else{
colorInt = 1;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* name = "Change the color of circle in the picture";
int radius = 30;
int thickness = 12;
int connectivity = 8;
cvscalar red = cv_RGB(0,0,255);
IplImage* src1 = cvLoadImage( "E:/2.jpg" );
CvPoint pt1 = cvPoint(405,195);
CvPoint pt2 = cvPoint(620,400);
cvNamedWindow( name, 1 );
cvShowImage(name, src1);
cvCreateTrackbar( "Change", name, &g_switch_value, 1, switch_callback );
while( 1 ) {
if( colorInt == 0) {
cvCircle(src1,pt1,radius,red,thickness,connectivity);}
else {
cvCircle(src1,pt2,radius,red,thickness,connectivity); }
colorInt == 1;
cvShowImage(name, src1);
if( cvWaitKey( 15 ) == 27 ) break;
}
cvReleaseImage( &src1 );
cvDestroyWindow( name );
return 0; }
Drawing overwrite the image, then you should keep a fresh copy for each different painting.
The easier way:
IplImage* src1 = cvLoadImage( "..." );
IplImage* src2 = cvLoadImage( "..." );
...
while( 1 ) {
if( colorInt == 0) {
cvShowImage(name, src1);
cvCircle(src1,pt1,radius,red,thickness,connectivity);
}
else {
cvShowImage(name, src2);
cvCircle(src2,pt2,radius,red,thickness,connectivity);
}
if( cvWaitKey( 15 ) == 27 ) break;
}
cvReleaseImage( &src1 );
cvReleaseImage( &src2 );
This may seem trivial for most people but I am getting problems in determining the exact size i.e. the exact width and height of my video frames. I used cvGetSize but I am probably coding it inaccurately because I am getting an error. Is it possible to output the values of the width and height of my frames as I have included in my code below? Please I would appreciate it if someone could advise me on this.
#include "cv.h"
#include "highgui.h"
#include "iostream"
using namespace std;
int main( int argc, char* argv ) {
CvCapture *capture = NULL;
capture = cvCaptureFromAVI("C:\\walking\\lady walking.avi");
if(!capture){
return -1;
}
IplImage* color_frame = NULL;
IplImage* gray_frame = NULL ;
int thresh_frame = 17;
int frameCount=0;//Counts every 5 frames
cvNamedWindow( "contours", CV_WINDOW_AUTOSIZE );
while(1) {
color_frame = cvQueryFrame( capture );//Grabs the frame from a file
if( !color_frame ) break;
gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);
if( !color_frame ) break;// If the frame does not exist, quit the loop
frameCount++;
if(frameCount==5)
{
cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
cvThreshold(gray_frame, gray_frame, thresh_frame, 255, CV_THRESH_TOZERO_INV);
cvGetSize(gray_frame);
int w;
int h;
cvSize(w,h);
cout <<" dimensions " << cvSize(w, h) << endl;
cvShowImage("contours", gray_frame);
frameCount=0;
}
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseImage(&color_frame);
cvReleaseImage(&gray_frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "contours" );
return 0;
}
Try the following code :)
The key point is the usage of cvGetSize function and CvSize structure.
#include "cv.h"
#include "highgui.h"
#include "iostream"
using namespace std;
int main( int argc, char* argv ) {
CvCapture *capture = NULL;
capture = cvCaptureFromAVI("C:\\walking\\lady walking.avi");
if(!capture){
return -1;
}
IplImage* color_frame = NULL;
IplImage* gray_frame = NULL ;
int thresh_frame = 17;
int frameCount=0;//Counts every 5 frames
cvNamedWindow( "contours", CV_WINDOW_AUTOSIZE );
while(1) {
color_frame = cvQueryFrame( capture );//Grabs the frame from a file
if( !color_frame ) break;
gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);
if( !color_frame ) break;// If the frame does not exist, quit the loop
frameCount++;
if(frameCount==5)
{
cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
cvThreshold(gray_frame, gray_frame, thresh_frame, 255, CV_THRESH_TOZERO_INV);
CvSize dim = cvGetSize(gray_frame);
cout <<" dimensions:: height:" << dim.height<<" width:"<< dim.width<< endl;
cvShowImage("contours", gray_frame);
frameCount=0;
}
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseImage(&color_frame);
cvReleaseImage(&gray_frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "contours" );
return 0;
}