Opencv绘制基本图形

Opencv基本运用

1.线段绘制

  • 方法
    cv2.line(img, pt1, pt2, color, thickness, lineType, shift )
  • 参数:
    1、img:图像;
    2、pt1:线段的第一个点。
    3、pt2:线段的第二个点;
    4、color:颜色线颜色。
    5、thickness:厚度线厚度。
    6、lineType:线条的线型类型。请参见“线型”。
    7、shift:点坐标中小数位数的移位数
  • 与左上角为坐标原点,坐标为(x , y)
import cv2
import numpy as np

#定义图片的宽高信息
newimage = (500, 500, 3)
image = np.zeros(newimage, np.uint8)
# 绘制线段
#参数:1、dst; 2、起始位置; 3、结束位置; 4、颜色
cv2.line(image, (0, 0), (400,400), (200, 200, 200))
#金黄色竖线,比较粗
#参数:1、dst; 2、起始位置; 3、结束位置; 4、颜色; 5、厚度线厚度
cv2.line(image, (100,500), (100, 200), (0, 255, 255), 20)
#蓝色竖线,尖端比较平滑
#参数:1、dst; 2、起始位置; 3、结束位置; 4、颜色; 5、厚度线厚度; 6、线条的线型类型
cv2.line(image, (150,500), (150, 200), (255, 0, 0), 20, cv2.LINE_AA)
#参数:1、dst; 2、起始位置; 3、结束位置; 4、颜色; 5、厚度线厚度; 6、线条的线型类型
cv2.line(image, (250,500), (250, 200), (0, 0, 255), 20, cv2.LINE_AA, 1)
# 用直线画矩形
cv2.line(image, (100,200), (300, 200), (0, 255, 0), 2)
cv2.line(image, (300,200), (300, 400), (0, 255, 0), 2)
cv2.line(image, (300,400), (100, 400), (0, 255, 0), 2)
cv2.line(image, (100,400), (100, 200), (0, 255, 0), 2)
cv2.imshow('line', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

2.矩形绘制

  • 方法
    cv2.rectangle(img, pt1, pt2, color, thickness, lineType, shift)
  • 参数:
    1、img:图像;
    2、pt1:矩形的顶点。
    3、pt2:相对的矩形的顶点;
    4、color:矩形颜色或亮度(灰度图像)。
    5、thickness:矩形的线条的厚度。负值,填充。
    6、lineType:线型。
    7、shift:点坐标中小数位数的参数移位数。
#定义图片的宽高信息
newimage = (500, 500, 3)
image = np.zeros(newimage, np.uint8)
cv2.rectangle(image, (50,50),(450,450), (0, 255, 255), 2)
cv2.imshow('正方形', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

3.椭圆和扇形绘制

  • 方法
    cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness, lineType, shift)
  • 参数:
    1、img:图像;
    2、center:椭圆的中心。
    3、axes:主轴尺寸的一半。
    4、angle:旋转角度(度)。
    5、startAngle:起始角。
    6、endAngle:结束角。
    4、color:颜色或亮度(灰度图像)。
    5、thickness:轮廓厚度。负值,填充。
    6、lineType:线型。
    7、shift:点坐标中小数位数的参数移位数。
newimage = (500, 500, 3)
image = np.zeros(newimage, np.uint8)
# 椭圆
cv2.ellipse(image, (150,150), (150,80), 0, 0, 360, (0, 255, 0), 2)
cv2.ellipse(image, (150,150), (150,80), 60, 0, 360, (0, 255, 0), 2)
# 弧形
cv2.ellipse(image, (350,350), (150,80), 180, 0, 180, (0, 255, 0), -1)
cv2.imshow('ellipse', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

4.绘制圆形

  • 方法
    circle(img, center, radius, color, thickness, lineType, shift)
  • 参数:
    1、img:图像;
    2、center:圆的中心。
    3、radius:圆半径。
    4、color:圆颜色或亮度(灰度图像)。
    5、thickness:轮廓厚度。负值,填充。
    6、lineType:线型。
    7、shift:点坐标中小数位数的参数移位数。
newimage = (500, 500, 3)
image = np.zeros(newimage, np.uint8)
cv2.circle(image, (150,150), 100, (255, 255, 255), 2)
cv2.circle(image, (300,150), 50, (255, 255, 255), -1)
cv2.imshow('circle', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

5.任意形状图形的绘制

  • 方法
    cv2.polylines(img, pts, isClosed, color, thickness, lineType, shift)
  • 参数
    1、img:图像;
    2、pts:多边形曲线的数组。
    3、isClosed:指示绘制的多段线是否闭合。
    4、color:圆颜色或亮度(灰度图像)。
    5、thickness:轮廓厚度。
    6、lineType:线型。
    7、shift:点坐标中小数位数的参数移位数。
newimage = (500, 500, 3)
image = np.zeros(newimage, np.uint8)
#指定图形中的各个点
points = np.array([[150, 50], [140, 140], [200, 170], [280, 250],[150, 90], [150,50]], np.int32)
points = points.reshape((-1, 1, 2))
cv2.polylines(image, [points], True, (255, 255, 255), 2)
cv2.imshow('polylines', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用OpenCV绘制图形可以通过调用不同的函数来实现。在Python中,可以使用cv2.line()来绘制线段,cv2.rectangle()来绘制矩形,cv2.circle()来绘制圆形,cv2.ellipse()来绘制椭圆,cv2.polylines()来绘制多边形,以及cv2.putText()来绘制文本。这些函数可以在计算机视觉任务和图像处理中非常有用。例如,可以使用cv2.circle()函数绘制圆形。 在OpenCV中,绘制图形的步骤通常包括创建一个画布,并在其上使用相应的函数绘制所需的图形。通过指定参数如颜色、线条宽度和填充等,可以对图形进行进一步的定制。例如,可以指定圆心坐标、半径和颜色来绘制一个圆形。 下面是一个示例代码,展示了如何使用OpenCV绘制图形: #include <opencv2/opencv.hpp> using namespace cv; int main() { // 载入图片 Mat srcImage = imread("dota.jpg", 1); Mat dstImage; // 转换颜色空间 cvtColor(srcImage, dstImage, COLOR_BGR2Lab); // 显示效果图 imshow("效果图", dstImage); waitKey(0); } 这个示例代码使用了cv2的imread()函数来载入一张图片,然后使用cvtColor()函数将图片转换为Lab颜色空间。最后,使用imshow()函数显示转换后的效果图。 希望这个回答能够帮助您了解如何使用OpenCV绘制图形。如果还有其他问题,请随时提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【CV 向】OpenCV 图形绘制指南](https://blog.csdn.net/qq_21484461/article/details/131333236)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [OpenCV—学习基本绘图](https://blog.csdn.net/qq_44859533/article/details/125160651)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值