Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This will be because the <code>UIImage</code> is not actually portrait. All photos taken with the iPhone camera are landscape in their raw bitmap state, eg 3264 wide x 2488 high. A "portrait" photo is displayed as such by the orientation EXIF flag set in the image, which is honoured, for example, by the photo library app which swivels images according to this flag and the viewing orientation of the camera.</p> <p>The flag also affects how <code>UIImage</code> reports its width and height properties, transposing them from their bitmap values for images flagged as portrait. </p> <p><code>cv::Mat</code> doesn't bother with any of that. This means that (i) when translating to <code>cv::Mat</code> a portrait image will have its <code>size.width</code> and <code>size.height</code> values transposed, and (ii) when translating back from <code>cv::Mat</code> you will have lost the orientation flag. </p> <p>The simplest way to handle this when going from <code>UIImage</code> to <code>cv::Mat</code> is to swap width and height values if the image is flagged as portrait:</p> <pre><code>if (self.imageOrientation == UIImageOrientationLeft || self.imageOrientation == UIImageOrientationRight) { cols = self.size.height; rows = self.size.width; } </code></pre> <p>When translating back from <code>cv::Mat</code> to <code>UIImage</code>, you will want to reinstate the orientation flag. Assuming your <code>cv::Mat -&gt; UIImage</code> code contains this:</p> <pre><code>self = [self initWithCGImage:imageRef]; </code></pre> <p>you can use this method instead, and reset the orientation as per the original.</p> <pre><code>self = [self initWithCGImage:imageRef scale:1 orientation:orientation]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload