OpenCV入门4——实现图形的绘制


在这里插入图片描述
在这里插入图片描述

OpenCV绘制直线

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)
# 坐标点为(x, y)
cv2.line(img, (0, 0), (300, 400), (0, 255, 0), 1, 4)
cv2.line(img, (100, 0), (400, 600), (255, 0, 0), 5, 16)

cv2.imshow('draw', img)

key = cv2.waitKey(0)
if key & 0xff == ord('q'):
    cv2.destroyAllWindows()

在这里插入图片描述

OpenCV绘制矩形和圆

详情参看官方文档

画矩形

在这里插入图片描述
在这里插入图片描述

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)

# 画线
# 坐标点为(x, y)
# cv2.line(img, (0, 0), (300, 400), (0, 255, 0), 1, 4)
# cv2.line(img, (100, 0), (400, 600), (255, 0, 0), 5, 16)

# 画矩形
# (10, 10)represents the top left corner of rectangle 
# (100, 100) represents the bottom right corner of rectangle 
cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), 5)

cv2.imshow('draw', img)

key = cv2.waitKey(0)
if key & 0xff == ord('q'):
    cv2.destroyAllWindows()

在这里插入图片描述

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)

# 画线
# 坐标点为(x, y)
# cv2.line(img, (0, 0), (300, 400), (0, 255, 0), 1, 4)
# cv2.line(img, (100, 0), (400, 600), (255, 0, 0), 5, 16)

# 画矩形
# (10, 10)represents the top left corner of rectangle 
# (100, 100) represents the bottom right corner of rectangle 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), 5)
# Thickness of -1 will fill the entire shape 
cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), -1)

cv2.imshow('draw', img)

key = cv2.waitKey(0)
if key & 0xff == ord('q'):
    cv2.destroyAllWindows()

在这里插入图片描述

画圆

在这里插入图片描述

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)

# 画线
# 坐标点为(x, y)
# cv2.line(img, (0, 0), (300, 400), (0, 255, 0), 1, 4)
# cv2.line(img, (100, 0), (400, 600), (255, 0, 0), 5, 16)

# 画矩形
# (10, 10)represents the top left corner of rectangle 
# (100, 100) represents the bottom right corner of rectangle 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), 5)
# Thickness of -1 will fill the entire shape 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), -1)

# 画圆
cv2.circle(img, (320, 240), 30, (0, 255, 0), -1)

cv2.imshow('draw', img)

key = cv2.waitKey(0)
if key & 0xff == ord('q'):
    cv2.destroyAllWindows()

在这里插入图片描述

OpenCV椭圆的绘制

在这里插入图片描述
在这里插入图片描述

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)

# 画线
# 坐标点为(x, y)
# cv2.line(img, (0, 0), (300, 400), (0, 255, 0), 1, 4)
# cv2.line(img, (100, 0), (400, 600), (255, 0, 0), 5, 16)

# 画矩形
# (10, 10)represents the top left corner of rectangle 
# (100, 100) represents the bottom right corner of rectangle 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), 5)
# Thickness of -1 will fill the entire shape 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), -1)

# 画圆
# cv2.circle(img, (320, 240), 30, (0, 255, 0), -1)
cv2.circle(img, (320, 240), 100, (0, 255, 0))
cv2.circle(img, (320, 240), 5, (255, 0, 0), -1)

# 画椭圆
# 长方形的度若为0则是从右侧x正轴开始的,顺时针旋转
cv2.ellipse(img, (320, 240), (100, 50), 0, 0, 360, (0, 255, 0))

cv2.imshow('draw', img)

key = cv2.waitKey(0)
if key & 0xff == ord('q'):
    cv2.destroyAllWindows()

在这里插入图片描述

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)

# 画线
# 坐标点为(x, y)
# cv2.line(img, (0, 0), (300, 400), (0, 255, 0), 1, 4)
# cv2.line(img, (100, 0), (400, 600), (255, 0, 0), 5, 16)

# 画矩形
# (10, 10)represents the top left corner of rectangle 
# (100, 100) represents the bottom right corner of rectangle 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), 5)
# Thickness of -1 will fill the entire shape 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), -1)

