2012-02-29 72 views
0

這對於大多數人來說可能看起來微不足道,但我在確定準確大小(即我的視頻幀的確切寬度和高度)方面遇到問題。我使用cvGetSize,但我可能編碼不準確,因爲我得到一個錯誤。是否有可能輸出我的幀的寬度和高度的值,因爲我已經包含在我的代碼下面了?如果有人能就此提出建議,我將不勝感激。如何知道框架或圖像的大小

#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; 
} 

回答

2

試試下面的代碼:) 關鍵的一點是cvGetSize功能和CvSize結構的使用。

#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; 
} 
+0

非常感謝! – 2012-02-29 14:03:05

相關問題