Add object to an image

3 views (last 30 days)
Ahmed Emad
Ahmed Emad on 7 Nov 2019
Commented: Akira Agata on 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 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 7 Nov 2019
Sir add means?
Ahmed Emad
Ahmed Emad on 8 Nov 2019
Edited: Ahmed Emad on 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

Sign in to comment.

Accepted Answer

Akira Agata
Akira Agata on 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 Comments
Ahmed Emad
Ahmed Emad on 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 on 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)

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!