why imwrite make image background become black ?
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (3)
Walter Roberson
le 25 Mar 2013
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 commentaires
Image Analyst
le 25 Mar 2013
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 commentaires
Image Analyst
le 25 Mar 2013
Modifié(e) : Image Analyst
le 25 Mar 2013
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
le 22 Août 2019
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 commentaires
Guillaume
le 22 Août 2019
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
le 22 Août 2019
Can you show the lines of code where youi constructed the image and used imwrite()?
Voir également
Catégories
En savoir plus sur Explore and Edit Images with Image Viewer App dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!