Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include<opencv/cv.hpp>
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <opencv2/imgproc/imgproc.hpp>
  5. #include <iostream>
  6. #include <vector>
  7. #include <stdio.h>
  8.  
  9. using namespace cv;
  10. using namespace std;
  11.  
  12. int main(int argc, char* argv[])
  13. {
  14.     VideoCapture cap; // open the webcam for reading
  15.    
  16.     if ( !cap.open(0) )  // if not success, exit program
  17.     {
  18.         cout << "Cannot open webcam" << endl;
  19.         return -1;
  20.     }
  21.    
  22.     namedWindow("Original Video",CV_WINDOW_AUTOSIZE); //create a window called "Original Video"
  23.     namedWindow("Object Detection",CV_WINDOW_AUTOSIZE); //create a window called "Object detection and changed colour"
  24. //    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
  25. //    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
  26.    
  27.     //Specifying the range of HSV.
  28.     int hueLow = 0; //13
  29.     int saturationLow = 110; // 160
  30.     int valueLow = 110; //160
  31.    
  32.     int hueHigh = 21; //22
  33.     int saturationHigh = 255;
  34.     int valueHigh = 255;
  35.    
  36.     while(1)
  37.     {
  38.         Mat frame, convertedBGR;
  39.        
  40.         bool bSuccess = cap.read(frame); // read a new frame from video
  41.        
  42.         Mat hsvFrame = Mat::zeros(frame.rows, frame.cols, frame.type());
  43.        
  44.         cvtColor(frame,hsvFrame, CV_BGR2HSV); // Converting a frame to HSV color space
  45.        
  46.         if (!bSuccess) //if not success, break loop
  47.         {
  48.             cout << "Cannot read the frame from video file" << endl;
  49.             break;
  50.         }
  51.  
  52.         Mat onlyOrange = Mat::zeros(frame.rows, frame.cols, frame.type());
  53.         // onlyOrange will have a binary image of 0s and 1s where 1s are orange colour.
  54.         inRange(hsvFrame, Scalar(hueLow, saturationLow,valueLow), Scalar(hueHigh, saturationHigh, valueHigh), onlyOrange);
  55.         cvtColor(onlyOrange, convertedBGR, CV_GRAY2BGR);
  56.         // simply set all the white pixels (which are orange detected pixels) to blue.
  57.         convertedBGR.setTo(Scalar(255, -1000, -1000), onlyOrange);
  58.        
  59.         std::vector<std::vector<cv::Point>> contours;
  60.         findContours(onlyOrange, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
  61.         int biggestContourIdx = -1;
  62.         float biggestContourArea = 0;
  63.         Mat drawing = onlyOrange.clone();
  64.         for( int i = 0; i< contours.size(); i++ )
  65.         {
  66.             cv::Scalar color = cv::Scalar(255, 0, 0);
  67.             drawContours( drawing, contours, i, color, 1, 8, NULL, 0, cv::Point() );
  68.            
  69.             float ctArea= cv::contourArea(contours[i]);
  70.             if(ctArea > biggestContourArea)
  71.             {
  72.                 biggestContourArea = ctArea;
  73.                 biggestContourIdx = i;
  74.             }
  75.         }
  76.  
  77.         // if no contour found
  78.         if(biggestContourIdx < 0)
  79.         {
  80.             continue;
  81.         }
  82.        
  83.         cv::RotatedRect boundingBox = cv::minAreaRect(contours[biggestContourIdx]);
  84.         cv::Point2f corners[4];
  85.         boundingBox.points(corners);
  86.         cv::line(drawing, corners[0], corners[1], cv::Scalar(255,0,0));
  87.         cv::line(drawing, corners[1], corners[2], cv::Scalar(255,0,0));
  88.         cv::line(drawing, corners[2], corners[3], cv::Scalar(255,0,0));
  89.         cv::line(drawing, corners[3], corners[0], cv::Scalar(255,0,0));
  90.         char diameter[100];
  91.         sprintf(diameter, "Diameter: %f", (corners[0].x-corners[1].x));
  92.         putText(drawing, diameter, Point2f(100,100), FONT_HERSHEY_PLAIN, 2, Scalar(255, 0,0), 2);
  93.         cvtColor(drawing, drawing, CV_GRAY2BGR);
  94.        
  95.         imshow("Original Video", frame); //show the frame in ""Original Video"" window.
  96.         imshow("Object Detection", frame + drawing + convertedBGR); //show the hsv frame in "object detection" window.
  97.        
  98.         if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
  99.         {
  100.             cout << "esc key is pressed by user" << endl;
  101.             break;
  102.         }
  103.     }
  104.    
  105.     return 0;
  106.    
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement