Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Colour + Contour Detection

Hi everybody,

my name is Chris and I'm pretty new with openCV.

I'm working on a project where a gopro is mounted on a drone and the goal is to implement a marker detection. (The marker is a huge, red exercise ball)

So I started searching for algorithms and stuff and found basically two openCV functions: HoughCircles and FindContours

it works pretty so well but as soon as a second red object is in the view, then the new one is tracked and the ball is discarded. Now I'm looking for a way that always the ball is tracked.

It should work when the tracking only takes place when the two functions are "connected", then the object is only tracked if it is a red circle. Everything else will be discarded. Does someone know how this can be done?

Thank you

Best, Bibof

Colour + Contour Detection

Hi everybody,hey pklab,

sorry for late reply, eclipse stated some really weird errors, now it's finally building: The actual code is this one ( I replaced my name TrackingObject function with yours):

the picture looks as following. Would you suggest to improve the code or to go back to the old one? Can you explain me, why the picture looks like that?

contours.jpg

#include <cv.h>
#include <highgui.h>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;


//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 = 5*5;
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";
void on_trackbar( int, void* )
{//This function gets called whenever a
    // trackbar position is Chris changed
 }
string intToString(int number){


    std::stringstream ss;
    ss << number;
    return ss.str();
}
void createTrackbars(){
    //create window for trackbars


    namedWindow(trackbarWindowName,0);


    //create trackbars and I'm pretty new with openCV.

I'm working on a project where a gopro insert them into window //3 parameters are: the address of the variable that is mounted on a drone 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(int x, int y,Mat &frame){ //use some of the openCV drawing functions to draw crosshairs //on your tracked image! //UPDATE:JUNE 18TH, 2013 //added 'if' and the goal 'else' statements to prevent //memory errors from writing off the screen (ie. (-25,-25) is not within the window!) circle(frame,Point(x,y),20,Scalar(0,255,0),2); if(y-25>0) line(frame,Point(x,y),Point(x,y-25),Scalar(0,255,0),2); else line(frame,Point(x,y),Point(x,0),Scalar(0,255,0),2); if(y+25<FRAME_HEIGHT) line(frame,Point(x,y),Point(x,y+25),Scalar(0,255,0),2); else line(frame,Point(x,y),Point(x,FRAME_HEIGHT),Scalar(0,255,0),2); if(x-25>0) line(frame,Point(x,y),Point(x-25,y),Scalar(0,255,0),2); else line(frame,Point(x,y),Point(0,y),Scalar(0,255,0),2); if(x+25<FRAME_WIDTH) line(frame,Point(x,y),Point(x+25,y),Scalar(0,255,0),2); else line(frame,Point(x,y),Point(FRAME_WIDTH,y),Scalar(0,255,0),2); putText(frame,intToString(x)+","+intToString(y),Point(x,y+30),1,1,Scalar(0,255,0),2); } void morphOps(Mat &thresh){ //create structuring element that will be used to implement a marker detection. (The marker "dilate" and "erode" image. //the element chosen here is a huge, red exercise ball)

So I started searching for algorithms and stuff and found basically two openCV functions: HoughCircles and FindContours

it works pretty 3px by 3px rectangle Mat erodeElement = getStructuringElement( MORPH_RECT,Size(3,3)); //dilate with larger element so well but as soon as a second red 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); } double CircularityStandard(double area,double perimeter) { if( (area<=0) || (perimeter<=0)) return 0; return (4 * CV_PI*area) / (perimeter*perimeter); } double CircularityStandard(const vector<cv::Point> &contour) { double area = cv::contourArea(contour); if (area == 0) return 0.0; double perimeter = cv::arcLength(contour, true); if (perimeter == 0) return 0.0; return (4 * CV_PI*area) / (perimeter*perimeter); } void trackFilteredObject(int &x, int &y, const Mat &threshold, Mat &cameraFeed) { vector< vector<Point> > contours; vector<Vec4i> hierarchy; // find external contours ignores holes cv::findContours(threshold, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter if (contours.size() > MAX_NUM_OBJECTS) { putText(cameraFeed, "TOO MUCH NOISE! ADJUST FILTER", Point(0, 50), 1, 2, Scalar(0, 0, 255), 2); return; } double refArea = 0; bool objectFound = false; double circularity; //int largest = -1; double minCircularity = 0.9; //use something > 0.85 for (size_t i = 0; i < contours.size(); i++) { // Mat src; // Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3); // draw all contours in the view, red //cv::drawContours(dst, contours, i, cv::Scalar(0, 0, 255), 1); cv::Moments moment = moments(contours[i]); double area = moment.m00; double perimeter = cv::arcLength(contours[i], true); //circularity = CircularityStandard(contours[i]); //alternative overload circularity = CircularityStandard(area,perimeter); if (circularity < minCircularity) //use something > 0.85 continue; // draw all circles in blue int cx = cvRound(moment.m10 / area); int cy = cvRound(moment.m01 / area); cv::Point center(x, y); int radius = cvRound(sqrt(refArea / CV_PI)); // circle center circle(cameraFeed, center, 3, Scalar(0, 255, 0), -1, 8, 0); // circle outline circle(cameraFeed, center, radius, Scalar(255, 0, 0), 3, 8, 0); //if the area is less than 20 px by 20px then the new one is tracked and the ball is discarded. Now I'm looking for a way that always the ball is tracked.

It should work when the tracking only takes place when the two functions are "connected", then the object is only tracked if it is probably just noise //if the area is the same as the 3/2 of the image size, probably just a red circle. Everything else bad filter //object wanted with the largest area so a reference area is stored each //iteration and compare it to the area in the next iteration. if (area > refArea && area > MIN_OBJECT_AREA && area < MAX_OBJECT_AREA) { objectFound = true; x = cx; y = cy; refArea = area; } } //let user know you found an object if (objectFound == true) { putText(cameraFeed, "Tracking Object", Point(0, 50), 2, 1, Scalar(0, 255, 0), 2); //draw object location on screen drawObject(x, y, cameraFeed); } } int main(int argc, char* argv[]) { //some boolean variables for different functionality within this //program bool trackObjects = true; bool useMorphOps = true; //Matrix to store each frame of the webcam feed Mat cameraFeed; //matrix storage for HSV image Mat HSV; //matrix storage for binary threshold image Mat threshold; //x and y values for the location of the object int x=0, y=0; //create slider bars for HSV filtering createTrackbars(); //video capture object to acquire webcam feed //open capture object at location zero (default location for webcam) VideoCapture capture(0); //capture.open(0);// open the video file for reading //set height and width of capture frame capture.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH); capture.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT); //start an infinite loop where webcam feed is copied to cameraFeed matrix //all of our operations will be discarded. Does someone know how performed within this loop while(1){ //store image to matrix capture.read(cameraFeed); //convert frame from BGR to HSV colorspace cvtColor(cameraFeed,HSV,COLOR_BGR2HSV); //filter HSV image between values and store filtered image to //threshold matrix inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold); //perform morphological operations on thresholded image to eliminate noise //and emphasize the filtered object(s) if(useMorphOps) morphOps(threshold); // Draw the circles detected for( size_t i = 0; i < circles.size(); i++ ) { int x = circles.size(); std::cout << x << std::endl; Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius = cvRound(circles[i][2]); // circle center circle(cameraFeed , center, 3, Scalar(0,255,0), -1, 8, 0 ); // circle outline circle( cameraFeed, center, radius, Scalar(0,0,255), 3, 8, 0 ); } //pass in thresholded frame to our object tracking function //this function will return the x and y coordinates of the //filtered object if(trackObjects) trackFilteredObject(x,y,threshold,cameraFeed); //show frames imshow(windowName2,threshold); imshow(windowName,cameraFeed); imshow(windowName1,HSV); //delay 30ms so that screen can be done?

Thank you

Best, Bibof

refresh. //image will not appear without this waitKey() command waitKey(30); } return 0; }