Object Tracking: C++
Table of Contents
OpenCV
There are many useful functions in the OpenCV libraries. Since they are not embedded into the C++ programs, they will need to be accessed through libraries. Below is some simple code showing the libraries required and how certain information can be read. This is a good way to introduce a new user to OpenCV and some possibilities. For more information, go to OpenCV Tutorials.
The Mat file type is a basic image container which stores the pixel information in a matrices from a photo and after can output it [1].
A waitKey value is also required. If the waitKey is 0, the photo will stay open indefinitely until the photo file is closed. For videos, a waitKey of 30 inside a while loop is usually good to go between each frame. The 30 refers to 30 milliseconds. Since there are both video and photos that may be accessed, it is important to recognize that there is a specific way they are entered. For videos, it will preview a frame in a while loop usually.
//required libraries for OpenCV to access functions. hereon out, it will be the "usual libraries" #include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> using namespace cv; //allow for OpenCV variables using namespace std; //allow for standard variables void main() { string path = “test_image.jpg”; //can be jpg or png Mat image = imread(path); //mat is matrix that is opened by open cv, data type is mat imshow(“Image”, img); waitKey(0); //system pause, not close until you close the file yourself }
Photo Functions
Some common functions for editing the photo include black and white, and greyscale. With that, you can dilate an image to decrease thickness and erode to increase the thickness of the lines. For instance, if there is a partial outline around a circle, then eroding the image would make the partial outline thicker and possibly connect the lines, creating a circle. This will help enable better object identification. As well, the threshold function turns the image to black and white (1s and 0s), which makes it easier to use object recognition.
void main() { string path = “Resources/test.jpg”; Mat image = imread(path); Mat imagery; cvtColor (img, imgGray, COLOR_BRG2GRAY); //input, output dilate(imgCanny, imgDil, imgErode); //make kernel can be used by dilation May kernel = getStructuringElement(MORPH, RECT, Size(5,5)); dilate (imgCanny, imgDil, kernel); erode (imgDil,imgErode, kernel); imshow(“Image Dilation”, imgDil); //make lines thicker imshow(“Image Erosion”, imgErode); //decrease thickness imshow(“Image”, img); imshow(“Image Gray”, imgGray); waitKey(0); //system pause, not close until you press close }
Shapes and Text
Below is an example of what you need to do for displaying a function. The key elements are the source file (img), points (centre, vertices) relative to the image coordinate system, the colour (scalar, use 0-255, 3 layer bit), and the thickness.
void main() { //Blank image Mat img (512,512, CV_8UC3, Scalar(255,0,0)) //0 to 255 8-bit rectangle(img, Point(100,200), Point (382,286), Scalar(255,255,255),3); circle(img, Point(256,256), 155, Scalar(0,69,255),10);//thickness, FILLED will fill line(img, Point(130,296),Point(382,296), Scalar(255,255,255),2); //point is starting and ending point putText(img, “John Doe”, Point(100,200), FONT_HERSHEY_DUPLEX, 1.5, Scalar(0,69,255),2); imshow(“New Image”, img); waitKey(0); }
Advantages and Disadvantages for OpenCV C++
Advantages of using C++ | Disadvantages of using C++ | |
---|---|---|
Storage / Speed |
|
|
Functions |
|
|
References
[1] “Mat - The Basic Image Container,” OpenCV. [Online]. Available: https://docs.opencv.org/master/d6/d6d/tutorial_mat_the_basic_image_container.html. [Accessed: 13-Apr-2021].
Contributors:
User Last Update Mayurakhi Khan 1145 days ago Former user (Deleted) Former user (Deleted)