Search code examples
visual-c++opencvimage-comparison

opencv capture image from webcam without post processing


I want to capture images from webcam without any post processing, that is NO auto focus , exposure correction , white balance and stuff. Well basically I want to capture continuous frames from webcam and make each frame compare with the previous one and save them to disk only when there is an actual change. Because of the post processing almost every frame is being returned as different for me.

code so far

using namespace cv;



bool identical(cv::Mat m1, cv::Mat m2)
{


if ( m1.cols != m2.cols || m1.rows != m2.rows || m1.channels() != m2.channels() || m1.type() != m2.type() )
{
  return false;
}

for ( int i = 0; i < m1.rows; i++ )
{
  for ( int j = 0; j < m1.cols; j++ )
    {
      if (  m1.at<Vec3b>(i, j) != m2.at<Vec3b>(i, j) )
        {
          return false;
        }
    }
}
return true;
}


 int main() {
 CvCapture* capture = cvCaptureFromCAM( 1);
 int i=0,firsttime=0;
 char filename[40];
 Mat img1,img2;
 if ( !capture ) {
 fprintf( stderr, "ERROR: capture is NULL \n" );
 getchar();
 return -1;
 }

 cvNamedWindow( "img1", CV_WINDOW_AUTOSIZE );
  cvNamedWindow( "img2", CV_WINDOW_AUTOSIZE );

  while ( 1 ) {

 IplImage* frame = cvQueryFrame( capture );
 img1=frame;
 if ( !frame ) {

   fprintf( stderr, "ERROR: frame is null...\n" );
   getchar();
   break;
 }
 if(firsttime==0){
 img2=frame;
 fprintf( stderr, "firtstime\n" );
 } 

 if ( (cvWaitKey(10) & 255) == 27 ) break;

 i++;

 sprintf(filename, "D:\\testimg\\img%d.jpg", i);


 cv::cvtColor(img1, img1, CV_BGR2GRAY);
 imshow( "img1", img1);
 imshow( "img2", img2);
 imwrite(filename,img1);

 if(identical(img1,img2))
 {
    //write to diff path
 }


 img2=imread(filename,1);
 firsttime=1;
}
 // Release the capture device housekeeping
 cvReleaseCapture( &capture );
 return 0;
}

While ur at it, I'll be great full if u can suggest a workaround for this using another frame compare solution aswell :)


Solution

  • with a bit of luck, you can get the properties page of your camera, and switch things off there:

    VideoCapture cap(0);
    cap.set(CV_CAP_PROP_SETTINGS,1);
    

    and please, skip the c-api in favour of c++. it'll go away soon.

    forgot to mention : you an change the cam-settings from vlc as well.