imwrite command not working properly

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

Do you open your file with another software than Matlab? If so it might simply be due to the dimension (i.e. width or height) displayed first, for instance with ImageJ. Is your image distorted when you re-open it?
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.
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

Ali
Ali on 26 Jul 2014
Edited: Ali on 26 Jul 2014
I have a SLM (Spatial Light Modulator), which has an LCD screen onto which this image is to be produced. The SLM has a resolution of 1920 by 1080 with a pixel pitch of 8 microns.
I checked the image dimension through it's properties. it says 1080x1920 there. and the problem is that if i rotate the image by 90, as you suggested, the patterns inside the image get rotated as well, which i dont want.
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.
How did you create cgh?

Sign in to comment.

Try transposing or rotating before calling imwrite
a3i = a3i'; % Transpose, or
a3i = imrotate(a3i, 90);

5 Comments

I did that. (My test code was the same as Ali’s, with a few diagnostic commands inserted and imread and imshow to verify it was being written and read correctly.) The problem was with MS Paint.
I do not observe that problem with mspaint. Try this code:
clc;
rgbImage = imread('peppers.png');
subplot(2,2,1);
imshow(rgbImage);
% Create a tall image.
rgbTall = imresize(rgbImage, [1920,1080]);
% Display it.
subplot(2,2,2);
imshow(rgbTall);
% Save to disk as a BMP file.
imwrite(rgbTall, 'rgbTall.bmp');
% Make a wide image.
rgbWide = imresize(rgbImage, [1080, 1920]);
% Display it.
subplot(2,2,3);
imshow(rgbWide);
% Save to disk as a BMP file.
imwrite(rgbWide, 'rgbWide.bmp');
and open up rgbTall and rgbWide in mspaint and you'll see (or should see) that each has the aspect ratio that you'd expect.
Show your code, like what you did to get cgh, etc. so I can try and replicate what you observe.
You’re correct.
I don’t know why my random (literally, using randi(255,1920,1080)) test image failed that test in MS Paint.
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]));
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.

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?

Asked:

Ali
on 26 Jul 2014

Answered:

on 26 Jul 2014

Community Treasure Hunt

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

Start Hunting!