Advertisement
Rapela

OpenCv Some Tests

Mar 1st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include "opencv2/imgproc/imgproc.hpp"
  2. #include "opencv2/highgui/highgui.hpp"
  3. #include <opencv2/video/background_segm.hpp>
  4. using namespace std;
  5. using namespace cv;
  6.  
  7. /**  @function Erosion  */
  8. Mat erosion(Mat &src, int erosion_elem)
  9. {
  10.   Mat erosion_dst;
  11.   int erosion_type;
  12.   int erosion_size = 3;
  13.   if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
  14.   else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
  15.   else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
  16.  
  17.   Mat element = getStructuringElement( erosion_type,
  18.                                        Size( 2*erosion_size + 1, 2*erosion_size+1 ),
  19.                                        Point( erosion_size, erosion_size ) );
  20.   /// Apply the erosion operation
  21.   erode( src, erosion_dst, element );
  22.   //imshow( "Erosion Demo", erosion_dst );
  23.  
  24.   return erosion_dst;
  25. }
  26.  
  27. /** @function Dilation */
  28.  
  29. Mat dilation(Mat &src, int dilation_elem)
  30. {
  31.   Mat dilation_dst;
  32.   int dilation_type;
  33.   int dilation_size = 3;
  34.   if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
  35.   else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
  36.   else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
  37.  
  38.   Mat element = getStructuringElement( dilation_type,
  39.                                        Size( 2*dilation_size + 1, 2*dilation_size+1 ),
  40.                                        Point( dilation_size, dilation_size ) );
  41.   /// Apply the dilation operation
  42.   dilate( src, dilation_dst, element );
  43.  
  44.   return dilation_dst;
  45. }
  46.  
  47. Mat open(Mat &src)
  48. {
  49.   Mat aux = erosion(src,0);
  50.   Mat open_dst = dilation(aux,0);
  51.   //imshow( "Open", open_dst );
  52.  
  53.   return open_dst;
  54. }
  55.  
  56.  
  57.  
  58. int main(int argc, char** argv)
  59. {
  60.  
  61.  Mat frame; //current frame  
  62.  Mat resizeF;  
  63.  Mat fgMaskMOG; //fg mask generated by MOG method  
  64.  Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method  
  65.  Mat fgMaskGMG; //fg mask fg mask generated by MOG2 method  
  66.    
  67.  Ptr< BackgroundSubtractor> pMOG; //MOG Background subtractor  
  68.  Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor  
  69.  Ptr< BackgroundSubtractorGMG> pGMG; //MOG2 Background subtractor  
  70.    
  71.  
  72.  
  73.  pMOG = new BackgroundSubtractorMOG();  
  74.  pMOG2 = new BackgroundSubtractorMOG2();  
  75.  pGMG = new BackgroundSubtractorGMG();  
  76.    
  77.  
  78.  VideoCapture stream1(argv[1]);      
  79.  
  80.  Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1,1) );    
  81.  
  82.  //unconditional loop    
  83.  while (true) {    
  84.   Mat cameraFrame;    
  85.   if(!(stream1.read(frame))) //get one frame form video    
  86.    break;  
  87.    
  88.  
  89.   resize(frame, resizeF, Size(frame.size().width/2, frame.size().height/2) );  
  90.   pMOG->operator()(resizeF, fgMaskMOG);  
  91.   pMOG2->operator()(resizeF, fgMaskMOG2);  
  92.   pGMG->operator()(resizeF, fgMaskGMG);  
  93.   //*/
  94.   //morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element);  
  95.  
  96.    
  97.  
  98.  
  99.   imshow("Origin", resizeF);  
  100.   imshow("MOG", fgMaskMOG);  
  101.   imshow("MOG2", fgMaskMOG2);  
  102.   imshow("GMG", fgMaskGMG);  
  103.    
  104.  
  105.   if (waitKey(30) >= 0)    
  106.    break;    
  107.  }  
  108.  
  109.  
  110.  
  111.     /*
  112.     VideoCapture cap(0);
  113.     //MOG(newFrame, foregroundMask);
  114.     backgroundSubtractor = new BackgroundSubtractorMOG(3, 4, 0.8);
  115.     if(!cap.isOpened())
  116.         return -1;
  117.  
  118.     Mat edges, dil, erod, op, clo;
  119.     namedWindow("Dif", 1);
  120.     namedWindow("Frame",1);
  121.     Mat frame1;
  122.     cap >> frame1;
  123.     /// Apply Histogram Equalization
  124.     double alpha = 2.2;
  125.     int beta = 50;
  126.     while(1)
  127.     {
  128.         Mat frame, teste;
  129.         cap >> frame;
  130.     */
  131.         //frame.convertTo(frame, -1, alpha, beta);
  132.         //frame1 = frame - frame1;
  133.         /*
  134.         cvtColor(frame,edges, CV_BGR2GRAY);
  135.         erod = erosion(frame,0);
  136.         imshow("erodion", erod);
  137.         dil = dilation(frame,0);
  138.         imshow( "dilation", dil );
  139.         op = open(frame);
  140.         imshow( "open", dil );
  141.         */
  142.         //GaussianBlur(edges,edges,Size(7,7),1.5,1.5);
  143.         //Canny(edges,edges,0,30,3);
  144.         //imshow("Dif", frame1);
  145. //      imshow("Frame",frame);
  146.         //frame1 = frame;
  147. //      if(waitKey(30) >= 0) break;
  148. //  }
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement