title: OpenCV corrupting HQ Rapsicam data above 4000x3040 resoution?
body: <h1>The Problem</h1>
  <p>When I take an image using OpenCV that is larger than 4000 pixels wide, using a high quality picam, the image data appears corrupted (it's offset).</p>
  <h2>More Detail</h2>
  <p>As stated, I cannot get a good image when using OpenCV at over 4000 pixels wide of resolution. I know that the hardware is not the issue as I can get a clean full resolution (4056x3040) image when I use the raspistill command:</p>
  <p><code>sudo raspistill -o testing.tiff -w 4056 -h 3040</code></p>
  <p>Here is the clean output from raspistill:</p>
  <p><a href="https://i.stack.imgur.com/EuE8o.jpg" rel="nofollow noreferrer">4056x3040 image - Clean Image produced by raspistill command</a></p>
  <p>I am able to capture images using OpenCV in either Python:</p>
  <pre><code>import cv2 as cv
  
  def main():
      cap = cv.VideoCapture(&quot;/dev/video2&quot;, cv.CAP_V4L2)
      cap.set(cv.CAP_PROP_FRAME_WIDTH, 4056)#works fine up to 4000
      cap.set(cv.CAP_PROP_FRAME_HEIGHT, 3040)
  
      if not(cap.isOpened()):
          print(&quot;could not open video device&quot;)
      else:
          print(&quot;cap opened&quot;)
  
      ret, frame = cap.read()
      if(ret):
          cv.imwrite(&quot;./test_im.jpg&quot;, frame)
          print(&quot;image saved&quot;)
      else:
          print(&quot;no capture data returned&quot;)
      
  
  if __name__ == &quot;__main__&quot;:
      main() 
  </code></pre>
  <p>Or I can capture in OpenCV with C++:</p>
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;iostream&gt;
  
  int main(int argc, char *argv[]){
      cv::Mat frame;
      cv::VideoCapture cap(&quot;/dev/video2&quot;, cv::CAP_V4L2);
  
      cap.set(cv::CAP_PROP_FRAME_WIDTH, 4056);
      cap.set(cv::CAP_PROP_FRAME_HEIGHT, 3040);
  
  
      if(cap.isOpened() == false){
          std::cout &lt;&lt; &quot;cap not opened&quot; &lt;&lt; std::endl;
      }
      else{
          std::cout &lt;&lt; &quot;cap opened&quot; &lt;&lt; std::endl;
      }
      cap &gt;&gt; frame;
  
      if(frame.empty()){
          std::cout &lt;&lt; &quot;empy frame&quot;;
      }
      else{
          std::cout &lt;&lt; &quot;successfully captured image&quot; &lt;&lt; std::endl;
          cv::imwrite(&quot;test.tiff&quot;, frame);
      }
  }
  </code></pre>
  <p>The Result is the same either way.</p>
  <p>Here is the corrupted output from OpenCV:</p>
  <p><a href="https://i.stack.imgur.com/NFoB5.jpg" rel="nofollow noreferrer">4056x3040 image - Corrupted and captured by OpenCV</a></p>
  <p>To me it seems as if it something was going wrong with the timing of the capture. The format of which I save it to does not make a difference either. I have tried .png, .jpg, and .tiff. The result is always the same. After reboot the results remain the same.</p>
  <p>If I reduce the desired capture resolution to only 4000x3040 then I am able to successfully make a capture. Here are the results:</p>
  <p><a href="https://i.stack.imgur.com/1BMdC.jpg" rel="nofollow noreferrer">4000x3040 image - successfully captured by OpenCV</a></p>
  <p>I am not sure why this is happening. And would really appreciate any help! Let me know if there is any other information that could be useful!</p>
  <h1>System</h1>
  <ul>
  <li>Raspberry Pi 4B with 4GB RAM.</li>
  <li>High Quality Picam</li>
  <li>Running Ubuntu 20.04</li>
  <li>OpenCV 4.5.5</li>
  </ul>