MATLAB Answers


Add object to an image

Asked by Ahmed Emad on 7 Nov 2019 at 16:57
Latest activity Commented on by Akira Agata
on 11 Nov 2019 at 0:02
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.


KALYAN ACHARJYA on 7 Nov 2019 at 17:09
Sir add means?
Ahmed Emad on 8 Nov 2019 at 5:50
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.

1 Answer

Akira Agata
Answer by Akira Agata
on 8 Nov 2019 at 0:09
 Accepted Answer

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


Ahmed Emad on 8 Nov 2019 at 15:42
Everything is great and thanks to Mr.Akira, he help me a lot but i want the other method too using loop over the other image pixel by pixel and add man to other image , Can you do that ? Thank you too @Kalyan
Ahmed Emad on 8 Nov 2019 at 18:48
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)
%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
figure, imshow(uint8(newimg));
Akira Agata
on 11 Nov 2019 at 0:02
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

Sign in to comment.