1. ホーム
  2. OpenCV

Opencvにおける矩形関数とRect関数の使用法

2022-03-01 15:57:06
<パス

記事目次


rectangle 関数は、通常画像のマークアップに使用される長方形のボックスを描画するために使用されます。

1.rectangle(img2, Point(j,i), Point(j + img4.cols, i + img4.rows), Scalar(255, 255, 0), 2, 8);

img2

Point(j,i) 処理中の画像
Point(j + cols, i + rows) 矩形の左上点の座標を表す
scalar 矩形の右下点の座標を表す[矩形の大きさ(cols,rows)]。
2 : 色
8 線の幅を表す
Rect() は行のタイプで、デフォルトでは8を取ります
Rect(x,y,width,height) は、画像に矩形を描画する関数です。
x, y width, height は左上隅の座標です。 Rect roi_rect = Rect(128, 128, roi.cols, roi.rows); template<typename _Tp> class Rect_ { public: typedef _Tp value_type; //! various constructors Rect_(); Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height); Rect_(const Rect_& r); Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz); Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2); Rect_& operator = ( const Rect_& r ); //! the top-left corner Point_<_Tp> tl() const; //! the bottom-right corner Point_<_Tp> br() const; //! size (width, height) of the rectangle Size_<_Tp> size() const; //! area (width*height) of the rectangle _Tp area() const; //! conversion to another data type template<typename _Tp2> operator Rect_<_Tp2>() const; //! checks whether the rectangle contains the point bool contains(const Point_<_Tp>& pt) const; _Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle }; Rect_() は縦と横の長さです。

x=y=width=height=0

I. rect() クラス

Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height)

(_x, _y) の場合、正式なパラメータのリストは空であり、すなわち空のウィンドウを定義することになります(デフォルト値は _width*_height ).
Rect_(const Rect_& r) の座標を持つ左上の点を定義します。 Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz) (_x, _y) 長方形の窓。 (強調)
(_width, _height) 他のRect_オブジェクトで初期化されます。
Point_ and Size_ で、それぞれ位置座標を設定します。 Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2) とウィンドウサイズ (_x, _y) 使用方法 (_width, _height) オブジェクトを初期化します。

Point_ and Point_ で、それぞれ座標位置を設定します。 //If you create a Rect object rect(100, 50, 50, 100), then rect will have the following functions. rect.area(); //returns the area of rect 5000 rect.size(); //returns the size of rect [50 × 100] rect.tl(); //returns the coordinates of the upper left vertex of rect [100, 50] rect.br(); //returns the coordinates of the lower right vertex of rect [150, 150] rect.width(); //returns the width of rect 50 rect.height(); //returns the height of rect 100 rect.contains(Point(x, y)); //returns the boolean variable that determines whether rect contains the Point(x, y) point //also find the intersection and concatenation of two rectangles rect = rect1 & rect2; rect = rect1 | rect2; //You can also translate and scale the rectangle rect = rect + Point(-100, 100); //Pan, that is, the x coordinate of the upper left vertex -100, y coordinate +100 rect = rect + Size(-100, 100); //scaling, the top left vertex remains the same, width -100, height +100 //also compares the rectangle, returning a boolean variable rect1 == rect2; rect1 ! = rect2; //It seems that there is no function in OpenCV to determine whether rect1 is inside rect2, so write one yourself bool isInside(Rect rect1, Rect rect2) { return (rect1 == (rect1&rect2)); } // OpenCV doesn't seem to have the ability to get the center point of a rectangle, so write one yourself Point getCenterPoint(Rect rect) { Point cpt; cpt.x = rect.x + cvRound(rect.width/2.0); cpt.y = rect.y + cvRound(rect.height/2.0); return cpt; } //scale around the center of the rectangle Rect rectCenterScale(Rect rect, Size size) { rect = rect + size; Point pt; pt.x = cvRound(size.width/2.0); pt.y = cvRound(size.height/2.0); return (rect-pt); } void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0); とウィンドウサイズ //! type of line enum LineTypes { FILLED = -1, LINE_4 = 4, //! < 4-connected line LINE_8 = 8, //! < 8-connected line LINE_AA = 16 //! < antialiased line }; 使用方法 rectangle(matImage,Point(20,200),Point(200,300),Scalar(255,0,0),1,1,0); //Rect(int a,int b,int c,int d) a,b are the coordinates of the upper left corner of the rectangle, c,d are the length and width of the rectangle rectangle(matImage,Rect(100,300,20,200),Scalar(0,0,255),1,1,0); オブジェクトで初期化します。

