Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<opencv/cv.hpp>
- #include <opencv2/core/core.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <iostream>
- #include <vector>
- #include <stdio.h>
- using namespace cv;
- using namespace std;
- int main(int argc, char* argv[])
- {
- VideoCapture cap; // open the webcam for reading
- if ( !cap.open(0) ) // if not success, exit program
- {
- cout << "Cannot open webcam" << endl;
- return -1;
- }
- namedWindow("Original Video",CV_WINDOW_AUTOSIZE); //create a window called "Original Video"
- namedWindow("Object Detection",CV_WINDOW_AUTOSIZE); //create a window called "Object detection and changed colour"
- // double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
- // double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
- //Specifying the range of HSV.
- int hueLow = 0; //13
- int saturationLow = 110; // 160
- int valueLow = 110; //160
- int hueHigh = 21; //22
- int saturationHigh = 255;
- int valueHigh = 255;
- while(1)
- {
- Mat frame, convertedBGR;
- bool bSuccess = cap.read(frame); // read a new frame from video
- Mat hsvFrame = Mat::zeros(frame.rows, frame.cols, frame.type());
- cvtColor(frame,hsvFrame, CV_BGR2HSV); // Converting a frame to HSV color space
- if (!bSuccess) //if not success, break loop
- {
- cout << "Cannot read the frame from video file" << endl;
- break;
- }
- Mat onlyOrange = Mat::zeros(frame.rows, frame.cols, frame.type());
- // onlyOrange will have a binary image of 0s and 1s where 1s are orange colour.
- inRange(hsvFrame, Scalar(hueLow, saturationLow,valueLow), Scalar(hueHigh, saturationHigh, valueHigh), onlyOrange);
- cvtColor(onlyOrange, convertedBGR, CV_GRAY2BGR);
- // simply set all the white pixels (which are orange detected pixels) to blue.
- convertedBGR.setTo(Scalar(255, -1000, -1000), onlyOrange);
- std::vector<std::vector<cv::Point>> contours;
- findContours(onlyOrange, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
- int biggestContourIdx = -1;
- float biggestContourArea = 0;
- Mat drawing = onlyOrange.clone();
- for( int i = 0; i< contours.size(); i++ )
- {
- cv::Scalar color = cv::Scalar(255, 0, 0);
- drawContours( drawing, contours, i, color, 1, 8, NULL, 0, cv::Point() );
- float ctArea= cv::contourArea(contours[i]);
- if(ctArea > biggestContourArea)
- {
- biggestContourArea = ctArea;
- biggestContourIdx = i;
- }
- }
- // if no contour found
- if(biggestContourIdx < 0)
- {
- continue;
- }
- cv::RotatedRect boundingBox = cv::minAreaRect(contours[biggestContourIdx]);
- cv::Point2f corners[4];
- boundingBox.points(corners);
- cv::line(drawing, corners[0], corners[1], cv::Scalar(255,0,0));
- cv::line(drawing, corners[1], corners[2], cv::Scalar(255,0,0));
- cv::line(drawing, corners[2], corners[3], cv::Scalar(255,0,0));
- cv::line(drawing, corners[3], corners[0], cv::Scalar(255,0,0));
- char diameter[100];
- sprintf(diameter, "Diameter: %f", (corners[0].x-corners[1].x));
- putText(drawing, diameter, Point2f(100,100), FONT_HERSHEY_PLAIN, 2, Scalar(255, 0,0), 2);
- cvtColor(drawing, drawing, CV_GRAY2BGR);
- imshow("Original Video", frame); //show the frame in ""Original Video"" window.
- imshow("Object Detection", frame + drawing + convertedBGR); //show the hsv frame in "object detection" window.
- if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
- {
- cout << "esc key is pressed by user" << endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement