一、繪制形狀與文字的模闆類:
1、Point模闆類(二維):
解析:為了描述圖像中的點,opencv提供了由坐标“x”和“y”指定的 二維點 的模闆類,該類的一個執行個體可以與C結構、CvPoint和CvPoint2D32f互換。有也是一個cast操作符,用于将點坐标轉換為指定類型。從通過舍入将浮點坐标轉換為整數坐标。通常,轉換對每個坐标使用此操作
pt1 = pt2 + pt3;
pt1 = pt2 - pt3;
pt1 = pt2 * a;
pt1 = a * pt2;
pt1 = pt2 / a;
pt1 += pt2;
pt1 -= pt2;
pt1 *= a;
pt1 /= a;
double value = norm(pt); // L2 norm
pt1 == pt2;
pt1 != pt2;
為友善起見,定義了以下類型别名:
typedef Point_<int> Point2i;
typedef Point2i Point;
typedef Point_<float> Point2f;
typedef Point_<double> Point2d;
舉例:
Point2f a(0.3f, 0.f), b(0.f, 0.4f);
Point pt = (a + b)*10.f;
cout << pt.x << ", " << pt.y << endl;
三維的點坐标,我們可以自行學習:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5CO0IzNxUTM3Y2MkRTZiZTYyYzXxIDNzITMzEzLcZDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
2、Scalar向量模闆類:
解析:從Vec派生的4元素向量的模闆類,從Vec<_Tp, 4> , Scalar_ 和 Scalar派生而來,可以像典型的4元素一樣使用向量。此外,還可以将它們轉換為CvScalar。OpenCV傳遞像素值。
//進行執行個體化,直接使用color使用建立的像素值
Scalar color = Scalar(0, 150, 15);
3、Rect二維矩陣模闆類:
解析:二維矩形的簡明模闆類;左上角的坐标開始,建構一個矩陣
OpenCV通常假定矩形的上邊界和左邊界是包含的,而右邊界和下邊界不相同。
//構造函數:
Rect_();
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
Rect_(const Rect_& r);
Rect_(Rect_&& r) CV_NOEXCEPT;
Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);
Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);
//參數:
_Tp x;//左上角的x坐标
_Tp y;//左上角的y坐标
_Tp width;//矩形的寬度
_Tp height;//矩形的高度
矩形的其他操作:
-\f$\texttt{rect} = \texttt{rect} \pm \texttt{point}\f$ (将矩形移動一定的偏移量)
-\f$\texttt{rect} = \texttt{rect} \pm \texttt{size}\f$ 通過一定數量)
-rect+=point,rect-=point,rect+=size,rect-=size(增廣運算)
-rect=rect1&rect2(矩形相交)
-rect=rect1 | rect2(包含rect1和rect2的最小面積矩形)
-rect&=rect1,rect |=rect1(及相應的增廣運算)
-rect==rect1,rect!=rect1(矩形比較)
4、RNG類
解析:第一個窗體将狀态設定為預定義值,在目前實作中等于2**32-1。這個第二種形式将狀态設定為指定值。如果通過state=0,構造函數使用上述預設值來避免奇異随機數序列,由全零組成。
構造函數:
RNG();
/** @overload
@param state 64-bit value used to initialize the RNG.
*/
RNG(uint64 state);
uniform()方法:
int uniform(int a, int b);
/** @overload */
float uniform(float a, float b);
/** @overload */
double uniform(double a, double b);
//傳回[a,b)範圍内均勻分布的整數随機數這些方法使用MWC算法進行狀态轉換,
//并傳回下一個指定類型的均勻布随機數從輸入參數類型,從範圍[a,b]。
二、繪制形狀與文字的函數:
1、line()函數:
簡而言之:繪制從第一個點指向第二個點的箭頭段。
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
int thickness = 1, int lineType = LINE_8, int shift = 0);
/*
img 圖像.
pt1 pt1箭頭的起始點.
pt2 pt2箭頭指向的點.
color 線條顔色.
thickness 線條厚度.
line_type line的類型.
shift Number of fractional bits in the point coordinates.點坐标中小數位數的移位
tipLength 相對于箭頭長度的箭頭尖端長度
*/
line()函數線的類型:
enum LineTypes {
FILLED = -1,
LINE_4 = 4, //!< 4-connected line
LINE_8 = 8, //!< 8-connected line
LINE_AA = 16 //!< antialiased line抗鋸齒
};
2、rectangle()函數:
簡而言之:繪制簡單的、粗的或填充的右矩形。rectangle繪制一個矩形輪廓或兩個對角的填充矩形是pt1和pt2。
rectangle()函數的多态:
CV_EXPORTS_W void rectangle(InputOutputArray img, Point pt1, Point pt2,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
/*use `rec` parameter as alternative specification of the drawn rectangle: `r.tl() and
r.br()-Point(1,1)` are opposite corners*/
CV_EXPORTS_W void rectangle(InputOutputArray img, Rect rec,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
/** @example samples/cpp/tutorial_code/ImgProc/basic_drawing/Drawing_2.cpp
An example using drawing functions*/
3、ellipse()函數:
簡而言之,繪制簡單或粗的橢圓弧或填充橢圓扇區。eliple繪制橢圓輪廓、填充橢圓和橢圓,圓弧或實心橢圓扇形。采用分段線性曲線逼近橢圓弧邊界。
ellipse()函數的多态:
CV_EXPORTS_W void ellipse(InputOutputArray img, Point center, Size axes,
double angle, double startAngle, double endAngle,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
CV_EXPORTS_W void ellipse(InputOutputArray img, const RotatedRect& box, const Scalar& color,
int thickness = 1, int lineType = LINE_8);
4、circle()函數:
簡而言之:畫了一個圓。circle用給定的圓心和半徑繪制一個簡單的或填充的圓。
circle()函數的構造:
CV_EXPORTS_W void circle(InputOutputArray img, Point center, int radius,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
5、fillPoly()函數:
簡而言之:填充凸多邊形,fillconverxpoly繪制一個填充的凸多邊形。這個函數比函數#fillPoly。它不僅可以填充凸多邊形,而且可以填充任何單調多邊形自相交,即輪廓與每條水準線(掃描線)相交的多邊形最多兩次(不過,其頂部和/或底部邊緣可能是水準的)。
fillPoly()函數的構造:
CV_EXPORTS void fillPoly(InputOutputArray img, const Point** pts,
const int* npts, int ncontours,
const Scalar& color, int lineType = LINE_8, int shift = 0,
Point offset = Point() );
三、在圖像上繪制形狀和文字:
1、繪制線條:
代碼塊:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat bgImage;
const char* dravdemo_win = "draw shapes and text demo!!!";
void Myline() {
Point p1 = Point(0, 0);
Point p2;
p2.x = bgImage.cols;
p2.y = bgImage.rows;
Scalar color = Scalar(0, 0, 255);
line(bgImage, p1, p2, color, 1, LINE_4);
}
int main() {
bgImage = imread("D:\\Myfile\\素材照片\\opencv素材照片\\4.jpg");
if (!bgImage.data) {
cout << "could not load image..." << endl;
return 0;
}
Myline();
namedWindow(dravdemo_win, WINDOW_AUTOSIZE);
imshow(dravdemo_win, bgImage);
waitKey(0);
return 0;
}
運作結果:
2、繪制矩形
代碼塊:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat bgImage;
const char* dravdemo_win = "draw shapes and text demo!!!";
void MyRectangle() {
Rect rect = Rect(66, 25, 73, 72);
Scalar color = Scalar(255, 0, 0);
rectangle(bgImage, rect, color, 2, LINE_8);
rect= Rect(142, 60, 55, 60);
color = Scalar(0, 255, 0);
rectangle(bgImage, rect, color, 2, LINE_8);
rect = Rect(210, 62, 55, 55);
color = Scalar(0, 0, 255);
rectangle(bgImage, rect, color, 2, LINE_8);
}
int main() {
bgImage = imread("D:\\Myfile\\素材照片\\opencv素材照片\\8.jpg");
if (!bgImage.data) {
cout << "could not load image..." << endl;
return 0;
}
MyRectangle();
namedWindow(dravdemo_win, WINDOW_AUTOSIZE);
imshow(dravdemo_win, bgImage);
waitKey(0);
return 0;
}
運作結果:
3、繪制橢圓與圓
代碼塊:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat bgImage;
const char* dravdemo_win = "draw shapes and text demo!!!";
void MyEllipse() {
Scalar color = Scalar(0, 255, 255);
ellipse(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), Size(bgImage.cols / 4, bgImage.rows / 4), 90, 0, 360, color, 2, LINE_8);
}
void MyCircle() {
Scalar color = Scalar(255, 0, 255);
circle(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), 30, color, 2, LINE_8);
}
int main() {
bgImage = imread("D:\\Myfile\\素材照片\\opencv素材照片\\8.jpg");
if (!bgImage.data) {
cout << "could not load image..." << endl;
return 0;
}
MyEllipse();
MyCircle();
namedWindow(dravdemo_win, WINDOW_AUTOSIZE);
imshow(dravdemo_win, bgImage);
waitKey(0);
return 0;
}
運作結果:
4、繪制凸多邊形
代碼塊:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat bgImage;
const char* dravdemo_win = "draw shapes and text demo!!!";
void MyPolygon() {
Point pts[1][5];
pts[0][0] = Point(10, 10);
pts[0][1] = Point(10, 40);
pts[0][2] = Point(200, 200);
pts[0][3] = Point(200, 10);
pts[0][4] = Point(10, 10);
const Point* ppts[] = { pts[0] };
int npt[] = { 5 };
Scalar color = Scalar(0, 150, 15);
fillPoly(bgImage, ppts, npt, 1, color, 8);
}
int main() {
bgImage = imread("D:\\Myfile\\素材照片\\opencv素材照片\\4.jpg");
if (!bgImage.data) {
cout << "could not load image..." << endl;
return 0;
}
MyPolygon();
namedWindow(dravdemo_win, WINDOW_AUTOSIZE);
imshow(dravdemo_win, bgImage);
waitKey(0);
return 0;
}
運作結果:
5、寫文字
代碼塊:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
Mat bgImage;
const char* dravdemo_win = "draw shapes and text demo!!!";
bgImage = imread("D:\\Myfile\\素材照片\\opencv素材照片\\6.jpg");
if (!bgImage.data) {
cout << "could not load image..." << endl;
return 0;
}
putText(bgImage, "Hello five-size-seven!", Point(10, 20), FONT_HERSHEY_DUPLEX, 1.0, Scalar(120, 255, 120), 1, 4); namedWindow(dravdemo_win, WINDOW_AUTOSIZE);
namedWindow(dravdemo_win, WINDOW_AUTOSIZE);
imshow(dravdemo_win, bgImage);
waitKey(0);
return 0;
}
運作結果:
6、繪制随機線條
代碼塊:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat bgImage;
const char* dravdemo_win = "draw shapes and text demo!!!";
void RandomLineDemo() {
RNG rng(12345);
Point pt1;
Point pt2;
namedWindow("Randow_new_image", WINDOW_AUTOSIZE);
Mat new_bgImage = Mat::zeros(bgImage.size(), bgImage.type());
for (int i = 0; i < 100000; i++) {
pt1.x = rng.uniform(0, bgImage.cols);
pt2.x = rng.uniform(0, bgImage.cols);
pt1.y = rng.uniform(0, bgImage.rows);
pt2.y = rng.uniform(0, bgImage.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
if (waitKey(50) > 0)break;
line(new_bgImage, pt1, pt2, color, 1, 8);
imshow("Randow_new_image", new_bgImage);
}
}
int main() {
bgImage = imread("D:\\Myfile\\素材照片\\opencv素材照片\\4.jpg");
if (!bgImage.data) {
cout << "could not load image..." << endl;
return 0;
}
RandomLineDemo();
namedWindow(dravdemo_win, WINDOW_AUTOSIZE);
imshow(dravdemo_win, bgImage);
waitKey(0);
return 0;
}
運作結果:
四、總代碼塊
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat bgImage;
const char* dravdemo_win = "draw shapes and text demo!!!";
void Myline() {
Point p1 = Point(0, 0);
Point p2;
p2.x = bgImage.cols;
p2.y = bgImage.rows;
Scalar color = Scalar(0, 0, 255);
line(bgImage, p1, p2, color, 1, LINE_8);
}
void MyRectangle() {
Rect rect = Rect(66, 25, 73, 72);
Scalar color = Scalar(255, 0, 0);
rectangle(bgImage, rect, color, 2, LINE_8);
rect= Rect(142, 60, 55, 60);
color = Scalar(0, 255, 0);
rectangle(bgImage, rect, color, 2, LINE_8);
rect = Rect(210, 62, 55, 55);
color = Scalar(0, 0, 255);
rectangle(bgImage, rect, color, 2, LINE_8);
}
void MyEllipse() {
Scalar color = Scalar(0, 255, 255);
ellipse(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), Size(bgImage.cols / 4, bgImage.rows / 4), 90, 0, 360, color, 2, LINE_8);
}
void MyCircle() {
Scalar color = Scalar(255, 0, 255);
circle(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), 30, color, 2, LINE_8);
}
void MyPolygon() {
Point pts[1][5];
pts[0][0] = Point(10, 10);
pts[0][1] = Point(10, 40);
pts[0][2] = Point(20, 20);
pts[0][3] = Point(20, 10);
pts[0][4] = Point(10, 10);
const Point* ppts[] = { pts[0] };
int npt[] = { 5 };
Scalar color = Scalar(0, 150, 15);
fillPoly(bgImage, ppts, npt, 1, color, 8);
}
void RandomLineDemo() {
RNG rng(12345);
Point pt1;
Point pt2;
namedWindow("Randow_new_image", WINDOW_AUTOSIZE);
Mat new_bgImage = Mat::zeros(bgImage.size(), bgImage.type());
for (int i = 0; i < 100000; i++) {
pt1.x = rng.uniform(0, bgImage.cols);
pt2.x = rng.uniform(0, bgImage.cols);
pt1.y = rng.uniform(0, bgImage.rows);
pt2.y = rng.uniform(0, bgImage.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
if (waitKey(50) > 0)break;
line(new_bgImage, pt1, pt2, color, 1, 8);
imshow("Randow_new_image", new_bgImage);
}
}
int main() {
bgImage = imread("D:\\Myfile\\素材照片\\opencv素材照片\\8.jpg");
if (!bgImage.data) {
cout << "could not load image..." << endl;
return 0;
}
Myline();
MyRectangle();
MyEllipse();
MyCircle();
MyPolygon();
//RandomLineDemo();
putText(bgImage, "Hello Opencv", Point(10, 20), FONT_HERSHEY_PLAIN, 1.0, Scalar(120, 120, 120), 1, 4); namedWindow(dravdemo_win, WINDOW_AUTOSIZE);
namedWindow(dravdemo_win, WINDOW_AUTOSIZE);
imshow(dravdemo_win, bgImage);
waitKey(0);
return 0;
}
運作結果: