MATLAB Answers

I Made

why imwrite make image background become black ?

Asked by I Made
on 25 Mar 2013

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);

Anything wrong with my code ?


2 Answers

Answer by Walter Roberson
on 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.


It's the same sir, rgbImage <128x128x3 uint8 > same as reconImage <128x128x3 uint8 >, how do i know if i returning alpha layer like what you sid, i just thought the code work perfect.

Can you upload your image? The exact PNG file you're using?

Answer by Image Analyst
on 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.


i mean that i insert an image with white bg color, is the code

rgbImage = imread('wtmk.png');

nothing more, that's all my code in my question. Maybe you can try it

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);
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);
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);
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.
% Display the reconstructed image.
subplot(2, 2, 3);
title('Masked Image', 'FontSize', fontSize);
% Save it to disk.
% Recall it to see that it saved okay.
rgbImage2 = imread('watermark.png');
subplot(2, 2, 4);
title('Image Recalled From Disk', 'FontSize', fontSize);

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!