# 画圆
# cv2.circle(img, (320, 240), 30, (0, 255, 0), -1)
cv2.circle(img, (320, 240), 100, (0, 255, 0))
cv2.circle(img, (320, 240), 5, (255, 0, 0), -1)

# 画椭圆
# 长方形的度若为0则是从右侧x正轴开始的,顺时针旋转
cv2.ellipse(img, (320, 240), (100, 50), 90, 45, 90, (0, 255, 0), -1)

cv2.imshow('draw', img)

key = cv2.waitKey(0)
if key & 0xff == ord('q'):
    cv2.destroyAllWindows()

在这里插入图片描述

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)

# 画线
# 坐标点为(x, y)
# cv2.line(img, (0, 0), (300, 400), (0, 255, 0), 1, 4)
# cv2.line(img, (100, 0), (400, 600), (255, 0, 0), 5, 16)

# 画矩形
# (10, 10)represents the top left corner of rectangle 
# (100, 100) represents the bottom right corner of rectangle 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), 5)
# Thickness of -1 will fill the entire shape 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), -1)

# 画圆
# cv2.circle(img, (320, 240), 30, (0, 255, 0), -1)
cv2.circle(img, (320, 240), 100, (0, 255, 0))
cv2.circle(img, (320, 240), 5, (255, 0, 0), -1)

# 画椭圆
# 长方形的度若为0则是从右侧x正轴开始的,顺时针旋转
cv2.ellipse(img, (320, 240), (100, 50), 15, 0, 360, (0, 255, 0))

cv2.imshow('draw', img)

key = cv2.waitKey(0)
if key & 0xff == ord('q'):
    cv2.destroyAllWindows()

在这里插入图片描述

OpenCV绘制多边形

在这里插入图片描述
点集必须为32位
在这里插入图片描述

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)

# 画线
# 坐标点为(x, y)
# cv2.line(img, (0, 0), (300, 400), (0, 255, 0), 1, 4)
# cv2.line(img, (100, 0), (400, 600), (255, 0, 0), 5, 16)

# 画矩形
# (10, 10)represents the top left corner of rectangle 
# (100, 100) represents the bottom right corner of rectangle 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), 5)
# Thickness of -1 will fill the entire shape 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), -1)

# 画圆
# cv2.circle(img, (320, 240), 30, (0, 255, 0), -1)
# cv2.circle(img, (320, 240), 100, (0, 255, 0))
# cv2.circle(img, (320, 240), 5, (255, 0, 0), -1)

# 画椭圆
# 长方形的度若为0则是从右侧x正轴开始的,顺时针旋转
# cv2.ellipse(img, (320, 240), (100, 50), 15, 0, 360, (0, 255, 0))

# 画多边形
pts = np.array([(300, 10), (150, 100), (450, 100)], np.int32)
cv2.polylines(img, [pts], True, (0, 255, 0))

cv2.imshow('draw', img)

key = cv2.waitKey(0)
if key & 0xff == ord('q'):
    cv2.destroyAllWindows()

在这里插入图片描述
在这里插入图片描述

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)

# 画线
# 坐标点为(x, y)
# cv2.line(img, (0, 0), (300, 400), (0, 255, 0), 1, 4)
# cv2.line(img, (100, 0), (400, 600), (255, 0, 0), 5, 16)

# 画矩形
# (10, 10)represents the top left corner of rectangle 
# (100, 100) represents the bottom right corner of rectangle 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), 5)
# Thickness of -1 will fill the entire shape 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), -1)

# 画圆
# cv2.circle(img, (320, 240), 30, (0, 255, 0), -1)
# cv2.circle(img, (320, 240), 100, (0, 255, 0))
# cv2.circle(img, (320, 240), 5, (255, 0, 0), -1)

# 画椭圆
# 长方形的度若为0则是从右侧x正轴开始的,顺时针旋转
# cv2.ellipse(img, (320, 240), (100, 50), 15, 0, 360, (0, 255, 0))

# 画多边形
pts = np.array([(300, 10), (150, 100), (450, 100)], np.int32)
cv2.polylines(img, [pts], True, (0, 255, 0))

