Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include <opencv2/video/background_segm.hpp>
- using namespace std;
- using namespace cv;
- /** @function Erosion */
- Mat erosion(Mat &src, int erosion_elem)
- {
- Mat erosion_dst;
- int erosion_type;
- int erosion_size = 3;
- if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
- else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
- else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
- Mat element = getStructuringElement( erosion_type,
- Size( 2*erosion_size + 1, 2*erosion_size+1 ),
- Point( erosion_size, erosion_size ) );
- /// Apply the erosion operation
- erode( src, erosion_dst, element );
- //imshow( "Erosion Demo", erosion_dst );
- return erosion_dst;
- }
- /** @function Dilation */
- Mat dilation(Mat &src, int dilation_elem)
- {
- Mat dilation_dst;
- int dilation_type;
- int dilation_size = 3;
- if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
- else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
- else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
- Mat element = getStructuringElement( dilation_type,
- Size( 2*dilation_size + 1, 2*dilation_size+1 ),
- Point( dilation_size, dilation_size ) );
- /// Apply the dilation operation
- dilate( src, dilation_dst, element );
- return dilation_dst;
- }
- Mat open(Mat &src)
- {
- Mat aux = erosion(src,0);
- Mat open_dst = dilation(aux,0);
- //imshow( "Open", open_dst );
- return open_dst;
- }
- int main(int argc, 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; //fg mask fg mask generated by MOG2 method
- Ptr< BackgroundSubtractor> pMOG; //MOG Background subtractor
- Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
- Ptr< BackgroundSubtractorGMG> pGMG; //MOG2 Background subtractor
- pMOG = new BackgroundSubtractorMOG();
- pMOG2 = new BackgroundSubtractorMOG2();
- pGMG = new BackgroundSubtractorGMG();
- VideoCapture stream1(argv[1]);
- Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1,1) );
- //unconditional loop
- while (true) {
- Mat cameraFrame;
- if(!(stream1.read(frame))) //get one frame form video
- break;
- resize(frame, resizeF, Size(frame.size().width/2, frame.size().height/2) );
- pMOG->operator()(resizeF, fgMaskMOG);
- pMOG2->operator()(resizeF, fgMaskMOG2);
- pGMG->operator()(resizeF, fgMaskGMG);
- //*/
- //morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element);
- imshow("Origin", resizeF);
- imshow("MOG", fgMaskMOG);
- imshow("MOG2", fgMaskMOG2);
- imshow("GMG", fgMaskGMG);
- if (waitKey(30) >= 0)
- break;
- }
- /*
- VideoCapture cap(0);
- //MOG(newFrame, foregroundMask);
- backgroundSubtractor = new BackgroundSubtractorMOG(3, 4, 0.8);
- if(!cap.isOpened())
- return -1;
- Mat edges, dil, erod, op, clo;
- namedWindow("Dif", 1);
- namedWindow("Frame",1);
- Mat frame1;
- cap >> frame1;
- /// Apply Histogram Equalization
- double alpha = 2.2;
- int beta = 50;
- while(1)
- {
- Mat frame, teste;
- cap >> frame;
- */
- //frame.convertTo(frame, -1, alpha, beta);
- //frame1 = frame - frame1;
- /*
- cvtColor(frame,edges, CV_BGR2GRAY);
- erod = erosion(frame,0);
- imshow("erodion", erod);
- dil = dilation(frame,0);
- imshow( "dilation", dil );
- op = open(frame);
- imshow( "open", dil );
- */
- //GaussianBlur(edges,edges,Size(7,7),1.5,1.5);
- //Canny(edges,edges,0,30,3);
- //imshow("Dif", frame1);
- // imshow("Frame",frame);
- //frame1 = frame;
- // if(waitKey(30) >= 0) break;
- // }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement