Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void videoProcessing(Mat& camera_input, Mat& original_img)
- {
- camera_input.copyTo(original_img);
- // object color
- // H: 25 - 90
- // S: 0 - 255
- // V: 50 - 255
- cvtColor(camera_input, camera_input, COLOR_BGR2HSV); //Convert the captured frame
- inRange(camera_input, Scalar(25, 0, 50), Scalar(90, 255, 255), camera_input);
- //imshow("camera_ranged", camera_input); // debug: HSV colorspace image
- //morphological operations
- erode(camera_input, camera_input, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
- dilate(camera_input, camera_input, getStructuringElement(MORPH_RECT, Size(10, 10)));
- dilate(camera_input, camera_input, getStructuringElement(MORPH_ELLIPSE, Size(10, 10)));
- //imshow("camera_morph", camera_input); // debug: morph image
- vector < vector <Point>> contours;
- findContours(camera_input, contours, RETR_LIST, CHAIN_APPROX_NONE, Point(0, 0));
- Mat drawing = Mat::zeros(camera_input.size(), CV_8UC3);
- for (size_t i = 0; i < contours.size(); i++)
- {
- Scalar color = Scalar(121, 113, 96);
- drawContours(drawing, contours, i, color, -1);
- //imshow("contour", drawing); // debug: show contours
- }
- gl_obj_decected = false;
- // Find areas
- vector <double> areas;
- for (size_t i = 0; i < contours.size(); i++)
- {
- areas.push_back(contourArea(contours[i], false));
- //printf("--------------contour #%d area:%f\n", i, areas[i]); // debug
- if (areas[i] >= 40000)
- {
- if (counterFound > 3)
- {
- gl_obj_decected = true;
- }
- }
- }
- // prevent bouncing
- if (counterFound > 3)
- {
- if ((gl_obj_decected == true) && (gl_obj_last_state == false))
- {
- printf("[FOUND] Open solenoid...\n");
- gl_obj_last_state = true;
- }
- else if ((gl_obj_decected == false) && (gl_obj_last_state == true))
- {
- printf("[NOT FOUND] Close solenoid\n");
- gl_obj_last_state = false;
- }
- counterFound = 0;
- }
- counterFound++;
- if (gl_obj_decected)
- {
- // show some text on initial image
- printVideoOverlay(original_img);
- }
- }
- int main(int argc, char* argv[])
- {
- Mat camera_input, camera_input_orig;
- VideoCapture cap;
- //----------------------
- int64 e1, e2; // testing clock counts for operations
- double time;
- //----------------------
- cap.open(0);
- if (!cap.isOpened())
- {
- cerr << "ERROR! Unable to open camera\n";
- system("pause"); //wait for any key press
- return -1;
- }
- cout << "Start grabbing" << endl;
- for (;;)
- {
- cap.read(camera_input);
- if (camera_input.empty())
- {
- cerr << "ERROR! blank frame grabbed\n";
- system("pause"); //wait for any key press
- return -1;
- }
- imshow(camera_live, camera_input); // debug: original capture
- e1 = getTickCount(); // test algoritm speed
- videoProcessing(camera_input, camera_input_orig);
- e2 = getTickCount(); // test algoritm speed
- time = (e2 - e1) / getTickFrequency(); // test algoritm speed (total time)
- // exit
- if (waitKey(5) >= 0)
- {
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement