Add object to an image

11 vues (au cours des 30 derniers jours)
Ahmed Emad
Ahmed Emad le 7 Nov 2019
Commenté : Akira Agata le 11 Nov 2019
Hi all , now i have an object and want to add it to an image using for loop .
For example : i have 'man' as 'img1' a object and want to add him to different image that have background 'img2' and result must be the same as 'img3'.
Please , help me if you can and thank you very much.
  2 commentaires
KALYAN ACHARJYA
KALYAN ACHARJYA le 7 Nov 2019
Sir add means?
Ahmed Emad
Ahmed Emad le 8 Nov 2019
Modifié(e) : Ahmed Emad le 8 Nov 2019
Let me tell you everything , i have an image called 'img.jpg' that contain man with black background. So i need to remove background first , then we will have the man only , right ? after that want to add this man to the another image that called 'img2.jpg' and result must be the same as 'img3.jpg'. i have good idea but can't apply it , i can loop over the image with man and black background and if pixel == 0 then skip it to remove background . after that want to loop over the another image pixel by pixel to add man's pixel to another image , Understood? but my problem that i am bignner with Matlab and can't code it very well , i hope to help me , if you can and thank you very much

Connectez-vous pour commenter.

Réponse acceptée

Akira Agata
Akira Agata le 8 Nov 2019
Like this?
% Read background and man image
Ibg = imread('img2.jpg');
Iman = imread('img.jpg');
% Adjust man's image size to the background image
Iman = imresize(Iman,size(Ibg,[1 2]));
% Create mask
BWmask = Iman(:,:,1) > 12;
BWmask = cat(3,BWmask,BWmask,BWmask);
% Add masked man to the background
Ibg(BWmask) = Iman(BWmask);
% Show the result
figure
imshow(Ibg)
outputImg.png
  7 commentaires
Ahmed Emad
Ahmed Emad le 8 Nov 2019
I have another question Mr. Akira Agata I have this image 'jump.jpg' and want to extract man from this image to have image 'img.jpg' , and this is my own code but still have noise and can't give man his own colors too , How to solve that ?
I = imread('jump.jpg');
x = rgb2gray(I);
BW = edge(x,'canny');
se = strel('square', 5);
BW = imdilate(BW,se);
BW = ~BW;
[L, num] = bwlabel(BW);
%RGB = label2rgb(L);
[h, w, ~] = size(I);
Ratio = h * w * 0.00000000000001;
d = zeros(size(I));
newimg = zeros(size(I));
for i=1:num
x = uint8(L==i);
f = sum(sum(x==1));
if(f < Ratio)
continue;
end
%Matrix contain all objects with his own colors , we just need another
%matrix and want to append every object to it
d(:,:,1) = uint8(x).*I(:,:,1);
d(:,:,2) = uint8(x).*I(:,:,2);
d(:,:,3) = uint8(x).*I(:,:,3);
for j=1:h
for k=1:w
if(d(j,k)~=0) %because i want to extract man with white background but still doesn't work
newimg(j,k,1)=d(j,k,1);
newimg(j,k,2)=d(j,k,2);
newimg(j,k,3)=d(j,k,3);
end
end
end
end
figure, imshow(uint8(newimg));
Akira Agata
Akira Agata le 11 Nov 2019
How about the following?
Regarding the "Color Thresholder App", the following help page will be useful.
I = imread('jump.jpg');
% Threshold was adjusted by usign "Color Thresholder App"
Ilab = rgb2lab(I);
BW = Ilab(:,:,3) > -20;
% Extract only target ROI
BW = imclearborder(BW);
BW = bwareafilt(BW,1);
% Making a binary mask image which has RGB dimension
BWmask = cat(3,BW,BW,BW);
% Multiply to the original image and obtain the result
I2 = immultiply(I,BWmask);
% Show the result
figure
imshow(I2)

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by