這一小節首先介紹一下Point
與Scalar
兩個類的使用,以及介紹如何在圖像上繪制一些幾何形狀和添加文字, 具體包括: 線, 矩形, 圓, 橢圓, 多邊形等
完整的代碼在最下方;
Point與Scalar
Point
Point
表示的是二維平面上的一個點,包含兩個屬性\(x, y\);
使用方式如下:
// 構建一個點
Point pt = Point(10, 20);
// 或者
Point pt;
pt.x = 10;
pt.y = 20;
Scalar
Scalar
表示的是一個向量, 其本質是一個由長度為4的數組作為元素的結構體;
typedef struct Scalar{
double val[4];
}Scalar;
使用場景在下面繪制幾何圖形時會用到;
繪制幾何圖形
繪制幾何圖形包括線, 矩形, 橢圓, 圓,多邊形
畫線
關於line
的API如下:
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
int thickness = 1, int lineType = LINE_8, int shift = 0);
img
表示需要繪制線的圖像;pt1
與pt2
表示線段的兩端;color
表示線的顏色,以Scalar
進行定義;thickness
表示線的粗細;lineType
表示線的類型;
在圖像上繪制一條線, API的使用:
// 畫線方法
void MyLine(Mat src){
Point p1 = Point(10, 20);
Point p2;
p2.x = 300;
p2.y = 300;
Scalar color = Scalar(0, 0, 255);
//畫線
line(src, p1, p2, color, 2, LINE_8);
}
- 首先創建線段兩個端點的位置,利用
Point
; - 其次利用Scalar創建一個顏色;
繪制矩形
關於rectangle
的API介紹如下:
void rectangle(InputOutputArray img, Rect rec,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
img
輸入圖像rec
表示一個矩形類,下面介紹color
,thickness
,lineType
,shift
含義與上述相同;
關於Rect
的API介紹如下:
class Rect_{
public:
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
}
- 其中
_x, _y
分別表示矩形的左上角坐標,_width, _height
表示矩形的寬度和高度;
舉例, 在一張圖像上繪制矩形:
// 繪制矩形方法
void MyRectangle(Mat src){
Rect rect = Rect(200, 100, 300, 300);
Scalar color = Scalar(0, 0, 255);
// 繪制矩形
rectangle(src, rect, color, 2, LINE_8);
}
- 首先,構建矩形
rect
; - 其次繪圖;
繪制橢圓
有關ellipse
繪制橢圓的API介紹如下:
void ellipse(InputOutputArray img, Point center, Size axes,
double angle, double startAngle, double endAngle,
const Scalar& color, int thickness = 1,
nt lineType = LINE_8, int shift = 0);
img
為輸入圖像;center
表示橢圓中心;axes
分別表示橢圓橫,縱向長度;angle
表示順時針旋轉多少度;startAngle
與endAngle
配合使用,表示從左上角逆時針,繪制多少度的橢圓;- 后面參數與前述相同;
舉例,在一張圖像上繪制橢圓:
// 繪制橢圓
void MyEllipse(Mat src){
Point center = Point(src.rows/2, src.cols/2);
Size size = Size(src.cols/4, src.rows/8);
Scalar color = Scalar(0, 0, 255);
ellipse(src, center, size, 30, 0, 360, color, LINE_8);
}
- 構造中心點
- 橢圓橫,縱長度
- 顏色
繪制圓
有關circle
的API的介紹如下:
void circle(InputOutputArray img, Point center, int radius,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
img
表示需要繪制的圖像;center
表示中心點坐標;radius
表示半徑大小;- 其他如前述一致;
舉例,在圖像上繪制一個圓:
// 繪制圓
void MyCircle(Mat src){
Scalar color = Scalar(0, 0, 255);
Point center = Point(src.rows/4, src.cols/2);
int radius = 100;
// 繪制圓
circle(src, center, radius, color, 2, LINE_8);
}
繪制多邊形
有關繪制多邊形fillPoly
API的使用介紹如下:
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() );
img
多邊形將要繪制到該圖像上;pts
表示多邊形的頂點集npts
表示頂點數目;ncontours
表示要繪制的多邊形數量- 其他與前述一致
舉例,在圖像中繪制一個多邊形,如下:
// 繪制多邊形
void MyPolygon(Mat src){
Point pts[1][5];
pts[0][0] = Point(100, 100);
pts[0][1] = Point(100, 200);
pts[0][2] = Point(200, 200);
pts[0][3] = Point(200, 100);
pts[0][4] = Point(100, 100);
const Point* ppts[] = {pts[0]};
int npt[] = {5};
Scalar color = Scalar(255, 0, 255);
// 繪制多邊形
fillPoly(src, ppts, npt, 1, color, LINE_8);
}
pts
頂點集,ppts
指針
隨機畫線
舉例,在一張圖像上隨機畫出一些線段;
// 隨機畫線
void RandomLineDemo(Mat src){
// 新的圖像
Mat dst = Mat::zeros(src.size(), src.type());
RNG rng(12345);
Point pt1;
Point pt2;
namedWindow("dst", WINDOW_AUTOSIZE);
for (int i = 0; i < 10000000; i++){
pt1.x = rng.uniform(0, src.cols);
pt2.x = rng.uniform(0, src.cols);
pt1.y = rng.uniform(0, src.rows);
pt2.y = rng.uniform(0, src.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); // 隨機產生顏色
// 停止條件
if (waitKey(50) > 0) {
break;
}
line(dst, pt1, pt2, color, 1, LINE_8);
imshow("dst", dst);
}
}
添加文字
在圖像中添加文字,putText
方法的使用介紹如下:
void putText( InputOutputArray img, const String& text, Point org,
int fontFace, double fontScale, Scalar color,
int thickness = 1, int lineType = LINE_8,
bool bottomLeftOrigin = false );
img
表示將要添加文字的圖像;text
表示需要添加的文本org
表示文件左下角坐標位置;fontFace
表示字體類型fontScale
表示問題縮放尺度,有一個默認的大小;color
表示圖像顏色,其他如前述一致;
舉例,在一張圖像中繪制一段文字;
putText(src,
"Hello OpenCV",
Point(100, 100),
FONT_HERSHEY_COMPLEX,
0.8,
Scalar(0, 255, 255), 2, LINE_8);
完整代碼如下:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void MyLine(Mat src); // 畫線方法
void MyRectangle(Mat src); // 繪制矩形方法
void MyEllipse(Mat src); // 繪制橢圓
void MyCircle(Mat src); // 繪制圓
void MyPolygon(Mat src); // 繪制多邊形
void RandomLineDemo(Mat src); // 隨機畫線
int main(){
// 讀取圖像
Mat src = imread("/home/chen/dataset/lena.jpg");
if (src.empty()){
printf("could not load image.\n");
return -1;
}
// 在圖像上畫線
MyLine(src);
// 在圖像上畫矩形
MyRectangle(src);
// 在凸顯上繪制橢圓
MyEllipse(src);
// 繪制圓
MyCircle(src);
// 繪制多邊形
MyPolygon(src);
// 寫入文字
// 字體左下端的坐標
putText(src, "Hello OpenCV", Point(100, 100), FONT_HERSHEY_COMPLEX, 0.8, Scalar(0, 255, 255), 2, LINE_8);
// 隨機畫線
RandomLineDemo(src);
namedWindow("src", WINDOW_AUTOSIZE);
imshow("src", src);
waitKey(0);
return 0;
}
// 畫線方法
void MyLine(Mat src){
Point p1 = Point(10, 20);
Point p2;
p2.x = 300;
p2.y = 300;
Scalar color = Scalar(0, 0, 255);
//畫線
line(src, p1, p2, color, 2, LINE_8);
}
// 繪制矩形方法
void MyRectangle(Mat src){
Rect rect = Rect(200, 100, 300, 300);
Scalar color = Scalar(0, 0, 255);
// 繪制矩形
rectangle(src, rect, color, 2, LINE_8);
}
// 繪制橢圓
void MyEllipse(Mat src){
Point center = Point(src.rows/2, src.cols/2);
Size size = Size(src.cols/4, src.rows/8);
Scalar color = Scalar(0, 0, 255);
// 繪制橢圓
// 第一個參數表示繪圖
// 第二個參數表示橢圓中心
// 第三個參數分別表示橫軸, 縱軸長度,用Size類型表示
// 第四個參數表示順時針傾斜角度
// 第五,六個參數表示從左上角逆時針開始繪制,繪制多少角度
ellipse(src, center, size, 30, 0, 360, color, LINE_8);
}
// 繪制圓
void MyCircle(Mat src){
Scalar color = Scalar(0, 0, 255);
Point center = Point(src.rows/4, src.cols/2);
int radius = 100;
// 繪制圓
circle(src, center, radius, color, 2, LINE_8);
}
// 繪制多邊形
void MyPolygon(Mat src){
Point pts[1][5];
pts[0][0] = Point(100, 100);
pts[0][1] = Point(100, 200);
pts[0][2] = Point(200, 200);
pts[0][3] = Point(200, 100);
pts[0][4] = Point(100, 100);
const Point* ppts[] = {pts[0]};
int npt[] = {5};
Scalar color = Scalar(255, 0, 255);
// 繪制多邊形
fillPoly(src, ppts, npt, 1, color, LINE_8);
}
// 隨機畫線
void RandomLineDemo(Mat src){
// 新的圖像
Mat dst = Mat::zeros(src.size(), src.type());
RNG rng(12345);
Point pt1;
Point pt2;
namedWindow("dst", WINDOW_AUTOSIZE);
for (int i = 0; i < 10000000; i++){
pt1.x = rng.uniform(0, src.cols);
pt2.x = rng.uniform(0, src.cols);
pt1.y = rng.uniform(0, src.rows);
pt2.y = rng.uniform(0, src.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); // 隨機產生顏色
// 停止條件
if (waitKey(50) > 0) {
break;
}
line(dst, pt1, pt2, color, 1, LINE_8);
imshow("dst", dst);
}
}