Hi, I have a white circle (255) and background is black (0), Picture is saved as png and is in grayscale, size 513x513. I need one thing. The white circle has to be transparent and black part no. How can I do this?
x=imread('filter.png');
[M,N]=size(x);
A=zeros(M,N);
for i=1:M
for j=1:N
if (x(i,j)==255)
A(i,j)=1;
end
end
end
imwrite(x,'filter.png','Alpha',A)
but i dont know if it is OK. I have this picture lay on another picture, so in white circle I will see second picture and in black part I will see black. Thanks for any help

 Accepted Answer

Your basic technique was correct, but your code can be written much more efficiently.
x = imread('filter.png');
A = double(x==0);
imwrite(x, 'newfilter.png', 'Alpha', A);
"I have this picture lay on another picture,"
If you are reading the new image in using imread() then you are probably not handling the alpha correctly at that time:
image(ArrayForImageToGoOnTheBottom);
[newfilter, map, filter_alpha] = imread('newfilter.png');
image(newfilter, 'AlphaData', filter_alpha);

8 Comments

Margareta Drozdikova
Margareta Drozdikova on 4 Dec 2017
Edited: Margareta Drozdikova on 26 Dec 2017
Thanks for answer. I have created the newfilter.png but I have still problem lay this newfilter on color picture. The picture on the bottom is uint8 513X513. I have attached the result. Thanks for help
image(newfilter(:,:,[1 1 1]), 'AlphaData', filter_alpha);
the newfilter(:,:,[1 1 1]) effectively converts the greyscale array newfilter into an RGB array for drawing purposes.
Margareta Drozdikova
Margareta Drozdikova on 5 Dec 2017
Edited: Margareta Drozdikova on 26 Dec 2017
Thanks. I did it, but the result is weird. The left part is rgb picture and on this picture I need to lay transparent circle. I did it, how you said. (Thanks :) ) but It doesnt look goog. In the circle I have to have color map
What the above suggests to me is that the picture to the left is either much smaller than the filter image, or else that the picture on the left has been created with a different coordinate system.
If the problem is one of coordinate systems, then you can get this almost right by putting up the color graph first and then using
ax = gca;
XL = get(ax, 'XLim');
YL = get(ax, 'YLim');
image(newfilter(:,:,[1 1 1]), 'AlphaData', filter_alpha, 'XData', XL, 'YData', YL );
This is not exactly right because a half-pixel border around the image will be outside of the existing boundaries -- but try it and see if it is close enough before I worry about calculating the formula for the correction to put the pixel centers in the right locations.
Thanks. It doesnt work correct. The size of color picture is 513x513 uint8 the same it is at newfilter. I dont know. I will think more about it
After you image() the bottom image, you need to colormap() the appropriate colormap for it, unless it is an RGB image.
and one more question. What does it mean A = double(x==0); ?
You are checking if x is zero. The result of x==0 will be a logical variable with values of true or false.
Using double() casts the data type from logical to double, so that now A will be a double with a value of 0 or 1, NOT a logical with a value of true or false.

Sign in to comment.

More Answers (0)

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!