2014-07-10 2 views
-3

은 개체 식별에 윤곽선을 사용하고있었습니다. 코드는 이미지에서 잘 작동했으며 코드를 수정하여 카메라 입력으로 실시간으로 객체를 식별했습니다. 내 노트북의 통합 카메라로는 잘 작동하지만 외부 카메라를 사용할 때 몇 초 후에 충돌이 발생합니다. 외부 카메라는 opencv를 사용하여 개발 한 몇 가지 다른 응용 프로그램과 잘 작동했습니다. 카메라는 20MP 카메라입니다. 코드를보고 무엇이 잘못 됐는지 알아내는 데 도움이됩니다. 내 프로세서는 고해상도 이미지를 처리 ​​할만큼 좋습니다. 앱을 시작할 때 이전에 없었던 캠 앞에 객체를 추가하면 앱이 다운되는 것 같습니다.외부 카메라를 사용할 때 Opencv 앱이 작동을 멈 춥니 다.

include <iostream> 
include "opencv2/highgui/highgui.hpp" 
include "opencv2/imgproc/imgproc.hpp" 
using namespace cv; using namespace std; 
int main() 
{ 
int largest_area = 0; 
int largest_contour_index = 0; 
Rect bounding_rect; 
int x = 0; 
int y = 0; 
VideoCapture xps(0); 
Mat src; 

while (1) 
{ 

xps.read(src); 


vector<vector<Point>> contours; // Vector for storing contour 
vector<Vec4i> hierarchy; 


Mat thr(src.rows, src.cols, CV_8UC1); 
Mat dst(src.rows, src.cols, CV_8UC1, Scalar::all(0)); 
cvtColor(src, thr, CV_BGR2GRAY); //Convert to gray 
threshold(thr, thr, 80, 255, THRESH_BINARY_INV); 
findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); 

for (int i = 0; i< contours.size(); i++) // iterate through each contour. 
{ 
    double a = contourArea(contours[i], false); // Find the area of contour 
    if (a>largest_area) 
    { 
     largest_area = a; 
     largest_contour_index = i;     
     bounding_rect = boundingRect(contours[i]); 
    } 

} 


Scalar color(255, 255, 255); 
drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); 
rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0); 

x = bounding_rect.x + bounding_rect.width/2; 
y = bounding_rect.y + bounding_rect.height/2; 

circle(src, Point(x, y), 1, Scalar(0, 0, 255)); 

imshow("src", src); 
imshow("largest Contour", dst); 
waitKey(2); 

} }

+2

"코드를보고 무엇이 잘못 되었을지 파악해주세요." 도움이 필요하면 오류에 대한 더 많은 정보를 제공하십시오 : 코드가 어디에서 충돌했는지, 오류 메시지는 무엇입니까? –

+0

'VideoCapture' 객체를 만든 후'xps.isOpen()'을 확인하십시오. false이면 카메라가 열려 있지 않습니다. 'xps.read (frame)'이'false'를 반환하면 이것을 확인할 수 있습니다. –

답변

1

나는 충돌이 발견되지 않을 수 있습니다 윤곽 예정이다 믿습니다. 이 문제를 피하려면 플래그를 사용하고 윤곽선이있는 경우 그려야합니다.

bool found = findContours(thr, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); 

/* for loop */ 

if(found) 
{ 
    drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); 
    rectangle(src, bounding_rect, Scalar(0, 255, 0), 1, 8, 0); 

    x = bounding_rect.x + bounding_rect.width/2; 
    y = bounding_rect.y + bounding_rect.height/2; 

    circle(src, Point(x, y), 1, Scalar(0, 0, 255)); 
} 
관련 문제