MRC/OpenCV: Difference between revisions

From Control Systems Technology Group
Jump to navigation Jump to search
No edit summary
 
(17 intermediate revisions by one other user not shown)
Line 1: Line 1:
'''OpenCV in C++ code'''
'''OpenCV in C++ code'''


In OpenCV each image is represented as a matrix and in order to create and manipulate windows that can display images or 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:


# include highgui:hpp
<pre># include highgui:hpp
# include cv
# include cv</pre>


For storing n-dimentional arrays of images or video frames basic class "Mat" is used.
For storing n-dimentional arrays of images or video frames basic class "Mat" is used.
cv :: Mat :: Mat()
<pre>cv :: Mat :: Mat()</pre>
For video frames : cv :: Mat image(height;width;CV􀀀8UC3; pixels; step)
 
For video frames :
<pre>cv :: Mat image(height;width;CV_8UC3; pixels; step)</pre>


'''Load and Display an Image'''
'''Load and Display an Image'''


Load 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.


Mat cv :: imread(filename; int flags)
<pre>Mat cv :: imread(filename; int flags)</pre>


Display: In order to display an image in an OpenCV window the following steps should be done:
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)
Create a window which can be used as a placeholder for images.  
<pre>cv::namedWindow("Name", ags)</pre>
 
Display an image in the specified window.
Display an image in the specified window.
<pre>cv::imshow( namedWindow, InputArray mat);</pre>
'''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.


cv::imshow( namedWindow, InputArray mat);
Draw a simple circle with a given center and radius:  


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


Various shapes like Circles,Rectangles, Lines, Ellipses, Polylines, Polylines, Polylines can be drawn.
Draw an arrow segment pointing from the first point to the second one.
Draws a simple or filled circle with a given center and radius in images or
frames.
void cv :: circle(Mat :: Image;Center_Circle;Radius_Circle; Thickness_Circle)


Draw a arrow segment pointing from the first point to the second one.
<pre>void cv :: arrowedLine(Mat :: Image; point1; point2; color; thickness; type)</pre>
void cv :: arrowedLine(Mat :: Image; point1; point2; color; thickness; type)


Draw a simple, thick, or filled rectangle.
Draw a simple, thick, or filled rectangle.
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 .
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.
void cv :: ellipse(Image; center; axes; angel; startAngle; endAngle; color; thickness)
<pre>void cv :: ellipse(Image; center; axes; angel; startAngle; endAngle; color; thickness)</pre>


'''Example'''
'''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:
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:
 
<pre>
#include <opencv2/core/core.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui.hpp>
Line 54: Line 59:
// Create b lack empty images
// Create b lack empty images


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


}
}
 
</pre>
[[File:emc.jpg]]
[[File:emc.jpg|thumb|center|500px|More information can be found on [https://docs.opencv.org/2.4/index.html]]]

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]