Chris Asadi
Chris Asadi

Reputation: 81

Extract Foreground from background

I have a background image and foreground image. Their point of view are equal. But shadows are different. I am going to extract contours of the legs.

I've applied background difference, but it doesn't work very well. Please someone tell me the good way. You can see the images below.

Thanks.

Upvotes: 2

Views: 500

Answers (1)

Saransh Kejriwal
Saransh Kejriwal

Reputation: 2533

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: result

Upvotes: 1

Related Questions