0

我正在使用 C 和 OpenCV 做“照片”处理应用程序。我的电脑会使用网络摄像头不断地观察周围环境。当它检测到移动时,它将当前的网络摄像头图像写入文件(通知)。这就是我编写代码的方式,但是它很慢,我不擅长通知......你能给我一些关于代码的好建议吗?

#include <stdio.h>  
#include "cv.h"
#include "highgui.h"
#include <dos.h>
#include <conio.h>


CvCapture   *capture;
int key;

IplImage *oldFrame;
IplImage *currentFrame;
IplImage *res;

int f=0,x,y,idx,idy;

int currentFrame_width, currentFrame_height;
int oldFrame_width, oldFrame_height;
int res_width, res_height;

int main( int argc, char** argv )
{   
    while( key != 'q' ) 
    {

           capture = cvCaptureFromCAM( 0 );
           if( !capture ) return 1;
           key = cvWaitKey( 1 );
           cvNamedWindow( "FrameController", CV_WINDOW_AUTOSIZE );

           if(f==0){

                    oldFrame = cvQueryFrame( capture );
                    cvShowImage( "FrameController", oldFrame );
                    f=1;
           }
           else{    
                    CvMemStorage* storage = cvCreateMemStorage(0);
                    currentFrame = cvQueryFrame( capture );
                    for  ( x=0;x<currentFrame->width;x++) 
                    { 
                      for ( y=0;y<currentFrame->height;y++) 
                      { 
                           idx = y*currentFrame->widthStep + currentFrame->nChannels*x; 
                           idy = y*oldFrame->widthStep + oldFrame->nChannels*x; 
                      }
                    }
                    uchar R= (uchar) currentFrame->imageData [idx+0];   
                    uchar G = (uchar) currentFrame->imageData [idx+1]; 
                    uchar B = (uchar) currentFrame->imageData [idx+2]; 
                    uchar R1= (uchar) oldFrame->imageData [idy+0];   
                    uchar G1 = (uchar) oldFrame->imageData [idy+1]; 
                    uchar B1 = (uchar) oldFrame->imageData [idy+2]; 

                    if(!((R==R1) || (G==G1) || (B==B1))){

                      f=0;
                      CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad( "C:\\OpenCV2.1\\data\\haarcascades\\haarcascade_frontalface_alt2.xml" );
                       double scale = 1.3;
                                         static CvScalar colors[] = {CV_RGB(rand()&255, rand()&255, rand()&255 )};


                                         cvClearMemStorage( storage );
                                         CvSeq* objects = cvHaarDetectObjects( currentFrame, cascade, storage, 1.1, 4, 0, cvSize( 40, 50 ));
                                         CvRect* r;


                                          for( int i = 0; i < (objects ? objects->total : 0 ); i++ ){
                                                r = ( CvRect* )cvGetSeqElem( objects, i );
                                                cvRectangle( currentFrame, cvPoint( r->x, r->y ), cvPoint( r->x + r->width, r->y + r->height ),
                                                colors[i%8]);
                                                cvShowImage( "FrameController", currentFrame );
                                          } 
                        }
           }
    }


    cvDestroyWindow( "FrameController" );   
    cvReleaseCapture( &capture );
    cvReleaseImage( &currentFrame);
    cvReleaseImage( &oldFrame);
    return 0;
}
4

1 回答 1

0

似乎您在 while 循环中进行了不必要的初始化。例如

capture = cvCaptureFromCAM( 0 ); << is that necessary to be in the loop?
cvNamedWindow( "FrameController", CV_WINDOW_AUTOSIZE ); << this?


for  ( x=0;x<currentFrame->width;x++) 
                    { 
                      for ( y=0;y<currentFrame->height;y++) 
                      { 
                           idx = y*currentFrame->widthStep + currentFrame->nChannels*x; 
                           idy = y*oldFrame->widthStep + oldFrame->nChannels*x; 
                      }
                    } << that's a loop over the WHOLE IMAGE that does NOTHING.

存储是做什么用的?

希望这有助于您清理代码。

于 2013-01-15T20:39:58.180 回答