OpenCV 雖是開源的計算機視覺庫,但里面也有一些基礎的繪圖函數,本文將介紹幾種常用繪圖函數:直線、圓、橢圓、長方形、多邊形等。
1 數據結構
1.1 二維向量
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
1.2 四維向量
cv::Scalar 代表的是四維向量,常用來傳遞像素值,尤其是 BGR 通道的像素值 (最后一個元素不用,則不定義)
$\texttt{Scalar} (blue \_ component, green \_ component, red \_ component)$
2 繪圖函數
2.1 line()
OpenCV 中,繪制直線段較簡單,就是過兩點畫一條直線,函數為 line()
// 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)
2.2 circle() 和 ellipse()
知道圓心和半徑,就可以繪制圓了,函數為 circle()
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()
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 )
2.3 rectangle()
長方形的繪制,主要是靠其對角線上的兩個點 pt1 和 pt2,函數為 rectangle()
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 )
2.4 fillpoly()
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() )
3 代碼示例
3.1 直線和長方形
#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); }
3.2 圓和橢圓
// 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));
3.3 多邊形
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 ));
3.4 顯示效果
參考資料:
OpenCV Tutorials / imgproc module / Basic Drawing