Вопрос

http://sweetclipart.com/multisite/sweetclipart/files/sunglasses_black.png

I have read the png image in MATLAB using [X,map,alpha]=imread('...','png'). Now I want to place this png image on another image. But I want the background color of the read png not to be shown. In the link I want the sunglasses alone to be shown without the 'white' background (Background is another image).

Это было полезно?

Решение 2

You can do it like this:

% Image with alpha channel
[glasses, ~, alpha] = imread('http://sweetclipart.com/multisite/sweetclipart/files/sunglasses_black.png');

% OPTIONAL: Let's rescale it (it's very big!)
glasses = imresize(glasses, 0.1);
alpha = imresize(alpha, 0.1);

% An image of a person (let's put the glasses on the person).
person = imread('http://cinemacao.com/wp-content/uploads/2013/12/Scarlett-CAPA-2.jpg');

% Lets make the alpha MxNx3 (so we can combine it with the RGB channels).
alpha = repmat(alpha, [1 1 3]);

% And convert everything from uint8 to double (to avoid precision issues).
glasses = im2double(glasses);
alpha = im2double(alpha);
person = im2double(person);

% Final image

% Let x,y be the top-left coordinates where we'll put the glasses.
x = 440;
y = 450;

% Let's combine the images.
img3 = person;
img3(y:y+size(glasses,1)-1, x:x+size(glasses,2)-1, :) = ...
    glasses .* alpha + ...
    person(y:y+size(glasses,1)-1, x:x+size(glasses,2)-1, :) .* (1 - alpha);

% An display the result.
imshow(img3);

Result: enter image description here

Другие советы

The alpha channel is opacity, the opposite of transparency.

MATLAB figures support alpha blending via the AlphaData property:

background = uint8(255*rand(size(alpha)));
imshow(background)
hold on
h = imshow(X);
set(h, 'AlphaData', alpha)

Result: enter image description here

Given another image Y and your image X with it's alpha data, you can use alpha to generate a blended image. In you case, alpha has only the values 0 and 255, but in general there can be levels of opacity. In this simple case, again with noise background, but in color:

out = uint8(255*rand(size(X))); % Y
hardMask = repmat(alpha==255,1,1,3);
out(hardMask) = X(hardMask);
imwrite(out,'sunglass_alphaC.png')

It's a big image, so I resized the output here:

enter image description here

If your alpha channel actually had more than two levels of transparency, you could blend with the background:

s = double(alpha)/255;
Yout = uint8((bsxfun(@times,1-s,double(Y)) + bsxfun(@times,s,double(X))));

For grayscale, bsxfun can be replaced with just .- (e.g. s.*double(X)).

I dont know much about Matlab, but seems to be that [X,map,alpha] use "alpha" as the alpha channel; alpha channel means level of transparency. (probably you already know this). Also check if the image itself has the alpha channel set. PNG doesn't recognise "white" as alpha channel by default. In this case, go to your favorite "*shop" software to edit the photo (maybe select with magic tool which will be the background, go to select inverse, copy-paste to a new image previously specifying that the background image will be transparency).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top