void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color,
                  int thickness=1, int line_type=8, int shift=0 );

img
    image .
pt1
    A vertex of the rectangle.
pt2
    The other vertex on the diagonal of the rectangle.
color
    The line color (RGB) or brightness (grayscale image).
thickness
    The thickness of the lines that make up the rectangle. With negative values (e.g. CV_FILLED) the function draws a rectangle with a color fill.
line_type
    The type of the line. See the description of cvLine
shift
    The number of decimal places of the coordinate points.
     

//Include the header files and namespaces used by the program
#include

#include

#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
//Macro definition section
#define Win_Name3 "Cross Red Cross"
//main function
int main(int argc,char** argv)
{	
<span style="white-space:pre"> </span>//draw the Red Cross
	Mat image3 = Mat::zeros(800, 800, CV_8UC3);//generate a window of 800x800
	Rect rec1 = Rect(100, 300, 600, 200);
	Rect rec2 = Rect(300, 100, 200, 600);
	rectangle (image3, rec1,Scalar(0, 0, 255), -1, 8, 0);//generate a horizontal rectangle
	rectangle (image3, rec2, Scalar(0, 0, 255), -1, 8, 0);//vertical rectangle
	rectangle (image3, Point(100, 300), Point(700, 500), Scalar(0, 255, 255), 2, 8, 0);//yellow rectangle with borders
	rectangle(image3, Point(300, 100), Point(500, 700), Scalar(0, 255, 255), 2, 8, 0);//yellow rectangle bordered
	rectangle(image3, Point(300, 300), Point(500, 500), Scalar(0, 0, 255), 3, 8);//red square overlay (central)
	imshow(Win_Name3, image3);
	waitKey();
	return 0;
}

void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color,
               int thickness=1, int line_type=8, int shift=0 );

img
    Image.
center
    The coordinates of the center of the circle.
radius
    The radius of the circle.
color
    The color of the line.
thickness
    If positive, the thickness of the lines forming the circle. Otherwise, it indicates whether the circle is filled or not.
line_type
    The type of the line. See the description of cvLine
shift
    The number of decimal places of the center point and radius value.
    The cvCircle function draws or fills a circle with the given center and radius. The circle is cropped by the rectangle of interest. If you specify the color of the circle, you can use the macro CV_RGB ( r, g, b ).


#include
 //Include the header files and namespaces used by the program

#include

#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
#define Win_Name2 "Audi Audi"
int main(int argc, char** argv)
{
	// Draw the Audi Audi logo
	Mat image2 = Mat::zeros(600, 850, CV_8UC3);//generate a window of 850x600
	circle(image2, Point(200, 300), 100, Scalar(225, 0, 225), 7, 8);//draw the first circle, radius 100, center (200, 300), color purple
	circle(image2, Point(350, 300), 100, Scalar(225, 0, 225), 7, 8);//Draw the first circle, radius 100, center (350, 300), line width of 7
	circle(image2, Point(500, 300), 100, Scalar(225, 0, 225), 7, 8);//draw the first circle, radius 100, the center of the circle (500, 300)
	circle(image2, Point(650, 300), 100, Scalar(225, 0, 225), 7, 8);// draw the first circle, radius of 100, the center of the circle (650, 300)
	imshow(Win_Name2, image2);
	waitKey();
	return 0;
}

void cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle,
                double start_angle, double end_angle, CvScalar color,
                int thickness=1, int line_type=8, int shift=0 );

img
    Image.
center
    The coordinates of the center of the ellipse.
axes
    The length of the axis.
angle
    The angle of the deflection.
start_angle
    The angle of the start angle of the arc.
end_angle
    The angle of the end of the arc.
color
    The color of the line.
thickness
    The thickness of the line.
