OpenCV 雖是開源的計算機視覺庫,但裏面也有一些基礎的繪圖函數,本文將介紹幾種經常使用繪圖函數:直線、圓、橢圓、長方形、多邊形等。html
cv::Point 表明的是二維點 (int 型),可用來表示圖像座標 (x, y)
數據結構
// one way
Point pt; pt.x = 10; pt.y = 8; // another way
Point pt = Point(10, 8);
OpenCV 中,二維點類型可分爲 Point2i, Point2l, Point2f, Point2d 四種,各自定義以下:函數
// 4 type of Point
typedef Point_<int> cv::Point2i typedef Point_<int64> cv::Point2l typedef Point_<float> cv::Point2f typedef Point_<double> cv::Point2d // cv::Point
typedef Point2i cv::Point
cv::Scalar 表明的是四維向量,經常使用來傳遞像素值,尤爲是 BGR 通道的像素值 (最後一個元素不用,則不定義)ui
$\texttt{Scalar} (blue \_ component, green \_ component, red \_ component)$spa
OpenCV 中,繪製直線段較簡單,就是過兩點畫一條直線,函數爲 line()
code
// pt1, first point
// pt2, second point
void cv::line ( InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0)
知道圓心和半徑,就能夠繪製圓了,函數爲 circle()
component
void cv::circle ( InputOutputArray img, Point center, // center of the circle int radius, // radius of the circle const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0 )
橢圓稍微複雜,橢圓中心,長、短軸半徑,以及橢圓弧的旋轉角度,則可獲得一段橢圓弧 ellipse()htm
void cv::ellipse ( InputOutputArray img, Point center, // center of the ellipse Size axes, // half size of the main axes double angle, // ellipse rotation angle in degrees double startAngle, double endAngle, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0 )
長方形的繪製,主要是靠其對角線上的兩個點 pt1 和 pt2,函數爲 rectangle()blog
void cv::rectangle ( InputOutputArray img, Point pt1, // vertex of the rectangle Point pt2, // vertex of the rectangle opposite to pt1 const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0 )
void cv::fillPoly ( InputOutputArray img, const Point ** pts, // const int * npts, // int ncontours, const Scalar & color, int lineType = LINE_8, int shift = 0, Point offset = Point() )
#include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> using namespace cv; #define w 300 int main() { // creat a white background image Mat img; img.create(w,w,CV_8UC3); img.setTo(Scalar(255,255,255)); // draw lines line(img, Point(w/4,w/4), Point(3*w/4, w/4), Scalar(255, 0, 0)); line(img, Point(w/6,w/2), Point(5*w/6, w/2), Scalar(0, 255, 0)); line(img, Point(w/10,3*w/4), Point(9*w/10, 3*w/4), Scalar(0, 0, 255)); // draw rectangle rectangle(img,Point(w/12,w/12),Point(11*w/12,9*w/10),Scalar(200,200,100)); // show lines in the image imshow("line and rectangle", img); waitKey(0); }
// draw circle and ellipse circle(img, Point(w/2,w/2), 50, Scalar(255, 0, 0)); ellipse(img, Point(w/2,w/2), Size(100,50), 0, 0, 360, Scalar(0, 255, 0));
Point rook_points[1][20]; rook_points[0][0] = Point( w/4, 7*w/8 ); rook_points[0][1] = Point( 3*w/4, 7*w/8 ); rook_points[0][2] = Point( 3*w/4, 13*w/16 ); rook_points[0][3] = Point( 11*w/16, 13*w/16 ); rook_points[0][4] = Point( 19*w/32, 3*w/8 ); rook_points[0][5] = Point( 3*w/4, 3*w/8 ); rook_points[0][6] = Point( 3*w/4, w/8 ); rook_points[0][7] = Point( 26*w/40, w/8 ); rook_points[0][8] = Point( 26*w/40, w/4 ); rook_points[0][9] = Point( 22*w/40, w/4 ); rook_points[0][10] = Point( 22*w/40, w/8 ); rook_points[0][11] = Point( 18*w/40, w/8 ); rook_points[0][12] = Point( 18*w/40, w/4 ); rook_points[0][13] = Point( 14*w/40, w/4 ); rook_points[0][14] = Point( 14*w/40, w/8 ); rook_points[0][15] = Point( w/4, w/8 ); rook_points[0][16] = Point( w/4, 3*w/8 ); rook_points[0][17] = Point( 13*w/32, 3*w/8 ); rook_points[0][18] = Point( 5*w/16, 13*w/16 ); rook_points[0][19] = Point( w/4, 13*w/16 );
const Point* ppt[1] = { rook_points[0]}; int npt[] = { 20 };
// draw polygon fillPoly(img, ppt, npt, 1, Scalar(0, 255, 255 ));
OpenCV Tutorials / imgproc module / Basic Drawingip