Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. void videoProcessing(Mat& camera_input, Mat& original_img)
  2. {
  3.     camera_input.copyTo(original_img);
  4.     // object color
  5.     // H: 25 - 90
  6.     // S: 0  - 255
  7.     // V: 50 - 255
  8.  
  9.     cvtColor(camera_input, camera_input, COLOR_BGR2HSV); //Convert the captured frame
  10.  
  11.     inRange(camera_input, Scalar(25, 0, 50), Scalar(90, 255, 255), camera_input);
  12.     //imshow("camera_ranged", camera_input);  // debug: HSV colorspace image
  13.  
  14.  
  15.     //morphological operations
  16.     erode(camera_input, camera_input, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
  17.     dilate(camera_input, camera_input, getStructuringElement(MORPH_RECT, Size(10, 10)));
  18.     dilate(camera_input, camera_input, getStructuringElement(MORPH_ELLIPSE, Size(10, 10)));
  19.     //imshow("camera_morph", camera_input);  // debug: morph image
  20.  
  21.     vector < vector <Point>> contours;
  22.     findContours(camera_input, contours, RETR_LIST, CHAIN_APPROX_NONE, Point(0, 0));
  23.     Mat drawing = Mat::zeros(camera_input.size(), CV_8UC3);
  24.  
  25.     for (size_t i = 0; i < contours.size(); i++)
  26.     {
  27.         Scalar color = Scalar(121, 113, 96);
  28.         drawContours(drawing, contours, i, color, -1);
  29.         //imshow("contour", drawing);  // debug: show contours
  30.     }
  31.  
  32.  
  33.     gl_obj_decected = false;
  34.     // Find areas
  35.     vector <double> areas;
  36.     for (size_t i = 0; i < contours.size(); i++)
  37.     {
  38.         areas.push_back(contourArea(contours[i], false));
  39.         //printf("--------------contour #%d area:%f\n", i, areas[i]); // debug
  40.         if (areas[i] >= 40000)
  41.         {
  42.             if (counterFound > 3)
  43.             {
  44.                 gl_obj_decected = true;    
  45.             }
  46.         }
  47.     }
  48.  
  49.     // prevent bouncing
  50.     if (counterFound > 3)
  51.     {
  52.         if ((gl_obj_decected == true) && (gl_obj_last_state == false))
  53.         {
  54.             printf("[FOUND] Open solenoid...\n");
  55.             gl_obj_last_state = true;
  56.         }
  57.         else if ((gl_obj_decected == false) && (gl_obj_last_state == true))
  58.         {
  59.             printf("[NOT FOUND] Close solenoid\n");
  60.             gl_obj_last_state = false;
  61.         }
  62.         counterFound = 0;
  63.     }
  64.     counterFound++;
  65.  
  66.     if (gl_obj_decected)
  67.     {
  68.         // show some text on initial image
  69.         printVideoOverlay(original_img);
  70.     }
  71. }
  72.  
  73.  
  74. int main(int argc, char* argv[])
  75. {
  76.     Mat camera_input, camera_input_orig;
  77.     VideoCapture cap;
  78.  
  79.     //----------------------
  80.     int64 e1, e2; // testing clock counts for operations
  81.     double time;
  82.     //----------------------
  83.  
  84.     cap.open(0);
  85.  
  86.     if (!cap.isOpened())
  87.     {
  88.         cerr << "ERROR! Unable to open camera\n";
  89.         system("pause"); //wait for any key press
  90.         return -1;
  91.     }
  92.  
  93.     cout << "Start grabbing" << endl;
  94.  
  95.     for (;;)
  96.     {
  97.         cap.read(camera_input);
  98.         if (camera_input.empty())
  99.         {
  100.             cerr << "ERROR! blank frame grabbed\n";
  101.             system("pause"); //wait for any key press
  102.             return -1;
  103.         }
  104.  
  105.         imshow(camera_live, camera_input); // debug: original capture
  106.  
  107.         e1 = getTickCount(); // test algoritm speed
  108.         videoProcessing(camera_input, camera_input_orig);
  109.         e2 = getTickCount(); // test algoritm speed
  110.         time = (e2 - e1) / getTickFrequency(); // test algoritm speed (total time)
  111.  
  112.         // exit
  113.         if (waitKey(5) >= 0)
  114.         {
  115.             break;
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement