User Tools

Site Tools


opencv_tutorials_t6

This is an old revision of the document!


Tutorial 6

On this tutorial you will learn how to use the Threshold together with tracking algorithm to track an object by its contour. The object will be defined as the biggest contour found.The idea is to mix Tutorial 4 ( Detecting edges and contours ) and Tutorial 5 ( Threshold ).

I recommend you to type the code on your own to get familiarized with the program language. If you have trouble , the original code is attached bellow ( Running on Visual Studio 2015 + OpenCV 3.1 ) * Check the installation guide to make sure that you linked all the OpenCV modules to your Visual Studio.


Tracking an object by its contour

<Code:c++ linenums:1> Initializing the modules that will be used #include <sstream> #include <iostream> #include <opencv/highgui.h> #include <opencv/cv.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #define _CRT_SECURE_NO_WARNINGS defining the namespace used ( no need to use cv:: or std:: before the API's) using namespace cv; using namespace std;

Values to be used on the HSV trackbar these will be changed using trackbars int iLowH = 0; int iHighH = 179;

int iLowS = 0; int iHighS = 255;

int iLowV = 0; int iHighV = 255;

default capture width and height const int FRAME_WIDTH = 640; const int FRAME_HEIGHT = 480; Function to create a window with the Trackbars to apply the Thresholding.

void createTrackbars() {

//Open the window to display the Trackbars
namedWindow("Trackbars", CV_WINDOW_AUTOSIZE);
//Hue values (0 - 179)
cvCreateTrackbar("LowH", "Trackbars", &iLowH, 179);
cvCreateTrackbar("HighH", "Trackbars", &iHighH, 179);
//Saturation Values (0-255)
cvCreateTrackbar("LowS", "Trackbars", &iLowS, 255);
cvCreateTrackbar("HighS", "Trackbars", &iHighS, 255);
//Value (0-255)
cvCreateTrackbar("LowV", "Trackbars", &iLowV, 255);
cvCreateTrackbar("HighV", "Trackbars", &iHighV, 255);

}

Function to apply the erode and dilate features. void DilateAndErode(Mat &image) { Defining the erode and dilate properties

//the erode element chosen here is a 3x3 piexels rectangle.
//Change the Size argument to optimize your thresholding. 
//dilate with 8x8 size element to make the thresholding object more visible
Mat erodeElement = getStructuringElement(MORPH_RECT, Size(3, 3));
Mat dilateElement = getStructuringElement(MORPH_RECT, Size(8, 8));
//Apply erode and dilate
erode(image, image, erodeElement);
dilate(image, image, dilateElement);

}

int main(int argc, char *argv[]) {

//Variables to store the opened image , a gray version , edge and cedge
Mat coloredimage;
Mat grayimage;
Mat edge;
Mat HSV;
Mat threshold;
//Edge detection Threshold 
int edgeThreshold = 10;
//auxiliar variables to control the actions of the program
char key = 0;
bool useFeatures = true;
//Call the function to create the window with the trackbars
createTrackbars();
//Initialize the webcam capture
VideoCapture capture;
capture.open(0);
//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);
//Until the User press Escape the loop will run
//Get the image from the webcam -> convert to HSV -> Threshold the image
//using the HSV max and min set on the Trackbar window.
//The threshold image will be used into the findcontours function to find the contours.
//The largest contour will be draw on the webcam frame.
while (key != 'q')
{
	//Get the image from the webcam
	capture >> coloredimage;
	//Convert the frame from BGR (RGB) to HSV
	cvtColor(coloredimage, HSV, COLOR_BGR2HSV);
	//filter the HSV image using the minimum and maximum values set on the 
	//Trackbars window using the inRange function.		
	inRange(HSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), threshold);
	if (key == 'm')
	{
		useFeatures = !useFeatures;
	}
	//If the key M is pressed the dilate and erode features will be activated.
	if (useFeatures)
	{
		DilateAndErode(threshold);
	}
	//Show Threshold image
	imshow("Threshold", threshold);
	//Canny is a openCV function  (Canny Algorithm to edge detection )
	//Canny(source image , output image , threshold 1 , threshold 2 (usually 3 times threshold1) , kernel size )
	Canny(threshold, edge, edgeThreshold, edgeThreshold * 3, 3);
	//Initializing two vectors to be used on the findContours function
	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;
	//The function findContours  Use the output of the Canny function as input
	findContours(edge, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
	// Initialize drawing variable to store the image with the contours
	Mat drawing = Mat::zeros(edge.size(), CV_8UC3);
	//Find the largest contour to be draw on the image
	int largest_area = 0;
	int largest_contour_index = 0;
	for (int i = 0; i < contours.size(); i++)
	{
		double a = contourArea(contours[i], false);  //  Find the area of contour
		if (a > largest_area)
		{
			largest_area = a;
			largest_contour_index = i;                //Store the index of largest contour
		}
	}
	//draw the largest contour on the image		
	drawContours(coloredimage, contours, largest_contour_index, Scalar(0, 0, 255), 2, 8, hierarchy, 0, Point());
	// Show image with contours in a new window.	
	imshow("Find Contours", coloredimage);
	key = waitKey(25);
}
return 0;

} </Code>


Understanding the code

opencv_tutorials_t6.1465263306.txt.gz · Last modified: by joaomatos