MRC/OpenCV

From Control Systems Technology Group

Jump to: navigation, search

OpenCV in C++ code

In OpenCV, each image is represented as a matrix. In order to create and manipulate windows that can display images, videos and "remember" their content HighGUI module has been used. To use this module we need following header files in c++ codes:

# include highgui:hpp
# include cv

For storing n-dimentional arrays of images or video frames basic class "Mat" is used.

cv :: Mat :: Mat()

For video frames :

cv :: Mat image(height;width;CV_8UC3; pixels; step)

Load and Display an Image

If you want to load an image from a file, you can use the imread function.

Mat cv :: imread(filename; int flags)

Display: In order to display an image in an OpenCV window the following steps should be done:

Create a window which can be used as a placeholder for images.

cv::namedWindow("Name", ags)

Display an image in the specified window.

cv::imshow( namedWindow, InputArray mat);

Draw various shapes

Various shapes like Circles, Rectangles, Lines, Ellipses, and Polylines can be drawn.You can draw in images or frames by using following commands.

Draw a simple circle with a given center and radius:

void cv :: circle(Mat :: Image;Center_Circle;Radius_Circle; Thickness_Circle)

Draw an arrow segment pointing from the first point to the second one.

void cv :: arrowedLine(Mat :: Image; point1; point2; color; thickness; type)

Draw a simple, thick, or filled rectangle.

void voidcv :: rectangle(Mat :: Image; pt1; pt2; color; thickness; lineT ype;shift)

pt1=Vertex of the rectangle. pt2=Vertex of the rectangle opposite to pt1.

Draws a simple or thick elliptic arc or fills an ellipse sector.

void cv :: ellipse(Image; center; axes; angel; startAngle; endAngle; color; thickness)

Example

In this part you can find an example of draw shapes in opencv.In order to create Figure 1, first black empty image is created then three shapes in certain positions are drawn.This figure is generated as follows:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv ;
int main ( )
{
// Create b lack empty images

Mat image = Mat::zeros(400,400,CV_8UC3 ) ;
// Draw a circle
cv::circle(image,Point(200,200),32.0,Scalar(100,150,0),1,8);

// Draw a rectangle
rectangle(image,Point(10,20),Point(40,50),Scalar(0,55,255),+1,4);

// Draw a filled rectangle 
rectangle(image,Point(215,320),Point(270,350),Scalar(140,255,255),+1,8);

imshow( "Image" , image ) ;
waitKey ( 0 ) ;
return ( 0 ) ;

}
More information can be found on [1]
Personal tools