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 个评论
Ben11
2014-7-26
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
2014-7-26
dpb
2014-7-26
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.
回答(3 个)
Star Strider
2014-7-26
0 个投票
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 个评论
Star Strider
2014-7-26
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
2014-7-26
How did you create cgh?
Image Analyst
2014-7-26
Try transposing or rotating before calling imwrite
a3i = a3i'; % Transpose, or
a3i = imrotate(a3i, 90);
5 个评论
Star Strider
2014-7-26
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
2014-7-26
编辑:Image Analyst
2014-7-26
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
2014-7-26
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
2014-7-26
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
2014-7-26
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
2014-7-26
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/
类别
在 帮助中心 和 File Exchange 中查找有关 Image Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!