2

I am trying to remove the white background on my png picture I get from a code I created. This is the picture I get: enter image description here

I want to make the white background transparent, because I have several of those images that I want to combine using imfuse.

What I do is this (my picture is called 'A1.png'):

A1=imread('A1.png');
D=zeros(size(A1));
D(A1==255) =1;
imwrite(A1,'A11.png','alpha',D);

However I get an error like this Error using writepng>parseInputs (line 349) The value of 'alpha' is invalid. Expected input to be of size 829x600 when it is actually size 829x600x3.

829x600x3 uint8 is the size of A1. I understand I need to get rid of the x3 thing. But i don't know if it's when I save the pic or earlier in my code.

What do you guys think?

Community
  • 1
  • 1
  • Both of the images are of different sizes. Also, the aspect ratios between the two boxes are different. There is no way you can successfully overlap these images so that they're perfectly aligned. Resizing the images so that they're the same dimensions won't work either due to the different aspect ratios. You're [SOL](http://www.urbandictionary.com/define.php?term=SOL) here... unless you actually have the RAW images themselves. I suspect that you used `imshow` for both images then saved the figure to file. Don't do that. Save the **raw** images, then overlap them. – rayryeng Apr 15 '15 at 21:46
  • I don't understand why they have different aspect ratio, cause I use the same code to create them, maybe I can send it to you if you wanna have a look. What if when I create the image using patch in my code, I specify axis length, then they would be the same AR and size right ? – Dorian Kartalovski Apr 16 '15 at 00:23
  • If you could show the code too that would be great. The images are different sized so you can't overlay them if you wanted to. – rayryeng Apr 16 '15 at 00:24
  • I don't have the code on my right now, cause I am at home but i can show it tomorrow, the thing is I use an excel file with some data, so it's not directly runable unless you have the excel sheet – Dorian Kartalovski Apr 16 '15 at 00:25

3 Answers3

2

You need simply create D with one less dimension. Here is the code

D = zeros( size(A(:,:,1)) );
D( all( A==255, 3 ) ) = 1; 
imwrite(A,'A11.png','alpha',D);
swapnil gandhi
  • 816
  • 1
  • 20
  • 38
Shai
  • 111,146
  • 38
  • 238
  • 371
1

The following MATLAB codes can remove a white background (i.e. write data to a new image with transparent background):

% name the input and output files
im_src = 'im0.png';
im_out = 'out.png';

% read in the source image (this gives an m * n * 3 array)
RGB_in = imread( im_src );
[m, n] = size( RGB_in(:,:,1) );

% locate the pixels whose RGB values are all 255 (white points ? --to be verified)
idx1 = ones(m, n);
idx2 = ones(m, n);
idx3 = ones(m, n);
idx1( RGB_in(:,:,1) == 255 ) = 0;
idx2( RGB_in(:,:,2) == 255 ) = 0;
idx3( RGB_in(:,:,3) == 255 ) = 0;

% write to a PNG file, 'Alpha' indicates the transparent parts
trans_val = idx1 .* idx2 .* idx3;
imwrite( RGB_in, im_out, 'png', 'Alpha', trans_val );

Voilà, hope that helps!

Zhi Q.
  • 71
  • 3
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read this [how-to-answer](http://stackoverflow.com/help/how-to-answer) for providing quality answer. – thewaywewere Jun 11 '17 at 15:52
0

Here is how I did it. I have a png that does not have an alpha channel which is why I had a hard time making it transparent using the code presented above.

I managed to make it transparent by first adding an alpha channel then reading it back in and used the code above.

[RGBarray,map,alpha] = imread('image1.png'); % if alpha channel is empty the next 2 lines add it

imwrite(RGBarray, 'image1_alpha.png', 'png', 'Alpha', ones(size(RGBarray,1),size(RGBarray,2)) )
[I,map,alpha] = imread('image1_alpha.png');

I2 = imcrop(I,[284.5 208.5 634 403]);
alpha = imcrop(alpha,[284.5 208.5 634 403]);

alpha( all( I2==255, 3 ) ) = 1; 
imwrite(I2,'image1_crop.png','alpha',alpha);
Pat
  • 627
  • 1
  • 9
  • 24