line_type
    The type of the line, see the description of CVLINE.
shift
    The precision of the center point and the number axis.

The cvEllipse function is used to draw or fill a simple elliptical arc or elliptical sector. Circular arcs are ignored by ROI rectangles. Anti-walking arcs and coarse arcs use linear segment approximations. All angles are given in the form of angles.


//---------------- drawing of basic geometric images ---------------
//Include the header files and namespaces used by the program
#include

#include

#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
#define Win_Name1 "Toyota Toyota" 
//Macro definition section

//main function
int main(int argc, char** argv)
{
	//draw the Toyota logo
	Mat image1 = Mat::zeros(900, 900, CV_8UC3);//900x900 window
	ellipse(image1, Point(450, 450), Size(400, 250), 0, 0, 360, Scalar(0, 0, 225), 5, 8);//draw the first ellipse, large ellipse, color is red
	ellipse(image1, Point(450, 450), Size(250, 110), 90, 0, 360, Scalar(0, 0, 225), 5, 8);//draw the second ellipse, vertical ellipse
	ellipse(image1, Point(450, 320), Size(280, 120), 0, 0, 360, Scalar(0, 0, 225), 5, 8);//draw the third ellipse, small ellipse (horizontal)
	imshow(Win_Name1, image1);
	waitKey();
	return 0;
}


2つ目は、rectangle()関数

void rectangle(InputOutputArray img, Point pt1, Point pt2,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);



はじめに 対角線の2点 pt1, pt2 を使って長方形の輪郭または塗りつぶし長方形を描く

param img 画像です。
param pt1 矩形の頂点。
param pt2 pt1 と反対側の矩形の頂点.
color 線色(RGB)または輝度(グレースケール画像).
param thickness 矩形を構成する線の太さ. CV_FILLED のような負の値.
矩形を構成する線の太さ。CV_FILLED のような負の値は,塗りつぶされた矩形を描かなければならないことを意味します.

<ブロッククオート

パラメータ lineType 線の種類を指定します。

//! type of line
enum LineTypes {
    FILLED = -1,
    LINE_4 = 4, //! < 4-connected line
    LINE_8 = 8, //! < 8-connected line
    LINE_AA = 16 //! < antialiased line
};


はじめに 行列クラス rec を使って、長方形の輪郭を描いたり、長方形を塗りつぶしたりします。

その他のパラメータは上記と同じです。

rectangle(matImage,Point(20,200),Point(200,300),Scalar(255,0,0),1,1,0);  
 //Rect(int a,int b,int c,int d) a,b are the coordinates of the upper left corner of the rectangle, c,d are the length and width of the rectangle  
rectangle(matImage,Rect(100,300,20,200),Scalar(0,0,255),1,1,0); 


はじめに 行列クラス rec を使って、長方形の輪郭を描いたり、長方形を塗りつぶしたりします。

III. アプリケーション

1. 単純な矩形、厚さ指定矩形、塗りつぶし矩形の描画

void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color,
                  int thickness=1, int line_type=8, int shift=0 );


img
    image .
pt1
    A vertex of the rectangle.
pt2
    The other vertex on the diagonal of the rectangle.
color
    The line color (RGB) or brightness (grayscale image).
thickness
    The thickness of the lines that make up the rectangle. With negative values (e.g. CV_FILLED) the function draws a rectangle with a color fill.
line_type
    The type of the line. See the description of cvLine
shift
    The number of decimal places of the coordinate points.
     


//Include the header files and namespaces used by the program
#include

#include

#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
//Macro definition section
#define Win_Name3 "Cross Red Cross"
//main function
int main(int argc,char** argv)
{	
<span style="white-space:pre"> </span>//draw the Red Cross
	Mat image3 = Mat::zeros(800, 800, CV_8UC3);//generate a window of 800x800
	Rect rec1 = Rect(100, 300, 600, 200);
	Rect rec2 = Rect(300, 100, 200, 600);
	rectangle (image3, rec1,Scalar(0, 0, 255), -1, 8, 0);//generate a horizontal rectangle
	rectangle (image3, rec2, Scalar(0, 0, 255), -1, 8, 0);//vertical rectangle
	rectangle (image3, Point(100, 300), Point(700, 500), Scalar(0, 255, 255), 2, 8, 0);//yellow rectangle with borders
	rectangle(image3, Point(300, 100), Point(500, 700), Scalar(0, 255, 255), 2, 8, 0);//yellow rectangle bordered
	rectangle(image3, Point(300, 300), Point(500, 500), Scalar(0, 0, 255), 3, 8);//red square overlay (central)
	imshow(Win_Name3, image3);
	waitKey();
	return 0;
}


