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

 採用された回答

Walter Roberson
Walter Roberson 2017 年 12 月 4 日

0 投票

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 件のコメント

Margareta Drozdikova
Margareta Drozdikova 2017 年 12 月 4 日
編集済み: Margareta Drozdikova 2017 年 12 月 26 日
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
Walter Roberson
Walter Roberson 2017 年 12 月 5 日
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 2017 年 12 月 5 日
編集済み: Margareta Drozdikova 2017 年 12 月 26 日
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
Walter Roberson
Walter Roberson 2017 年 12 月 5 日
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.
Margareta Drozdikova
Margareta Drozdikova 2017 年 12 月 5 日
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
Walter Roberson
Walter Roberson 2017 年 12 月 5 日
After you image() the bottom image, you need to colormap() the appropriate colormap for it, unless it is an RGB image.
Margareta Drozdikova
Margareta Drozdikova 2017 年 12 月 7 日
and one more question. What does it mean A = double(x==0); ?
Image Analyst
Image Analyst 2017 年 12 月 7 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeModify Image Colors についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by