I have just written imshow and want to find that imwrite and imshow are exactly the same. Therefore, I simply wrote imwrite usage based on the previous article.
Article link: http://blog.csdn.net/watkinsong/article/details/38535341
Images used:
In imwrite (), if the parameter is of the uint8 type, the expected pixel value range is 0-255. If the parameter matrix is of the double type, the expected pixel value range is 0-255.
In imwrite, If you convert the read image to the double type and directly use imwrite to save the image, all the white images will appear, because when converting the image type, although the pixel value of the image changes to the double data type, the value range is still 0-255. When imwrite processes the double type, the expected data range is 0-1, the value greater than 1 is treated as 1, so that almost all pixels are white.
According to the previous article, all the test code and saved image results are provided here:
%% this file is used to show how to use imshow or imagesc and so on% there is some different when you use imshow to display double image or uint8 image% here all the code will process gray image, RGB color image will not suitable for this file's code% but you can convert RGB color image to gray image if you want to exercise by this code% img will be uint8 typeimg = imread('syz.bmp');% imshow: when the parameter img is uint8 type, % imshow will default make the range of pixel value as [0-255]imshow(img);% this write correct picimwrite(img, 'sw1.bmp');fprintf('Program paused. Press enter to continue.\n');pause;% sometimes we need to process the img by double type,% so, you may convert the data type to doubledimg = double(img);% but, right now you will not get the correct display,% because if the parameter of imshow is double type, if will defaultly % take range of [0-1], but now the range is [0-255]% all the value over 1 is ceilled to 1. (clamped to 1)% so, the displayed image will be a whole white picture.imshow(dimg);% this will write a white picimwrite(dimg, 'sw2.bmp')fprintf('Program paused. Press enter to continue.\n');pause;% how to correctly display double typed image% way 1: convert double to uint8imshow(uint8(dimg));imwrite(uint8(dimg), 'sw3.bmp')fprintf('Program paused. Press enter to continue.\n');pause;% way 2: change the value of double typed image to [0-1]maxVal = max(max(dimg));imshow(dimg / maxVal);imwrite(dimg / maxVal, 'sw4.bmp');fprintf('Program paused. Press enter to continue.\n');pause;%% some other occurence, the image maybe in double type, % but some normalization operation will lead to negative pixel value% In order to display the image, we need to add a value that make% all negative value to positive value% here i just minus a specific value, in real application,% a face image maybe normalized by substract mean face.normImg = dimg - 127; % then normImg will have some negative valuesminVal = min(min(normImg));imshow(uint8(normImg - minVal));imwrite(uint8(normImg - minVal), 'sw5.bmp');fprintf('Program paused. Press enter to continue.\n');pause;%% if you want to save image by imwrite()% if the image is double type, you need to normalize the value to [0-1]% if the image is uint8 type, it's ok to save image directly.
Saved images:
SW1: Because the IMG itself is of the uint8 type, the saved image is correct.
Sw2: (in fact, the following is a white image). After converting the image to double, the value is still 0-255, but imwrite must be 0-1, so at this time, more than 1 pixel is treated as 1, and almost all image pixels are white .... In fact, the following image shows a black pixel...
Sw3: Correct after pixel type conversion
Sw4: Correct to convert the pixel value to 0-1
Sw5: Correct after processing pixel values with negative pixel values