2013-07-13 2 views
0

는이 코드를OpenCV의 동시에

while (true) 
{ 
    cv::Mat frame1,frame2,dst,temp; 
    if (!input_video.read(frame1)) 
    { 
     fprintf(stderr, "Video has Finished .\n"); 
     getchar(); 
     return -1; 
    } 
    if (!input_video.read(frame2)) 
    { 
     fprintf(stderr, "Video has Finished .\n"); 
     getchar(); 
     return -1; 
    } 
    cv::cvtColor(frame1,frame1,CV_RGB2GRAY) ; 
    cv::cvtColor(frame2,frame2,CV_RGB2GRAY) ; 
    cv::subtract(frame2,frame1,dst); 
    cv::imshow("F1",frame1); 
    cv::imshow("F2",frame2); 
    cv::imshow("dst",dst); 
    cv::waitKey(); 
     } 

을 작성하고 내가 (안 카메라하지만 내 하드 디스크에서 비디오) 비디오 프레임 1을 읽고 있어요 때, 같은 프레임 2입니다했습니다 !!!

"읽기"메서드가 frame2에 대해 동일한 프레임을 반환하는 이유를 모르겠습니다. 그래서 감산 된 이미지는 항상 빈 그림입니다 !!!

순차적 프레임을 동시에 읽는 데 특별한 조치를 취해야합니까? 예를 들어 내가 그 프레임에 도착하기 위해 다른 프레임이 줄을 사용 읽고 싶은 때마다 : 그렇다면

 input_video.set(CV_CAP_PROP_POS_FRAMES,current_frame+1); 

, 다른 방법이 일을하지가?!

감사

+0

http://stackoverflow.com/q/11469281/2065121 –

+0

그래, 난 그 읽었습니다하지만 대답은 근무하지 않았다 나! 이 코드에서 결과는 항상 0입니다 (심지어 키 프레임 !!!). frame1, frame2는 항상 동일합니다 ... – PsP

답변

1

기능 input_video.read() 새로운 객체에게 당신이 그것을 호출하고 cv::VideoCapture의 내부 버퍼의 포인터를 반환 할 때마다 작성하지 않습니다. 당신이 cv::Mat 객체에 대한 첫 번째 프레임을 복사 할 경우이 경우를 들어, 문제가 해결 된 것입니다 :

while (true) 
{ 
    cv::Mat frame,frame1,frame2,dst,temp; 
    if (!input_video.read(frame)) 
    { 
     fprintf(stderr, "Video has Finished .\n"); 
     getchar(); 
     return -1; 
    } 
    frame.assignTo(frame1); 
    if (!input_video.read(frame)) 
    { 
     fprintf(stderr, "Video has Finished .\n"); 
     getchar(); 
     return -1; 
    } 
    frame.assignTo(frame2); 
    cv::cvtColor(frame1,frame1,CV_RGB2GRAY) ; 
    cv::cvtColor(frame2,frame2,CV_RGB2GRAY) ; 
    cv::subtract(frame2,frame1,dst); 
    cv::imshow("F1",frame1); 
    cv::imshow("F2",frame2); 
    cv::imshow("dst",dst); 
    cv::waitKey(); 
     } 
+0

frame.assignTo (frame2); 우리가 frame2를 읽은 후에 코드에서, 우리는 비디오 프레임을 루핑하기 때문에, 다음 반복에서 frame1 대신에 frame2를 읽게 될 수도 있습니다. – PsP

+0

다음 줄에서 'cv :: cvtColor (frame2, frame2, CV_RGB2GRAY)'가 'frame2'내용을 수정하기 때문에 그 이유 때문에 변경되지 않았습니다. –

+0

+1 & 대답을 수락하면, 프레임을 읽을 때마다 계산을 수행하고 다른 프레임을 읽어야한다는 것을 의미하므로 다른 프레임을 읽는 "읽기"방법을 사용합니까? – PsP