0

Given below is the code that I am using to find the difference between 2 images.

#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include<iostream>

int main()
{
char a,b;
cv::Mat frame;
cv::Mat frame2;
VideoCapture cap(0);
if(!cap.isOpened())
{
    cout<<"Camera is not connected"<<endl;
    getchar();
    exit(0);

}
Mat edges;
namedWindow("Camera Feed",1);
cout<<"Ready for background?(y/Y)"<<endl;
cin>>a;
if(a=='y'||a=='Y')
{
cap>>frame;
cv::cvtColor(frame,frame,CV_RGB2GRAY);
cv::GaussianBlur(frame,frame,cv::Size(51,51),2.00,0,BORDER_DEFAULT);
}
cv::waitKey(5000);
cout<<"Ready for foreground?(y/Y)"<<endl;
cin>>b;
if(b=='y'||b=='Y')
{
cap>>frame2;
cv::cvtColor(frame2,frame2,CV_RGB2GRAY);
cv::GaussianBlur(frame2,frame2,cv::Size(51,51),2.00,0,BORDER_DEFAULT);
}
cv::Mat frame3;
cv::absdiff(frame,frame2,frame3);


imwrite("img_bw.jpg",frame3);

return 0;
}

The output is something like this. I wanted to know if there is any way I can draw something like an outline around the body. Thanks.

praxmon
  • 5,009
  • 22
  • 74
  • 121

1 Answers1

1

I just tried the following method.

First dilated the grayscale image, then applied adaptive thresholding on the image.

Later found contours in the image, and on the assumption that your body will be biggest blob in the image, drew outline for the biggest blob.

import cv2
import numpy as np

img = cv2.imread('sofqn.jpg')
gray = cv2.imread('sofqn.jpg',0)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10,10))
gray = cv2.dilate(gray,kernel)

thresh = cv2.adaptiveThreshold(gray,255,0,1,11,2)

cont,hier = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
max_area = -1
best_cnt = None
for cnt in cont:
    area = cv2.contourArea(cnt)
    if area > max_area:
        max_area = area
        best_cnt = cnt

cv2.drawContours(img,[best_cnt],0,[0,255,0],2)

Below is the result :

enter image description here

Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
  • W-O-W!! Thanks!! And do you know how I can improve the result of my program? That is how to differentiate between 2 images so that the output is completely black and white? Thanks again!! – praxmon Jul 13 '12 at 05:45
  • I dont have a readymade answer,if you can share links for two images, i can have a try. – Abid Rahman K Jul 13 '12 at 05:50
  • Like in my situation, one image of the background and the other of someone in front of that background. The images I get are from my webcam. – praxmon Jul 13 '12 at 05:54
  • What's a kernel and what are cont,hier and thresh. I'll have to port it to c++ so need their data types. – praxmon Jul 13 '12 at 06:00
  • 1
    when you find contours, you get all the contours. Then you take each contour for calculation, ie cnt (just a name). It is in Python. If you are using C++, try this tutorial to know how to access individual contours : http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html#bounding-rects-circles – Abid Rahman K Jul 13 '12 at 08:58