Application crashes with external camera

asked 2014-07-10 03:08:35 -0500

thearduinoguy gravatar image

I was using contours for object identification. The code worked well with images and I modified the code to identify objects in real time with camera input. Things work well with my laptop's integrated cam but crashes after a few seconds when using an external camera. The external camera worked fine with a few other applications I developed using opencv. The camera is a 20MP camera. Please look at the code and help me figure out what might be wrong. My processor is good enough to handle images with high resolutions. It seems that the app crashes when I introduce an object in front of the cam which was not there before when the app started up.

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) {;

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


edit retag flag offensive close merge delete


could you try to format your code ? (mark it, press 10101 button)

berak gravatar imageberak ( 2014-07-10 03:12:35 -0500 )edit

i don't think, there's any problem with the camera, first try to get rid of the bugs in your code:

largest_contour_index should be initialized to -1. else you're accessing contours[0] (inside drawContours) even if there was nothing found. so please put this inside your loop (else you're reusing the values from the last iteration, kaboom):

 int largest_area = 0; int largest_contour_index = -1;
berak gravatar imageberak ( 2014-07-10 03:16:24 -0500 )edit was a problem with drawing contours. The error occurs when attempting to call draw contours even when nothing was found.

thearduinoguy gravatar imagethearduinoguy ( 2014-07-10 05:21:35 -0500 )edit

that was the crash. yes. but int largest_area = 0; has to go inside the while-loop, too, else you never get the correct largest shape per frame.

berak gravatar imageberak ( 2014-07-10 05:32:04 -0500 )edit