Skip to content

Instantly share code, notes, and snippets.

@sinannar
Created October 18, 2014 12:02
Show Gist options
  • Save sinannar/56a6858495dffd72735c to your computer and use it in GitHub Desktop.
Save sinannar/56a6858495dffd72735c to your computer and use it in GitHub Desktop.
convex hull problem :D
//
// main.cpp
// BackgroundSubstraction
//
// Created by Sinan on 18/10/14.
// Copyright (c) 2014 Sinan. All rights reserved.
//
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
using namespace std;
using namespace cv;
Mat findConvexHull(Mat img,int thr);
int main(int argc, const char * argv[]) {
Mat frame; //current frame
Mat resizeF;
Mat fgMaskMOG; //fg mask generated by MOG method
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Mat fgMaskGMG;
Mat fgMaskBCK;
//BackgroundSubtractor
Ptr< BackgroundSubtractor> pBCK; //MOG Background subtractor
Ptr< BackgroundSubtractor> pMOG; //MOG Background subtractor
Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
Ptr< BackgroundSubtractorGMG> pGMG; //MOG2 Background subtractor
pBCK = new BackgroundSubtractor();
pMOG = new BackgroundSubtractorMOG();
pMOG2 = new BackgroundSubtractorMOG2();
pGMG = new BackgroundSubtractorGMG();
VideoCapture stream1(0);
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1,1) );
do{
namedWindow("CHMOG");
namedWindow("CHMOG2");
namedWindow("CHGMG");
namedWindow("CHBCK");
int t1 = 10;
int t2 = 10;
int t3 = 10;
int t4 = 10;
createTrackbar("lrate", "CHMOG", &t1,100);
createTrackbar("lrate", "CHMOG2", &t2,100);
createTrackbar("lrate", "CHGMG", &t3,100);
createTrackbar("lrate", "CHBCK", &t4,100);
Mat cameraFrame;
if(!(stream1.read(frame))) //get one frame form video
break;
resize(frame, resizeF, Size(frame.size().width/4, frame.size().height/4) );
pMOG->operator()(resizeF, fgMaskMOG,(double)t1/100);
pMOG2->operator()(resizeF, fgMaskMOG2,(double)t2/100);
pGMG->operator()(resizeF, fgMaskGMG,(double)t3/100);
pGMG->operator()(resizeF, fgMaskBCK,(double)t4/100);
//morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element);
Mat grayFgMaskMOG;
Mat grayFgMaskMOG2;
Mat grayFgMaskGMG;
Mat grayFgMaskBCK;
//cvtColor( fgMaskMOG, grayFgMaskMOG, CV_BGR2GRAY );
//cvtColor( fgMaskMOG2, grayFgMaskMOG2, CV_BGR2GRAY );
//cvtColor( fgMaskGMG, grayFgMaskGMG, CV_BGR2GRAY );
blur( fgMaskMOG, grayFgMaskMOG, Size(3,3) );
blur( fgMaskMOG2, grayFgMaskMOG2, Size(3,3) );
blur( fgMaskGMG, grayFgMaskGMG, Size(3,3) );
blur( fgMaskBCK, grayFgMaskBCK, Size(3,3) );
imshow("Origin", resizeF);
int thr1=78;
int thr2=78;
int thr3=78;
int thr4=78;
createTrackbar("thr", "CHMOG", &thr1,255);
createTrackbar("thr", "CHMOG2", &thr2,255);
createTrackbar("thr", "CHGMG", &thr3,255);
createTrackbar("thr", "CHBCK", &thr4,255);
Mat CHgrayFgMaskMOG = findConvexHull(grayFgMaskMOG,thr1);
Mat CHgrayFgMaskMOG2 = findConvexHull(grayFgMaskMOG2,thr2);
Mat CHgrayFgMaskGMG = findConvexHull(grayFgMaskGMG,thr3);
Mat CHgrayFgMaskBCK = findConvexHull(grayFgMaskBCK,thr3);
imshow("CHMOG", CHgrayFgMaskMOG);
imshow("CHMOG2", CHgrayFgMaskMOG2);
imshow("CHGMG", CHgrayFgMaskGMG);
imshow("CHBCK", CHgrayFgMaskBCK);
}while(waitKey(30)!=27);
return 0;
}
Mat findConvexHull(Mat img,int thr){
Mat src_copy = img.clone();
Mat threshold_output;
RNG rng(12345);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
threshold( img, threshold_output, thr, 255, THRESH_BINARY );
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector<vector<Point> >hull( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ convexHull( Mat(contours[i]), hull[i], false ); }
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
drawContours( drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
}
return drawing;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment