Reputation: 5121
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.
Upvotes: 0
Views: 267
Reputation: 52646
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 :
Upvotes: 1