【12】opencv图形绘制
参考:https://blog.csdn.net/qq_28258885/article/details/112572873
线:
void line( InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0 ); @param img :输入图像。 @param pt1 :直线的第一个端点。 @param pt2 :直线的第二个端点。 @param color :直线的颜色。 @param thickness :直线的粗细程度。 @param lineType :直线类型,一共有三个:LINE_4,LINE_8,LINE_AA.其中LINE_AA是无锯齿直线。 这里就要注意,反锯齿就是在画完线后再进行渲染处理,占用资源也就更多。根据需要去选择是否使用。 @param shift :点坐标中的小数位数,一般用默认值。可以不写
方形以及椭圆:
void rectangle( Mat& img, Rect rec, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0 ); @param img :输入图像。 @param rec :一个矩形,包含四个参数,横纵坐标以及长宽。 @param color :直线的颜色。 @param thickness :直线的粗细程度。 @param lineType :直线类型,一共有三个:LINE_4,LINE_8,LINE_AA.其中LINE_AA是无锯齿直线。 @param shift :点坐标中的小数位数,一般用默认值。可以不写
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 ); @param img :输入图像。 @param center :椭圆中心。 @param axes :轴线。 @param angle :椭圆旋转角度,单位为度。 @param startAngle :椭圆弧的起始角,单位为度。 @param endAngle :椭圆弧的终止角,单位为度。 @param color :椭圆的颜色。 @param thickness :椭圆的粗细程度。 @param lineType :椭圆线类型,一共有三个:LINE_4,LINE_8,LINE_AA.其中LINE_AA是无锯齿直线。 @param shift :点坐标中的小数位数,一般用默认值。可以不写 若轴线的XY一样,那么这个椭圆就是一个圆
⚪
void circle( InputOutputArray img, Point center, int radius, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0 ); @param img:输入图像。 @param center:圆心。 @param radius:半径。 @param color :圆的颜色。 @param thickness :圆的粗细程度。 @param lineType :圆线的类型,一共有三个:LINE_4,LINE_8,LINE_AA.其中LINE_AA是无锯齿直线。 @param shift:点坐标中的小数位数,一般用默认值。可以不设置
设置填充
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() ); @param img:输入图像。 @param pts :多边形顶点。 @param npts:顶点个数。 @param ncontours:边的个数。 @param color:填充的颜色。 @param lineType :直线类型,一共有三个:LINE_4,LINE_8,LINE_AA.其中LINE_AA是无锯齿直线。 @param shift:点坐标中的小数位数,一般用默认值。可以不设置 @param offset:轮廓所有点的可选偏移。可以不设置
绘制文字
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 ); @param img :输入图像。 @param text:添加的文字。 @param org:图像中文本字符串的左下角。 @param fontFace:文字的字体。 @param color :文字的颜色。 @param thickness :文字的粗细程度。 @param lineType:文字的类型,一共有三个:LINE_4,LINE_8,LINE_AA.其中LINE_AA是无锯齿直线。 @param bottomLeftOrigin:如果为真,则图像数据原点位于左下角。否则,它在左上角。
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; int main() { //001 线 cv::Mat image = cv::imread("0002.jpg"); cv::Point a = cv::Point(0, 0); cv::Point b = cv::Point(360, 400); cv::Scalar c = cv::Scalar(48,129 ,173 ); cv::line(image, a, b, c, 90, cv::LINE_4); //002矩形 cv::Rect rect = cv::Rect(0, 0, 700, 700);//横纵长宽 cv::Scalar color = cv::Scalar(0, 255, 255); rectangle(image, rect, color, 10, cv::LINE_4); //003椭圆 cv::Point center = cv::Point(100, 100); cv::Size s1 = cv::Size(image.cols / 4, image.rows / 8); cv::Scalar color_2 = cv::Scalar(0, 0, 255); cv::ellipse(image, center, s1, 40, 0, 360, color_2, 4, cv::LINE_AA); //(TODO)椭圆这块还没明白 //004圆 cv::Point center_1 = cv::Point(120, 60); cv::Point center_2 = cv::Point(60, 120); cv::circle(image, center_1, 100, c, 80, cv::LINE_4); cv::circle(image, center_2, 100, c, 80, cv::LINE_4); //005填充操作 cv::Point pst[1][6]; pst[0][0] = cv::Point(120, 350); pst[0][1] = cv::Point(80, 370); pst[0][2] = cv::Point(100, 400); pst[0][3] = cv::Point(140, 400); pst[0][4] = cv::Point(160, 370); pst[0][5] = cv::Point(120, 350); const cv::Point* ppst[] = { pst[0] }; int npt[] = { 6 }; fillPoly(image, ppst, npt, 1, c, cv::LINE_4); //006输入文字 cv::Point location = cv::Point(10, image.rows - 10); string text = "prprprprprprprprprprpr"; putText(image, text, location, cv::FONT_HERSHEY_COMPLEX, 2, cv::Scalar(0, 0, 255), 3, 8); cv::imshow("【addLine】", image); cv::waitKey(0); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人