# image - Background on leg stays after subtraction in MATLAB

The picture on the left is my background image and the one on the right is the original image:

After doing subtraction,im having trouble in removing the background on the ankle:

The purpose is to get the foreground only, does anyone have an idea? Shown below is the code on how I carry out my background subtraction as well as display it. Thanks.

`` I = (imread('frame9.jpg'));figure(1),imshow(I);bg =(imread('background.jpg'));figure(2),imshow(bg);Ip = bg-I;figure(3),imshow(Ip);``

The following solution is incomplete, but it can give you a lead:

I used the following operations to create a mask:

• Shrink images by factor of x2 in each axis (reduce some noise).
• Compute absolute difference of shrank images.
• Convert to binary image, using threshold (threshold is found using `graythresh`).
• Dilate binary image.
• Resize binary back to full size.
• Mask pixels from foreground image by multiplying it with binary mask.

Check the following code:

``````%Read two images from hosting site.

%Convet from uint8 to double (pixels range [0, 1])
twoImages = im2double(twoImages);

B = twoImages(7:332, 23:599, :);
I = twoImages(7:332, 706:1282, :);

%Shrink images by factor of x2 in each axis.
sB = imresize(B, 0.5);
sI = imresize(I, 0.5);

%Diference of shrank images.
sD = abs(sB - sI);

%Convert to binary image, using threshold.
level = graythresh(sD);
sBW = im2bw(sD, level);

%Dilate binary image
se = strel('disk', 6);
sBW = imdilate(sBW, se);

%Resize binary image to size of I (create binary mask).
BW = imresize(sBW, [size(I,1), size(I,2)]);