Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Written by Kyle Hounslow 2013
- // modified by: Ahmad Kaifi, Hassan Althobaiti
- //Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software")
- //, to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
- //and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
- //The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- //IN THE SOFTWARE.
- #include <sstream>
- #include <string>
- #include <iostream>
- #include <vector>
- #include "Object.h"
- //initial min and max HSV filter values.
- //these will be changed using trackbars
- int H_MIN = 0;
- int H_MAX = 256;
- int S_MIN = 0;
- int S_MAX = 256;
- int V_MIN = 0;
- int V_MAX = 256;
- //default capture width and height
- const int FRAME_WIDTH = 640;
- const int FRAME_HEIGHT = 480;
- //max number of objects to be detected in frame
- const int MAX_NUM_OBJECTS=50;
- //minimum and maximum object area
- const int MIN_OBJECT_AREA = 20*20;
- const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH/1.5;
- //names that will appear at the top of each window
- const string windowName = "Original Image";
- const string windowName1 = "HSV Image";
- const string windowName2 = "Thresholded Image";
- const string windowName3 = "After Morphological Operations";
- const string trackbarWindowName = "Trackbars";
- //The following for canny edge detec
- Mat dst, detected_edges;
- Mat src, src_gray;
- int edgeThresh = 1;
- int lowThreshold;
- int const max_lowThreshold = 100;
- int ratio = 3;
- int kernel_size = 3;
- char* window_name = "Edge Map";
- void on_trackbar( int, void* )
- {//This function gets called whenever a
- // trackbar position is changed
- }
- string intToString(int number){
- std::stringstream ss;
- ss << number;
- return ss.str();
- }
- void createTrackbars(){
- //create window for trackbars
- namedWindow(trackbarWindowName,0);
- //create memory to store trackbar name on window
- char TrackbarName[50];
- sprintf( TrackbarName, "H_MIN", H_MIN);
- sprintf( TrackbarName, "H_MAX", H_MAX);
- sprintf( TrackbarName, "S_MIN", S_MIN);
- sprintf( TrackbarName, "S_MAX", S_MAX);
- sprintf( TrackbarName, "V_MIN", V_MIN);
- sprintf( TrackbarName, "V_MAX", V_MAX);
- //create trackbars and insert them into window
- //3 parameters are: the address of the variable that is changing when the trackbar is moved(eg.H_LOW),
- //the max value the trackbar can move (eg. H_HIGH),
- //and the function that is called whenever the trackbar is moved(eg. on_trackbar)
- // ----> ----> ---->
- createTrackbar( "H_MIN", trackbarWindowName, &H_MIN, H_MAX, on_trackbar );
- createTrackbar( "H_MAX", trackbarWindowName, &H_MAX, H_MAX, on_trackbar );
- createTrackbar( "S_MIN", trackbarWindowName, &S_MIN, S_MAX, on_trackbar );
- createTrackbar( "S_MAX", trackbarWindowName, &S_MAX, S_MAX, on_trackbar );
- createTrackbar( "V_MIN", trackbarWindowName, &V_MIN, V_MAX, on_trackbar );
- createTrackbar( "V_MAX", trackbarWindowName, &V_MAX, V_MAX, on_trackbar );
- }
- void drawObject(vector<Object> theObjects,Mat &frame, Mat &temp, vector< vector<Point> > contours, vector<Vec4i> hierarchy){
- for(int i =0; i<theObjects.size(); i++){
- cv::drawContours(frame,contours,i,theObjects.at(i).getColor(),3,8,hierarchy);
- cv::circle(frame,cv::Point(theObjects.at(i).getXPos(),theObjects.at(i).getYPos()),5,theObjects.at(i).getColor());
- cv::putText(frame,intToString(theObjects.at(i).getXPos())+ " , " + intToString(theObjects.at(i).getYPos()),cv::Point(theObjects.at(i).getXPos(),theObjects.at(i).getYPos()+20),1,1,theObjects.at(i).getColor());
- cv::putText(frame,theObjects.at(i).getType(),cv::Point(theObjects.at(i).getXPos(),theObjects.at(i).getYPos()-20),1,2,theObjects.at(i).getColor());
- }
- }
- void morphOps(Mat &thresh){
- //create structuring element that will be used to "dilate" and "erode" image.
- //the element chosen here is a 3px by 3px rectangle
- Mat erodeElement = getStructuringElement( MORPH_RECT,Size(3,3));
- //dilate with larger element so make sure object is nicely visible
- Mat dilateElement = getStructuringElement( MORPH_RECT,Size(8,8));
- erode(thresh,thresh,erodeElement);
- erode(thresh,thresh,erodeElement);
- dilate(thresh,thresh,dilateElement);
- dilate(thresh,thresh,dilateElement);
- }
- void trackFilteredObject(Object theObject,Mat threshold,Mat HSV, Mat &cameraFeed){
- vector <Object> objects;
- Mat temp;
- threshold.copyTo(temp);
- //these two vectors needed for output of findContours
- vector< vector<Point> > contours;
- vector<Vec4i> hierarchy;
- //find contours of filtered image using openCV findContours function
- findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );
- //use moments method to find our filtered object
- double refArea = 0;
- bool objectFound = false;
- if (hierarchy.size() > 0) {
- int numObjects = hierarchy.size();
- //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
- if(numObjects<MAX_NUM_OBJECTS){
- for (int index = 0; index >= 0; index = hierarchy[index][0]) {
- Moments moment = moments((cv::Mat)contours[index]);
- double area = moment.m00;
- //if the area is less than 20 px by 20px then it is probably just noise
- //if the area is the same as the 3/2 of the image size, probably just a bad filter
- //we only want the object with the largest area so we safe a reference area each
- //iteration and compare it to the area in the next iteration.
- if(area>MIN_OBJECT_AREA){
- Object object;
- object.setXPos(moment.m10/area);
- object.setYPos(moment.m01/area);
- object.setType(theObject.getType());
- object.setColor(theObject.getColor());
- objects.push_back(object);
- objectFound = true;
- }else objectFound = false;
- }
- //let user know you found an object
- if(objectFound ==true){
- //draw object location on screen
- drawObject(objects,cameraFeed,temp,contours,hierarchy);}
- }else putText(cameraFeed,"TOO MUCH NOISE! ADJUST FILTER",Point(0,50),1,2,Scalar(0,0,255),2);
- }
- }
- Mat trackingOranges(Mat cameraFeed)
- {
- //if we would like to calibrate our filter values, set to true.
- bool calibrationMode = true;
- //Matrix to store each frame of the webcam feed
- Mat threshold;
- Mat HSV;
- if(calibrationMode){
- //create slider bars for HSV filtering
- createTrackbars();
- }
- calibrationMode = false;
- //convert frame from BGR to HSV colorspace
- //cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
- //create some temp fruit objects so that
- //we can use their member functions/information
- Object orange("orange");
- //orange
- cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
- inRange(HSV,orange.getHSVmin(),orange.getHSVmax(),threshold);
- morphOps(threshold);
- trackFilteredObject(orange,threshold,HSV,cameraFeed);
- return cameraFeed ;
- }
- int main(int argc, char* argv[])
- {
- VideoCapture cap("C:/Users/Dominika/Documents/STUDIA/compvis/FINAL/video-tp2.avi"); // open the video file for reading
- if ( !cap.isOpened() ) // if not success, exit program
- {
- cout << "Cannot open the video file" << endl;
- return -1;
- }
- cap.set(CV_CAP_PROP_POS_MSEC, 3000); //start the video at 300ms
- double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
- cout << "Frame per seconds : " << fps << endl;
- namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
- while(1)
- {
- //Matrix to store each frame of the cam feed
- Mat frame;
- bool bSuccess = cap.read(frame); // read a new frame from video
- if (!bSuccess) //if not success, break loop
- {
- cout << "Cannot read the frame from video file" << endl;
- break;
- }
- // process here!
- imshow("MyVideo", trackingOranges(frame));
- //vc_gray_gaussian_filter(gray,dst);
- //vc_gray_highpass_filter(gray,dst);
- //imshow("Highpass", dst); // highpass video
- 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