opencv capture image from webcam without post processing

早过忘川 提交于 2019-12-08 10:10:24

问题


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 :)


回答1:


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.




回答2:


I had this problem, and the only solution that found and wrote was a program based on Direct-show (in case you're using windows )so no opencv code at all




回答3:


@Prince, sorry I have been looking for my Directshow code I didn't found it, and I don't think it will help because I used it for the DirectLink (Black magic Design)card, since I've never did that befor it was pretty hard, my suggestion will be try to use GraphEditPlus : http://www.infognition.com/GraphEditPlus/

it helps a lot, and it's easy to use ! good luck !




回答4:


If you just wish to capture frames when there is an actual change, try background subtraction algorithms. Also, instead of just subtracting subsequent frames, use one of the many algorithms already implemented for you in OpenCV - they are much more robust to changes in lightning conditions etc than vanilla background subtraction.

In Python :

backsub = cv2.BackgroundSubtractorMOG2(history=10000,varThreshold=100)
fgmask = backsub.apply(frame, None, 0.01)

Frame is the stream of pictures read from your webcam. Google for the corresponding function in Cpp.



来源:https://stackoverflow.com/questions/17993677/opencv-capture-image-from-webcam-without-post-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!