title: "Sometimes" getting garbage value for an integer type varibale in openCV
body: <p>I build a program using the basic idea of detecting squares from <code>square.c</code> of openCV samples. I detect the squares in <code>CvSeq*</code> and then i calculate the minimum_x, min_y, width and columns to create seperate image for each detected square. </p>
  
  <p><strong>Problem:</strong> <em>But sometimes i am getting  <code>min_x = 149186927</code> and <code>min_y = 149186937</code></em></p>
  
  <p><em><strong>I am doing the following process in a <code>for loop</code> i.e. <code>for(int i=0; i&lt;square.size(); i+=4)</code>. So, if i detect 12 square then it works for all 12 square. But my program crash after sometime. It works for sometime whether it is one square or more than one.</em></strong></p>
  
  <p>First of all, i store four points of the square as following:</p>
  
  <pre><code>    CvPoint pt_reader[4] ;
      pt_reader[0].x =  ( (CvPoint*)cvGetSeqElem(squares, i) )-&gt;x; // first coordinate 
      pt_reader[0].y =  ( (CvPoint*)cvGetSeqElem(squares, i) )-&gt;y;
  
      pt_reader[1].x =  ( (CvPoint*)cvGetSeqElem(squares, i+1) )-&gt;x;// second coordinate
      pt_reader[1].y =  ( (CvPoint*)cvGetSeqElem(squares, i+1) )-&gt;y;
  
      pt_reader[2].x =  ( (CvPoint*)cvGetSeqElem(squares, i+2) )-&gt;x;
      pt_reader[2].y =  ( (CvPoint*)cvGetSeqElem(squares, i+2) )-&gt;y;
  
      pt_reader[3].x =  ( (CvPoint*)cvGetSeqElem(squares, i+3) )-&gt;x;
      pt_reader[3].y =  ( (CvPoint*)cvGetSeqElem(squares, i+3) )-&gt;y;
  </code></pre>
  
  <p>Then i try to get the minimum_x and minimum_y from those 4 coordinates of the square so that i can create a ROI</p>
  
  <pre><code>        int min_x = findMin_x_pointA(pt_reader); 
          int min_y = findMin_y_pointB(pt_reader); 
          int max_x = findMax_x_pointC(pt_reader);
          int max_y = findMax_y_pointD(pt_reader);
  </code></pre>
  
  <p>Calculating the number of rows and cols for ROI</p>
  
  <pre><code>        int cols = max_x - min_x;
          int rows = max_y - min_y;
  </code></pre>
  
  <p>Now, i am finally creating an image for ROI</p>
  
  <pre><code>        if(rows&gt;0 &amp;&amp; rows&lt;300 &amp;&amp; cols&gt;0 &amp;&amp; cols&lt;300)
          {
              Mat mySquare;
              mySquare.create(rows, cols, CV_8UC3);
              cout&lt;&lt;"\nROI:  "&lt;&lt;min_x&lt;&lt;"   "&lt;&lt;min_y&lt;&lt;"   "&lt;&lt;cols&lt;&lt;"   "&lt;&lt;rows;
              Rect regionOfInterest = Rect (min_x,min_y, cols, rows);
              mySquare= original_frame(regionOfInterest);
  
              squareImages.push_back(mySquare);
  
          }
  </code></pre>
  
  <p><strong>My functions to calculate the minimum values look like following:</strong></p>
  
  <pre><code>int FindRect::findMin_x_pointA(CvPoint pt_reader[] )
  {
      CvPoint pointA;
          int min_x =pt_reader[0].x;
  
          for(int i=1; i&lt;4; i++)
          {
              if(min_x &gt; pt_reader[i].x)
              {
                  min_x= pt_reader[i].x;
              }
          }
          return min_x;
      }
  </code></pre>