If you're interested solely in extracting the image of a leg, then HSV-based skin detection, followed by repetitive dilation should produce better results.
See code below:
#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<stdio.h>
using namespace std;
using namespace cv;
int main()
{
Mat b=imread("b2.png");//back
Mat f=imread("f2.png");//fore
Mat hsv_th;
cvtColor(b,b,CV_BGR2HSV);
cvtColor(f,f,CV_BGR2HSV);
inRange(f,Scalar(0,100,0),Scalar(100,255,100),hsv_th);
dilate(hsv_th,hsv_th,cv::Mat());
dilate(hsv_th,hsv_th,cv::Mat());
dilate(hsv_th,hsv_th,cv::Mat());
dilate(hsv_th,hsv_th,cv::Mat());
for(;;)
{
imshow("fore",f);
imshow("hsv",hsv_th);
char c=waitKey(10);
if(c=='b')//break when 'b' is pressed
{
break;
}
}
return 0;
}
Output image:
