Created
April 22, 2019 17:10
-
-
Save MareArts/9bfbab373800644a0865cd5bff1eecb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <opencv2/opencv.hpp> | |
#ifdef _DEBUG | |
#pragma comment(lib, "opencv_world401d.lib") | |
#else | |
#pragma comment(lib, "opencv_world401.lib") | |
#endif | |
using namespace cv; | |
using namespace std; | |
using namespace std; | |
using namespace cv; | |
void drawRect(Mat& img, vector< Rect >& v_rect) { | |
for (auto it : v_rect) { | |
rectangle(img, it, CV_RGB(255, 0, 0), 2); | |
} | |
} | |
int main(int, char) | |
{ | |
//Mat absdiff_frame; | |
VideoCapture stream1(0); | |
if (!stream1.isOpened()) { //check if video device has been initialised | |
cout << "cannot open camera 1"; | |
} | |
Mat frame; | |
Mat PR_img; | |
Mat sub_frame; | |
Mat BG_img; | |
int frame_flow = 50; | |
int count_frame = 0; | |
float min_area_percent = 0.1; | |
float min_window_area; | |
while (1) | |
{ | |
if (!(stream1.read(frame))) //get one frame form video | |
break; | |
//frame flow | |
if (BG_img.empty()) { | |
if (count_frame < frame_flow) | |
{ | |
count_frame++; | |
printf("calibration %d\n", count_frame); | |
continue; | |
} | |
else { | |
BG_img = frame.clone(); | |
cvtColor(BG_img, BG_img, COLOR_BGR2GRAY); | |
min_window_area = BG_img.size().area() * min_area_percent/100.0; | |
} | |
} | |
PR_img = frame.clone(); | |
cvtColor(PR_img, PR_img, COLOR_BGR2GRAY); | |
//subtract(old_frame, frame, sub_frame); | |
absdiff(BG_img, PR_img, sub_frame); | |
threshold(sub_frame, sub_frame, 70, 255, THRESH_BINARY); | |
//find contour | |
vector< vector< Point> > contours; | |
vector< Vec4i> hierarchy; | |
findContours(sub_frame.clone(), contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE); | |
//drawContours(frame, contours, -1, CV_RGB(255, 0, 0), 5, 8, hierarchy); | |
//Blob labeling | |
vector< Rect > v_rect; | |
for(auto it:contours){ | |
double area = contourArea(it, false); | |
if (area > min_window_area) | |
{ | |
Rect mr = boundingRect(Mat(it)); | |
v_rect.push_back(mr); | |
//printf("%lf\n", area); | |
} | |
} | |
drawRect(frame, v_rect); | |
// | |
imshow("frame", frame); | |
imshow("sub_frame", sub_frame); | |
imshow("BG_frame", BG_img); | |
if (waitKey(5) >= 0) | |
break; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment