OpenCV的繪圖函數可以在任意深度的圖像上工作,但在大多數情況下,它們只對圖像的前三個通道有影響BGR,如果是單通道圖像,則默認隻影響第一個通道。大多數繪圖函數都支持操作對象的顏色、寬度、線型和亞像素對齊等參數。
畫直線或者其他圖形的函數通常可以接受寬度(thickness)和線型(lineType)的參數。這兩個參數都是整型的,lineType參數只能是4、8或cv::LINE_AA,分別代表4鄰域連接、8鄰域連接和平滑處理。
void cv::circle(InputOutputArray img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=LINE_8, int shift=0)
參數:
參數 | img | center | radius | color | thickness | lineType | shift |
---|---|---|---|---|---|---|---|
含義 | 繪製的圓 | 圓心 | 半徑 | 顏色 | 線寬 | 線型 | 小數位數 |
bool cv::clipLine(Size imgSize, Point& pt1, Point& pt2)
bool cv::clipLine(Size2I imgSize, Point& pt1, Point& pt2)
bool cv::clipLine(Rect imgRect, Point& pt1, Point& pt2)
參數:
參數 | imgSize或imgRect | pt1 | pt2 |
---|---|---|---|
含義 | 指定矩陣 | 第一個點的座標 | 第二個點的座標 |
注:前兩種形式只指定矩形的大小,默認從(0,0)點開始。
void cv::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)
void cv::ellipse(InputOutputArray img, const RotatedRect& box, const Scalar& color, int thickness=1, int lineType=LINE_8)
參數:
參數 | img | center | axes | angle | startAngle | endAngle | color | thickness | lineType | shift |
---|---|---|---|---|---|---|---|---|---|---|
含義 | 繪製的橢圓 | 中心 | 橢圓尺寸 | 主軸角度 | 圓弧起點 | 圓弧終點 | 顏色 | 線寬 | 線型 | 小數位數 |
第二種形式其實是根據外接矩形繪製橢圓。
void cv::ellipse2Poly(Point center, Size axes, int angle, int arcStart, int arcEnd, int delta, std::vector<Point>& pts)
void cv::ellipse2Poly(Point2d center, Size2d axes, int angle, int arcStart, int arcEnd, int delta, std::vector<Point>& pts)
參數:
參數 | center | axes | angle | startAngle | endAngle | delta | pts |
---|---|---|---|---|---|---|---|
含義 | 圓心 | 中心 | 主軸角度 | 圓弧起點 | 圓弧終點 | 折線頂點間的角度,定精度 | 結果 |
void cv::fillConvexPoly(Mat& img, const Point* pts, int npts, const Scalar& color, int lineType=LINE_8, int shift=0)
void cv::fillConvexPoly(InputOutputArray img, InputArray pts, int npts, const Scalar& color, int lineType=LINE_8, int shift=0)
參數:
參數 | img | pts | npts | color | thickness | lineType | shift |
---|---|---|---|---|---|---|---|
含義 | 繪製的多邊形 | 頂點集 | pts中的點數 | 顏色 | 線寬 | 線型 | 小數位數 |
void cv::fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=LINE_8, int shift=0, Point offset=Point())
void cv::fillPoly(InputOutputArray img, InputArrayOfArrays pts, const Scalar& color, int lineType=LINE_8, int shift=0, Point offset=Point())
參數:
參數 | img | pts | npts | ncontours | color | thickness | lineType | shift | offset |
---|---|---|---|---|---|---|---|---|---|
含義 | 繪製的多邊形 | 頂點集 | pts中的點數 | 多邊形數目 | 顏色 | 線寬 | 線型 | 小數位數 | 頂點偏移量 |
第二種形式,pts中有多少個元素就有多少個多邊形。
void cv::line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=LINE_8,int shift=0)
參數:
參數 | img | pt1 | pt2 | color | thickness | lineType | shift |
---|---|---|---|---|---|---|---|
含義 | 繪製的直線 | 第一個點 | 第二個點 | 顏色 | 線寬(4或8) | 線型 | 小數位數 |
void cv::polyLines(Mat& img, const Point* const* pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=LINE_8,int shift=0)
void cv::polyLines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar& color, int thickness=1, int lineType=LINE_8,int shift=0)
參數:
參數 | img | pts | npts | ncontours | isClosed | color | thickness | lineType | shift | offset |
---|---|---|---|---|---|---|---|---|---|---|
含義 | 繪製的多邊形 | 頂點集 | pts中的點數 | 多邊形數目 | 是否封閉 | 顏色 | 線寬(4或8) | 線型 | 小數位數 | 頂點偏移量 |
第二種形式,pts中有多少個元素就有多少個多邊形。isClosed爲false則不封閉。
void cv::rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=LINE_8,int shift=0)
void cv::rectangle(InputOutputArray img, Rect rec, const Scalar& color, int thickness=1, int lineType=LINE_8,int shift=0)
參數:
參數 | img | pt1 | pt2 | rec | color | thickness | lineType | shift |
---|---|---|---|---|---|---|---|---|
含義 | 繪製的矩形 | 第一個點 | 第二個點 | 矩形的座標和大小 | 顏色 | 線寬 | 線型 | 小數位數 |
cv::LineIterator::LineIterator(const Mat& img, Point pt1, Point pt2, int connectivity=8, bool leftToRight=false)
參數:
參數 | img | pt1 | pt2 | connectivity | leftToRight |
---|---|---|---|---|---|
含義 | 圖像矩陣 | 第一個點 | 第二個點 | 線型(4或8) | 是否從左到右 |
分析:初始化後,直線像素的數量就保存在一個整型成員變量cv::LineIterator::count中,重載的取值算子cv::LineIterator::operator*()返回一個指向當前像素的指針。從直線的一端開始,通過重載增量算子cv::LineIterator::operator++()移動當前像素。
void cv::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 | thickness | lineType | bottomLeftOrigin |
---|---|---|---|---|---|---|---|---|---|
含義 | 原始圖片 | 文本內容 | 左上角的位置 | 字體 | 字體大小 | 顏色 | 線寬 | 線型(4,或8) | 是否左下角爲原點 |
標識符 | 描述 |
---|---|
cv::FONT_HERSHEY_SIMPLEX | 普通大小無襯線字體 |
cv::FONT_HERSHEY_PLAN | 小號無襯線字體 |
cv::FONT_HERSHEY_DUPLEX | 普通大小無襯線字體,但比cv::FONT_HERSHEY_SIMPLEX更復雜 |
cv::FONT_HERSHEY_COMPLEX | 普通大小無襯線字體,但比cv::FONT_HERSHEY_DUPLEX更復雜 |
cv::FONT_HERSHEY_TRIPLEX | 普通大小無襯線字體,但比cv::FONT_HERSHEY_COMPLEX更復雜 |
cv::FONT_HERSHEY_COMPLEX_SMALL | 小號版本的cv::FONT_HERSHEY_COMPLEX |
cv::FONT_HERSHEY_SCRIPT_SIMPLEX | 手寫體 |
cv::FONT_HERSHEY_SCRIPT_COMPLEX | 比cv::FONT_HERSHEY_SCRIPT_SIMPLEX更復雜的變體 |
cv::FONT_HERSHEY_ITALIC | 斜體,可以和以上每一種組合使用 |
Size cv::getTextSize(const String& text, int fontFace, double fontScale, int thickness, int* baseLine)
參數:
參數 | text | fontFace | fontScale | thickness | baseLine |
---|---|---|---|---|---|
含義 | 文本 | 字體 | 字體大小 | 線寬 | 文字最低點文字基線的y座標值 |