# 填充多边形
cv2.fillPoly(img, [pts], (255, 255, 0))

cv2.imshow('draw', img)

key = cv2.waitKey(0)
if key & 0xff == ord('q'):
    cv2.destroyAllWindows()

在这里插入图片描述

OpenCV绘制文本

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

# -*- coding: utf-8 -*-
import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)

# 画线
# 坐标点为(x, y)
# cv2.line(img, (0, 0), (300, 400), (0, 255, 0), 1, 4)
# cv2.line(img, (100, 0), (400, 600), (255, 0, 0), 5, 16)

# 画矩形
# (10, 10)represents the top left corner of rectangle 
# (100, 100) represents the bottom right corner of rectangle 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), 5)
# Thickness of -1 will fill the entire shape 
# cv2.rectangle(img, (10, 10), (100, 100), (0, 255, 0), -1)

# 画圆
# cv2.circle(img, (320, 240), 30, (0, 255, 0), -1)
# cv2.circle(img, (320, 240), 100, (0, 255, 0))
# cv2.circle(img, (320, 240), 5, (255, 0, 0), -1)

# 画椭圆
# 长方形的度若为0则是从右侧x正轴开始的,顺时针旋转
# cv2.ellipse(img, (320, 240), (100, 50), 15, 0, 360, (0, 255, 0))

# 画多边形
# pts = np.array([(300, 10), (150, 100), (450, 100)], np.int32)
# cv2.polylines(img, [pts], True, (0, 255, 0))

# 填充多边形
# cv2.fillPoly(img, [pts], (255, 255, 0))

# 绘制文本
cv2.putText(img, "ILoveFS", (10, 400), cv2.FONT_HERSHEY_DUPLEX, 3, (0, 255, 0))

cv2.imshow('draw', img)

key = cv2.waitKey(0)
if key & 0xff == ord('q'):
    cv2.destroyAllWindows()

在这里插入图片描述

实现鼠标绘制基本图形

# -*- coding: utf-8 -*-
import cv2
import numpy as np
 
# 基本功能:
# 可以通过鼠标进行基本图形的绘制
# 1. 可以画线: 当用户按下l键, 即选择了画线。 此时, 滑动鼠标即可画线。
# 2. 可以画矩形: 当用户按下r键, 即可选择画矩形。 此时, 滑动鼠标即可画矩形。
# 3. 可以画圆: 当用户按下c键,即可选择画圆。 此时, 滑动鼠标即可画圆。

# curshape: 0-drawline, 1-drawrectangle, 2-drawcircle

# 显示窗口和背景
img = np.zeros((480, 640, 3), np.uint8)

curshape = 0
startpos = (0, 0)

# 创建窗口
cv2.namedWindow('mouse', cv2.WINDOW_NORMAL)

# 鼠标回调函数
def mouse_callback(event, x, y, flags, userdata):
    global startpos, curshape
    # print(event, x, y, flags, userdata)
    if(event & cv2.EVENT_LBUTTONDOWN == cv2.EVENT_LBUTTONDOWN):
        startpos = (x, y)
    elif(event & cv2.EVENT_LBUTTONUP == cv2.EVENT_LBUTTONUP):
        if curshape == 0:
            cv2.line(img, startpos, (x, y), (0, 255, 0))
        elif curshape == 1:
            cv2.rectangle(img, startpos, (x, y), (0, 255, 0))
        elif curshape == 2:
            a = (x - startpos[0])
            b = (y - startpos[1])
            r = int((a ** 2 + b ** 2) ** 0.5)
            cv2.circle(img, startpos, r, (0, 0, 255))
        else:
            print('error: no shape')
    
# 设置鼠标回调
cv2.setMouseCallback('mouse', mouse_callback, "666")

while True:
    cv2.imshow('mouse', img)
    key = cv2.waitKey(0) & 0xff
    if key == ord('q'):
        break
    elif key == ord('l'):
        curshape = 0
    elif key == ord('r'):
        curshape = 1
    elif key == ord('c'):
        curshape = 2

cv2.destroyAllWindows()

在这里插入图片描述

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值