MRC/OpenCV: Difference between revisions

From Control Systems Technology Group
Jump to navigation Jump to search
No edit summary
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
--[[User:20170370|20170370]] 12:14, 2 May 2018 (UTC)'''OpenCV in C++ code'''
'''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:
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:


<pre># include highgui:hpp
<pre># include highgui:hpp
Line 14: Line 14:
'''Load and Display an Image'''
'''Load and Display an Image'''


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


<pre>Mat cv :: imread(filename; int flags)</pre>
<pre>Mat cv :: imread(filename; int flags)</pre>
Line 28: Line 28:
'''Draw various shapes'''
'''Draw various shapes'''


Various shapes like Circles, Rectangles, Lines, Ellipses and Polylines can be drawn. For example ,By using following command you can draw a simple circle with a given center and radius in images or frames.
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:


<pre>void cv :: circle(Mat :: Image;Center_Circle;Radius_Circle; Thickness_Circle)</pre>
<pre>void cv :: circle(Mat :: Image;Center_Circle;Radius_Circle; Thickness_Circle)</pre>


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


<pre>void cv :: arrowedLine(Mat :: Image; point1; point2; color; thickness; type)</pre>
<pre>void cv :: arrowedLine(Mat :: Image; point1; point2; color; thickness; type)</pre>


Draw a simple, thick, or filled rectangle.
Draw a simple, thick, or filled rectangle.
<pre>void voidcv :: rectangle(Mat :: Image; pt1; pt2; color; thickness; lineT ype;shift)
<pre>void voidcv :: rectangle(Mat :: Image; pt1; pt2; color; thickness; lineT ype;shift)</pre>


pt1=Vertex of the rectangle.
pt1=Vertex of the rectangle.
pt2=Vertex of the rectangle opposite to pt1.</pre>
pt2=Vertex of the rectangle opposite to pt1.


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

Latest revision as of 11:12, 27 March 2020

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]