mirror of
https://github.com/opencv/opencv.git
synced 2024-12-27 11:28:14 +08:00
aacf188e83
Conflicts: modules/ocl/include/opencv2/ocl/ocl.hpp modules/ocl/src/arithm.cpp modules/ocl/src/build_warps.cpp modules/ocl/src/color.cpp modules/ocl/src/haar.cpp modules/ocl/src/imgproc.cpp modules/ocl/src/split_merge.cpp modules/ocl/test/test_color.cpp samples/cpp/3calibration.cpp samples/cpp/OpenEXRimages_HDR_Retina_toneMapping.cpp samples/cpp/OpenEXRimages_HDR_Retina_toneMapping_video.cpp samples/cpp/Qt_sample/main.cpp samples/cpp/camshiftdemo.cpp samples/cpp/descriptor_extractor_matcher.cpp samples/cpp/distrans.cpp samples/cpp/generic_descriptor_match.cpp samples/cpp/grabcut.cpp samples/cpp/morphology2.cpp samples/cpp/segment_objects.cpp samples/cpp/stereo_calib.cpp samples/cpp/tutorial_code/Histograms_Matching/compareHist_Demo.cpp samples/cpp/tutorial_code/core/mat_mask_operations/mat_mask_operations.cpp samples/cpp/tutorial_code/introduction/display_image/display_image.cpp samples/cpp/tutorial_code/introduction/windows_visual_studio_Opencv/Test.cpp samples/cpp/tutorial_code/objectDetection/objectDetection.cpp samples/cpp/tutorial_code/objectDetection/objectDetection2.cpp samples/cpp/video_dmtx.cpp
114 lines
2.8 KiB
C++
114 lines
2.8 KiB
C++
#include "opencv2/imgproc/imgproc.hpp"
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
#include "opencv2/video/background_segm.hpp"
|
|
#include <stdio.h>
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
using namespace cv;
|
|
|
|
static void help()
|
|
{
|
|
printf("\n"
|
|
"This program demonstrated a simple method of connected components clean up of background subtraction\n"
|
|
"When the program starts, it begins learning the background.\n"
|
|
"You can toggle background learning on and off by hitting the space bar.\n"
|
|
"Call\n"
|
|
"./segment_objects [video file, else it reads camera 0]\n\n");
|
|
}
|
|
|
|
static void refineSegments(const Mat& img, Mat& mask, Mat& dst)
|
|
{
|
|
int niters = 3;
|
|
|
|
vector<vector<Point> > contours;
|
|
vector<Vec4i> hierarchy;
|
|
|
|
Mat temp;
|
|
|
|
dilate(mask, temp, Mat(), Point(-1,-1), niters);
|
|
erode(temp, temp, Mat(), Point(-1,-1), niters*2);
|
|
dilate(temp, temp, Mat(), Point(-1,-1), niters);
|
|
|
|
findContours( temp, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE );
|
|
|
|
dst = Mat::zeros(img.size(), CV_8UC3);
|
|
|
|
if( contours.size() == 0 )
|
|
return;
|
|
|
|
// iterate through all the top-level contours,
|
|
// draw each connected component with its own random color
|
|
int idx = 0, largestComp = 0;
|
|
double maxArea = 0;
|
|
|
|
for( ; idx >= 0; idx = hierarchy[idx][0] )
|
|
{
|
|
const vector<Point>& c = contours[idx];
|
|
double area = fabs(contourArea(Mat(c)));
|
|
if( area > maxArea )
|
|
{
|
|
maxArea = area;
|
|
largestComp = idx;
|
|
}
|
|
}
|
|
Scalar color( 0, 0, 255 );
|
|
drawContours( dst, contours, largestComp, color, FILLED, LINE_8, hierarchy );
|
|
}
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
VideoCapture cap;
|
|
bool update_bg_model = true;
|
|
|
|
help();
|
|
|
|
if( argc < 2 )
|
|
cap.open(0);
|
|
else
|
|
cap.open(std::string(argv[1]));
|
|
|
|
if( !cap.isOpened() )
|
|
{
|
|
printf("\nCan not open camera or video file\n");
|
|
return -1;
|
|
}
|
|
|
|
Mat tmp_frame, bgmask, out_frame;
|
|
|
|
cap >> tmp_frame;
|
|
if(!tmp_frame.data)
|
|
{
|
|
printf("can not read data from the video source\n");
|
|
return -1;
|
|
}
|
|
|
|
namedWindow("video", 1);
|
|
namedWindow("segmented", 1);
|
|
|
|
Ptr<BackgroundSubtractorMOG> bgsubtractor=createBackgroundSubtractorMOG();
|
|
bgsubtractor->setNoiseSigma(10);
|
|
|
|
for(;;)
|
|
{
|
|
cap >> tmp_frame;
|
|
if( !tmp_frame.data )
|
|
break;
|
|
bgsubtractor->apply(tmp_frame, bgmask, update_bg_model ? -1 : 0);
|
|
refineSegments(tmp_frame, bgmask, out_frame);
|
|
imshow("video", tmp_frame);
|
|
imshow("segmented", out_frame);
|
|
int keycode = waitKey(30);
|
|
if( keycode == 27 )
|
|
break;
|
|
if( keycode == ' ' )
|
|
{
|
|
update_bg_model = !update_bg_model;
|
|
printf("Learn background is in state = %d\n",update_bg_model);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|