python如何利用中心坐标绘制矩形
作者:绍琳的男朋友
这篇文章主要介绍了python如何利用中心坐标绘制矩形问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
python利用中心坐标绘制矩形
python利用matplotlib库根据中心坐标与方位角绘制矩形
import matplotlib.pyplot as plt
import math
def convert_center_to_leftdown(x_cord, y_cord, angle, width, height):
xp = x_cord-(math.sqrt(width**2+height**2)/2)*math.cos(math.atan2(height, width)+angle/180*math.pi)
yp = y_cord-(math.sqrt(width**2+height**2)/2)*math.sin(math.atan2(height, width)+angle/180*math.pi)
return xp, yp
fig1 = plt.figure() # 确保正方形在屏幕上显示一致,固定figure的长宽相等
axes1 = fig1.add_subplot(1, 1, 1)
# 输入矩形中心坐标,矩形方位角(逆时针为正)以及矩形的宽与高即可绘制矩形
x = 0.5
y = 0.5
phi = -30
w = 0.2
h = 0.1
xp, yp = convert_center_to_leftdown(x, y, phi, w, h)
square = plt.Rectangle(xy=(xp, yp), width=w, height=h, alpha=0.8, angle=phi)
axes1.add_patch(square) # 把图形加载到绘制区域
axes1.axis('equal')
axes1.grid()
plt.show()
opencv-python绘制矩形框
函数
cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
img:要画的圆所在的矩形或图像pt1:矩形左上角的点pt2:矩形右下角的点color:线条颜色,如 (0, 0, 255) 红色,BGRthickness:线条宽度
lineType:
- 8 (or omitted) : 8-connected line
- 4:4-connected line
- CV_AA - antialiased line
shift:坐标点小数点位数
import numpy as np
import cv2 as cv
img = np.zeros((320, 320, 3), np.uint8) #生成一个空灰度图像
print img.shape # 输出:(320, 320, 3)
# 矩形左上角和右上角的坐标,绘制一个绿色矩形
ptLeftTop = (60, 60)
ptRightBottom = (260, 260)
point_color = (0, 255, 0) # BGR
thickness = 1
lineType = 4
cv.rectangle(img, ptLeftTop, ptRightBottom, point_color, thickness, lineType)
# 绘制一个红色矩形
ptLeftTop = (120, 100)
ptRightBottom = (200, 150)
point_color = (0, 0, 255) # BGR
thickness = 1
lineType = 8
cv.rectangle(img, ptLeftTop, ptRightBottom, point_color, thickness, lineType)
cv.namedWindow("AlanWang")
cv.imshow('AlanWang', img)
cv.waitKey (10000) # 显示 10000 ms 即 10s 后消失
cv.destroyAllWindows()总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
