title: Roi extraction with cv::rect in opencv
body: <p>I am trying to extract the first quarter of a 100x100 pixel image. I was thinking of using the cv::rect function from a roi-definition (see code example below). Furthermore my original image is part of a vector-array (here in the code-example below, I only work with one image - later there will be several but this is probably irrelevant for the example here).</p>
  
  <p>What I am observing is a strange behaviour: The newly created image "Test" seems to be ok until about half of its rows. Then it shows completely random pixels (see example image).</p>
  
  <p>What is the cause ?</p>
  
  <p>Image-description: left: orig 100x100 image      /      right: created "Test" image with errors...</p>
  
  <p><img src="https://i.stack.imgur.com/thX5n.jpg" alt="left: orig 100x100 image  / right: created &quot;Test&quot; image with errors..."></p>
  
  <pre><code>std::vector&lt;cv::Mat&gt; Image(1);   // create vector array of one 100x100 pixel image
  Image[0] = cv::imread(argv[1]);  // or in iOS:  Image[0] = [in_image CVMat];
  cv::Rect roi1 = cv::Rect(0, 0, Image[0].cols/2, Image[0].rows/2);
  cv::Mat Test = Image[0](roi1);   
  imshow("final_result", Test);    // the Test image has error rows !!!!!!! Why ????????
  </code></pre>
  
  <p>Another example:
  The second example shows the original 150x150 pixel image to the left. The rectangle rect1 is 100x100pixel. Again - same problem: The cropped image shows good performance at the beginning.But from a certain row, the pixels get messed up (in this example they turned all black...). Below is the original iOS-code. What could be the problem ??? (I am using iOS-simulator - could this be the issue??</p>
  
  <p>Code for example2:</p>
  
  <pre><code>UIImage *in_image = [UIImage imageNamed:@"image013.jpg"];    // 150 x 150 pixel image
  cv::Mat cv_in_image = [in_image CVMat];
  
  NSLog(@"cv_in_image cols = %i", cv_in_image.cols);
  NSLog(@"cv_in_image rows = %i", cv_in_image.rows);
  
  cv::Rect rect1;
  rect1.x = 28;
  rect1.y = 27;
  rect1.width = 100;
  rect1.height = 100;
  
  NSLog(@"rect1 x = %i", rect1.x);
  NSLog(@"rect1 y = %i", rect1.y);
  NSLog(@"rect1 width = %i", rect1.width);
  NSLog(@"rect1 height = %i", rect1.height);
  
  cv::Mat Image1 = cv_in_image(rect1);
  
  NSLog(@"Image1 cols = %i", Image1.cols);
  NSLog(@"Image1 rows = %i", Image1.rows);
  
  self.imageView0.image = [[UIImage alloc] UIImageFromCVMat:(Image1)];
  </code></pre>
  
  <p>The NSLog sais:</p>
  
  <pre><code>cv_in_image cols = 150
  cv_in_image rows = 150
  rect1 x = 28
  rect1 y = 27
  rect1 width = 100
  rect1 height = 100
  Image1 cols = 100
  Image1 rows = 100
  </code></pre>
  
  <p>Example2: Image to the left: 150x150 pixels / rectangle (Image1) to the right shows errors !!
  Here you can find the original image from example2: <a href="https://www.wuala.com/skuenstler/Photos/opencv/?key=wSksBqIhMIw2" rel="nofollow noreferrer">link</a>
  <img src="https://i.stack.imgur.com/Rwr3e.jpg" alt="enter image description here"></p>