imwrite command not working properly

4 views (last 30 days)
Ali
Ali on 26 Jul 2014
Answered: Image Analyst on 26 Jul 2014
Hi all, So, I have a matrix of 1920x1080 dimension. I want to store this in a file, so I use the following command.
a2=cgh*127/pi;
a3=a2+128;
a3i=uint8(a3);
imwrite(a3i,'loc_square.bmp')
where cgh is the 1920x1080 matrix. Now the image is correctly saved as a .bmp file but the resolution is reversed. Instead of the file resolution being 1920x1080, it is 1080x1920. Can anyone help me with this problem?
  3 Comments
Ali
Ali on 26 Jul 2014
No, the image is as it should be but the dimensions are reversed. I need to use the image in another application which requires the dimensions to be 1920x1080. I have opened the file outside matlab.
dpb
dpb on 26 Jul 2014
I'm not believing the symptoms descried accurately. What does
size(a3i)
return? I suspect the hypothesis of Ben's (or another very similar effect in something that hasn't been disclosed) is most likely the correct one.

Sign in to comment.

Answers (3)

Star Strider
Star Strider on 26 Jul 2014
MATLAB writes (and reads) and displays it correctly. You don’t say what external application you used to view it, but when I opened it in ‘Paint’ (Windows 8), regardless of how I saved my test image, either (1920x1080) or (1080x1920), ‘Paint’ always opened it as (1080x1920). You will have to manually rotate it ±90° in your external application to make it display correctly.
  3 Comments
Star Strider
Star Strider on 26 Jul 2014
You can try transposing it in MATLAB before you save it (the patterns should remain the same if the image is displayed in (1920x1080) resolution), but if it continues to display (1080x1920) even with a transposed initial image, your SLM remains the problem, not MATLAB.

Sign in to comment.


Image Analyst
Image Analyst on 26 Jul 2014
Try transposing or rotating before calling imwrite
a3i = a3i'; % Transpose, or
a3i = imrotate(a3i, 90);
  5 Comments
Image Analyst
Image Analyst on 26 Jul 2014
Did you cast to uint8? And did you try it with a 3D color image? Because if I do this, it still works as expected:
rgbImage = uint8(randi(255, [1920,1080,3]));
Star Strider
Star Strider on 26 Jul 2014
I created it as:
cgh = randi(255,1920,1080);
and then used Ali’s code, essentially unchanged (that includes an uint8 call to create a3i) except to add my imshow, and imread lines to be sure it saved and loaded correctly, and the images didn’t change.
I’m beginning to believe the problem is in the construction of cgh, which we haven’t seen.

Sign in to comment.


Image Analyst
Image Analyst on 26 Jul 2014
Why do you say the dimensions are reversed? Exactly what led you to that conclusion?
After your code, do this
[rows, columns, numberOfColorChannels] = size(a3i) % Don't use semicolon.
storedImage = imread('loc_square.bmp');
[rows, columns, numberOfColorChannels] = size(storedImage) % Don't use semicolon.
imshow(storedImage);
What does it say?
When you call imshow(), does the image look rotated or sheared?

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!