Created
April 28, 2019 11:25
-
-
Save MareArts/245059bb5f730559eee4e1e4f50f62bd 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; | |
void draw_rect(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(1); | |
if (!stream1.isOpened()) { //check if video device has been initialised | |
cout << "cannot open camera 1"; | |
} | |
Mat frame; | |
Mat PR_img_gray; | |
Mat sub_frame; | |
Mat BG_img_gray; | |
int frame_flow = 50; | |
int count_frame = 0; | |
float min_area_percent = 0.1; | |
float min_window_area; | |
float alpha = 0.1; | |
float beta = (1.0 - alpha); | |
namedWindow("frame", 0); | |
namedWindow("sub_frame", 0); | |
namedWindow("BG_frame", 0); | |
while (1) | |
{ | |
if (!(stream1.read(frame))) //get one frame form video | |
break; | |
//frame flow | |
if (BG_img_gray.empty()) { | |
cvtColor(frame.clone(), BG_img_gray, COLOR_BGR2GRAY); | |
min_window_area = BG_img_gray.size().area() * min_area_percent / 100.0; | |
} | |
cvtColor(frame.clone(), PR_img_gray, COLOR_BGR2GRAY); | |
addWeighted(PR_img_gray, alpha, BG_img_gray, beta, 0.0, BG_img_gray); | |
if (count_frame < frame_flow) | |
{ | |
count_frame++; | |
printf("calibration %d\n", count_frame); | |
continue; | |
} | |
//subtract(old_frame, frame, sub_frame); | |
absdiff(BG_img_gray, PR_img_gray, 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); | |
} | |
} | |
draw_rect(frame, v_rect); | |
// | |
imshow("frame", frame); | |
imshow("sub_frame", sub_frame); | |
imshow("BG_frame", BG_img_gray); | |
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