IMWRITE writing all black pictures?
11 次查看(过去 30 天)
显示 更早的评论
I've looked at the documentation for this but I'm clearly missing something. The highest rated answer according to google on MATLAB Answers was to perform
A = A - min(A(:));
A = A/max(A(:));
However, when I do this, my values are all 0. The data I'm using for imwrite is characterized as a 512x512 uint16 variable.
1 个评论
Stephen23
2017-1-23
@Lee: please edit your question and upload a sample array by clicking the paperclip button. Also show us exactly the code that you are using so that we can replicate the problem.
回答(1 个)
Guillaume
2017-1-23
编辑:Guillaume
2017-1-23
Not many format support uint16 images. The only ones available to imwrite are JPEG, PNG and TIFF. Support for reading and displaying 16 bit images is also variable. Since most displays are only 8 bits, viewers have to convert the 16 bits to 8. Some viewers may do that by only displaying the low bits, other the high bits, which depending on the image may result in everything black (or white). So how are you viewing the image?
To be safe you could convert the image to 8 bits (or double, but imwrite will then convert it to 8 bits):
imwrite(im2uint8(yourimage), somefile);
%or
imwite(im2double(yourimage), somefile);
Of course, by converting to 8 bit you're dividing your dynamic range by 256.
edit: the reason your A = (A - min(A(:))) /max(A(:)) produces all zero is because you're doing integer division. You need to convert both numerators and denominators to double before dividing:
A = double(A - min(A(:))) / double(max(A(:))
2 个评论
Walter Roberson
2017-1-24
minA = min(A(:));
A = double(A - minA) ./ double( max(A(:)) - minA );
The result will be in the range 0 to 1. When you imwrite() that, imwrite() will see that it is floating point data and will automatically do im2uint8() and write the resulting 8 bits per channel image
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!