第一件事:OpenCV 基本圖形繪制
1. ellipse 函數(shù)(繪制橢圓)
函數(shù)原型:
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);
| 參數(shù) | 解釋 |
|---|---|
| InputOutPutArray img | 在 img 圖像上繪制 |
| Point center | 橢圓中心 |
| Size axes | axes 大小的矩形內(nèi)切 |
| double angle | 旋轉(zhuǎn)角度 |
| double startAngle | 開始繪制的角度 |
| double endAngle | 結(jié)束繪制的角度 |
| Scalar& color | 顏色 |
| int thickness | 線條厚度 |
| lineType | 線條邊緣類型(LINE_4(邊緣像素采用4連通,即上下左右),LINE_8(邊緣像素采用8連通,即上下左右還有四個對角),LINE_AA(邊緣像素采用高斯濾波,抗鋸齒)) |
| shift | 坐標點的小數(shù)點位數(shù) |
示例程序
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, const char * argv[]) {
cv::Mat img = cv::imread("1.jpg");
int thickness = 2;
cv::ellipse(img, cv::Point(img.cols/2, img.rows/2), cv::Size(img.cols/4, img.rows/8), 0.0, 0.0, 360.0, cv::Scalar(255,129,0),thickness,cv::LINE_AA);
cv::imshow("img", img);
cv::waitKey();
return 0;
}

ellipse 函數(shù)示例程序運行效果圖
2. circle 函數(shù)(繪制實心圓)
函數(shù)原型:
void circle(InputOutputArray img, Point center, int radius,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
| 參數(shù) | 解釋 |
|---|---|
| InputOutPutArray img | 在 img 圖像上繪制 |
| Point center | 圓心 |
| int radius | 半徑 |
| Scalar& color | 顏色 |
| int thickness | 線條厚度 |
| lineType | 線條邊緣類型(LINE_4(邊緣像素采用4連通,即上下左右),LINE_8(邊緣像素采用8連通,即上下左右還有四個對角),LINE_AA(邊緣像素采用高斯濾波,抗鋸齒)) |
| shift | 坐標點的小數(shù)點位數(shù) |
示例程序
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, const char * argv[]) {
cv::Mat img = cv::imread("1.jpg");
int thickness = 1;
cv::circle(img, cv::Point(img.cols/2,img.rows/2), 100, cv::Scalar(255,0,0),thickness,cv::LINE_AA);
cv::imshow("img", img);
cv::waitKey();
}

circle 函數(shù)示例程序運行效果圖
3. polygon 函數(shù)(繪制多邊形)
函數(shù)原型:
void fillPoly(Mat& img, const Point** pts,
const int* npts, int ncontours,
const Scalar& color, int lineType = LINE_8, int shift = 0,
Point offset = Point() );
| 參數(shù) | 解釋 |
|---|---|
| InputOutPutArray img | 在 img 圖像上繪制 |
| Point**pts | 二維頂點數(shù)組(每一個一維數(shù)組代表一個多邊形頂點集) |
| int* npts | 每個多邊形的頂點數(shù) |
| int ncontours | 多邊形數(shù)量 |
| Scalar& color | 顏色 |
| lineType | 線條邊緣類型(LINE_4(邊緣像素采用4連通,即上下左右),LINE_8(邊緣像素采用8連通,即上下左右還有四個對角),LINE_AA(邊緣像素采用高斯濾波,抗鋸齒)) |
| shift | 坐標點的小數(shù)點位數(shù) |
| offset | 多邊形偏移量 |
示例程序
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, const char * argv[]) {
cv::Mat img = cv::imread("1.jpg");
cv::Point rookPoints[] = {cv::Point(img.cols/4,img.rows*3/4),cv::Point(img.cols/4,img.rows/4),cv::Point(img.cols*3/4,img.rows*3/4)};
const cv::Point* points[1] = {rookPoints};
int npts[1] = {3};
cv::fillPoly(img, points, npts, 1, cv::Scalar(255,0,0),cv::LINE_AA);
cv::imshow("img", img);
cv::waitKey();
}

polygon 函數(shù)示例程序運行效果圖
4. line 函數(shù)(繪制線)
函數(shù)原型:
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
int thickness = 1, int lineType = LINE_8, int shift = 0);
| 參數(shù) | 解釋 |
|---|---|
| InputOutPutArray img | 在 img 圖像上繪制 |
| Point pt1 | 端點1 |
| Point pt2 | 端點2 |
| Scalar& color | 顏色 |
| int thickness | 線條厚度 |
| lineType | 線條邊緣類型(LINE_4(邊緣像素采用4連通,即上下左右),LINE_8(邊緣像素采用8連通,即上下左右還有四個對角),LINE_AA(邊緣像素采用高斯濾波,抗鋸齒)) |
| shift | 坐標點的小數(shù)點位數(shù) |
示例程序:
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, const char * argv[]) {
cv::Mat img = cv::imread("1.jpg");
int thickness = 5;
cv::line(img, cv::Point(img.cols/8,img.rows/2), cv::Point(img.cols*7/8,img.rows/4), cv::Scalar(255,0,0),thickness,cv::LINE_AA);
cv::imshow("img", img);
cv::waitKey();
}

line 函數(shù)示例程序運行效果圖