2. Circleは円を描きます。

void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color,
               int thickness=1, int line_type=8, int shift=0 );


img
    Image.
center
    The coordinates of the center of the circle.
radius
    The radius of the circle.
color
    The color of the line.
thickness
    If positive, the thickness of the lines forming the circle. Otherwise, it indicates whether the circle is filled or not.
line_type
    The type of the line. See the description of cvLine
shift
    The number of decimal places of the center point and radius value.
    The cvCircle function draws or fills a circle with the given center and radius. The circle is cropped by the rectangle of interest. If you specify the color of the circle, you can use the macro CV_RGB ( r, g, b ).



#include
 //Include the header files and namespaces used by the program

#include

#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
#define Win_Name2 "Audi Audi"
int main(int argc, char** argv)
{
	// Draw the Audi Audi logo
	Mat image2 = Mat::zeros(600, 850, CV_8UC3);//generate a window of 850x600
	circle(image2, Point(200, 300), 100, Scalar(225, 0, 225), 7, 8);//draw the first circle, radius 100, center (200, 300), color purple
	circle(image2, Point(350, 300), 100, Scalar(225, 0, 225), 7, 8);//Draw the first circle, radius 100, center (350, 300), line width of 7
	circle(image2, Point(500, 300), 100, Scalar(225, 0, 225), 7, 8);//draw the first circle, radius 100, the center of the circle (500, 300)
	circle(image2, Point(650, 300), 100, Scalar(225, 0, 225), 7, 8);// draw the first circle, radius of 100, the center of the circle (650, 300)
	imshow(Win_Name2, image2);
	waitKey();
	return 0;
}


3. 楕円は、楕円弧と楕円セクターを描画します。

void cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle,
                double start_angle, double end_angle, CvScalar color,
                int thickness=1, int line_type=8, int shift=0 );


img
    Image.
center
    The coordinates of the center of the ellipse.
axes
    The length of the axis.
angle
    The angle of the deflection.
start_angle
    The angle of the start angle of the arc.
end_angle
    The angle of the end of the arc.
color
    The color of the line.
thickness
    The thickness of the line.
line_type
    The type of the line, see the description of CVLINE.
shift
    The precision of the center point and the number axis.

The cvEllipse function is used to draw or fill a simple elliptical arc or elliptical sector. Circular arcs are ignored by ROI rectangles. Anti-walking arcs and coarse arcs use linear segment approximations. All angles are given in the form of angles.



//---------------- drawing of basic geometric images ---------------
//Include the header files and namespaces used by the program
#include

#include

#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
#define Win_Name1 "Toyota Toyota" 
//Macro definition section

//main function
int main(int argc, char** argv)
{
	//draw the Toyota logo
	Mat image1 = Mat::zeros(900, 900, CV_8UC3);//900x900 window
	ellipse(image1, Point(450, 450), Size(400, 250), 0, 0, 360, Scalar(0, 0, 225), 5, 8);//draw the first ellipse, large ellipse, color is red
	ellipse(image1, Point(450, 450), Size(250, 110), 90, 0, 360, Scalar(0, 0, 225), 5, 8);//draw the second ellipse, vertical ellipse
	ellipse(image1, Point(450, 320), Size(280, 120), 0, 0, 360, Scalar(0, 0, 225), 5, 8);//draw the third ellipse, small ellipse (horizontal)
	imshow(Win_Name1, image1);
	waitKey();
	return 0;
}



<ブロッククオート

https://blog.csdn.net/wwwlyj123321/article/details/80563114
https://blog.csdn.net/sinat_34707539/article/details/51912610