MRC/OpenCV: Difference between revisions
(Created page with 'Well, this will be typed by Marzieh') |
m (moved Embedded Motion Control/OpenCV to MRC/OpenCV) |
||
(18 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
'''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: | |||
<pre># include highgui:hpp | |||
# include cv</pre> | |||
For storing n-dimentional arrays of images or video frames basic class "Mat" is used. | |||
<pre>cv :: Mat :: Mat()</pre> | |||
For video frames : | |||
<pre>cv :: Mat image(height;width;CV_8UC3; pixels; step)</pre> | |||
'''Load and Display an Image''' | |||
If you want to load an image from a file, you can use the imread function. | |||
<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: | |||
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. | |||
<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. | |||
Draw a simple circle with a given center and radius: | |||
<pre>void cv :: circle(Mat :: Image;Center_Circle;Radius_Circle; Thickness_Circle)</pre> | |||
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> | |||
Draw a simple, thick, or filled rectangle. | |||
<pre>void voidcv :: rectangle(Mat :: Image; pt1; pt2; color; thickness; lineT ype;shift)</pre> | |||
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. | |||
<pre>void cv :: ellipse(Image; center; axes; angel; startAngle; endAngle; color; thickness)</pre> | |||
'''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: | |||
<pre> | |||
#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 ) ; | |||
} | |||
</pre> | |||
[[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 ) ; }