title: Opencv app crashes when using external camera
body: <p>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.</p>
  
  <pre><code>include &lt;iostream&gt;
  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&lt;vector&lt;Point&gt;&gt; contours; // Vector for storing contour
  vector&lt;Vec4i&gt; 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&lt; contours.size(); i++) // iterate through each contour.
  {
      double a = contourArea(contours[i], false);  //  Find the area of contour
      if (a&gt;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);
  </code></pre>
  
  <p>}
  }</p>