why imwrite make image background become black ?
조회 수: 3 (최근 30일)
이전 댓글 표시
Anyone can tell me why the imwrite make the image background become black, i inserted an image of something with background of white but after using code below i got perfect image but the background become black :
rgbImage = imread('wtmk.png');
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
reconImage=cat(3,redChannel,greenChannel,blueChannel);
imwrite(reconImage,'watermark.png','png');
Anything wrong with my code ?
댓글 수: 0
답변 (3개)
Walter Roberson
2013년 3월 25일
Please check whether size(rgbImage) is exactly the same as size(reconImage), and whether
all(rgbImage(:) == reconImage(:))
Your png file might be returning an alpha layer that you are not writing out to the new file.
댓글 수: 3
Image Analyst
2013년 3월 25일
What is the class of reconImage just before you call imwrite? You didn't somehow convert it (with code not shown) into double did you? Is it still uint8? Can you show the code where you "inserted an image of something with background of white" into your original rgbImage?
By the way, you don't need the third imwrite() argument of 'png' - it figures that out from the filename.
댓글 수: 2
Image Analyst
2013년 3월 25일
편집: Image Analyst
2013년 3월 25일
Try this. Copy, paste, change folder to where your image lives, then run.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in image.
folder = 'C:\Users\iMade\Documents\Temporary';
baseFileName = 'grayscale-icon.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
mask = redChannel == 0 & greenChannel == 0 & blueChannel == 0;
% Display the mask image.
subplot(2, 2, 2);
imshow(mask);
title('Mask Image', 'FontSize', fontSize);
% Make it white where the mask = true.
redChannel(mask) = 255;
greenChannel(mask) = 255;
blueChannel(mask) = 255;
% Create new RGB image with white background.
reconImage=cat(3,redChannel,greenChannel,blueChannel);
% Display the reconstructed image.
subplot(2, 2, 3);
imshow(reconImage);
title('Masked Image', 'FontSize', fontSize);
% Save it to disk.
imwrite(reconImage,'watermark.png');
% Recall it to see that it saved okay.
rgbImage2 = imread('watermark.png');
subplot(2, 2, 4);
imshow(rgbImage2);
title('Image Recalled From Disk', 'FontSize', fontSize);
Serge Mooijman
2019년 8월 22일
Interesting, in my case imwrite delivered black (/white) images startng from uint8 images because the had 4 bands (RGB and an infrared band). The problem was only resolved when I excluded the 4th band or turned the pics in grayscale first.
댓글 수: 2
Guillaume
2019년 8월 22일
Not many image formats support 4 colour channels. Some formats, such as png discussed here, support a 4th channel but that is exclusively for transparency information. Hence if you save an image with 4 colour channels as png, that 4th infrared channel will be intepreted as transparency. If that channel is full of 0s, that would mean that the only thing displayed is the background, which depending on your image viewer may be black.
Image Analyst
2019년 8월 22일
Can you show the lines of code where youi constructed the image and used imwrite()?
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!