imwrite command not working properly
Mostrar comentarios más antiguos
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 comentarios
Ben11
el 26 de Jul. de 2014
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?
Ali
el 26 de Jul. de 2014
dpb
el 26 de Jul. de 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.
Respuestas (3)
Star Strider
el 26 de Jul. de 2014
0 votos
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 comentarios
Star Strider
el 26 de Jul. de 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.
Star Strider
el 26 de Jul. de 2014
How did you create cgh?
Image Analyst
el 26 de Jul. de 2014
Try transposing or rotating before calling imwrite
a3i = a3i'; % Transpose, or
a3i = imrotate(a3i, 90);
5 comentarios
Star Strider
el 26 de Jul. de 2014
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.
Image Analyst
el 26 de Jul. de 2014
Editada: Image Analyst
el 26 de Jul. de 2014
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.
Star Strider
el 26 de Jul. de 2014
You’re correct.
I don’t know why my random (literally, using randi(255,1920,1080)) test image failed that test in MS Paint.
Image Analyst
el 26 de Jul. de 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
el 26 de Jul. de 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.
Image Analyst
el 26 de Jul. de 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?
Finally please read this: http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/
Categorías
Más información sobre Image Data en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!