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

2 votes

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
Hi Akira , thank you very much for your answer but i get this error " Error using size Dimension argument must be a positive integer scalar within indexing range." I have another idea but can't code it because i am beginner with Matlab , we have first to remove black background from Man's image , after that we will have man only , right ? So we can loop over the another image pixel by pixel and add man's pixel to background's image , so we will solve our problem , Understood ? but i can't do that because i am beginner with Matlab.
The error you got is caused by your MATLAB version. The code I posted works the latest version -- R2019b.
If your MATLAB is R2019a or earlier, please replace the following line
Iman = imresize(Iman,size(Ibg,[1 2]));
to
sz = size(Ibg);
Iman = imresize(Iman,sz(1:2));
>So we can loop over the another image pixel by pixel
Yes, I understand what you explained. Actually, it works and can produce the image you want. But, in MATLAB, for-loop takes much longer calculation time than indexing technique. (as for the indexing, please take a look at this article) . So I would recommend using indexsing, as in my previously posted code.
Ahmed Emad
Ahmed Emad le 8 Nov 2019
Thank you very very much , so please tell me the other way that loop over the another image pixel by pixel because i want to understand the other Method ? Glad to hear from you Mr.Akira and if you have enough time , i have another question with only one problem and can't solve it .
KALYAN ACHARJYA
KALYAN ACHARJYA le 8 Nov 2019
@Akira +1
Hello Ahmed, if the problem is solved without loop, which is more efficient code than using loop. You can understand from the code. If you have any specific issue, let us know whch line is not clear to you.
Keep Learning MATLAB!
Regards,
Kalyan Acharjya
Ahmed Emad
Ahmed Emad le 8 Nov 2019
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
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)

Catégories

En savoir plus sur Images dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by