Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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 matrices that stores the pixel information from the photo and then outputs it. A waitKey value is necessary. If it is 0, the photo will stay indefinitely until the file is closed. For videos, a waitKey of 30 inside a while loop is usually good. 30 would be 30 milliseconds, the time between each frame.

Code Block
//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, canny. 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 erode the image would make the partial outline thicker and possible 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 for object recognition.

Code Block
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. 

Code Block
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);
}


Contributors: 
Contributors
modelist