概述
官方文档:https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html
opencv画预测框的几种方式:
- cv2.boxPoints((x_c,y_c),(w,h),theta) 参考 参考2
# 中心点坐标(x_c,y_c) 宽w 高h 旋转角度theta
# 最小外接矩形,返回八个坐标值(x1,y1) (x2,y2) (x3,y3) (x4,y4)右下→左下→左上→右上
rect = cv2.boxPoints((x_c,y_c),(w,h),theta)
rect = np.int0(rect) # 取整
- cv2.rectangle(img, (240, 0), (480, 375), (0, 255, 0), 2) 参考
import cv2
img = cv2.imread(r"C:UsersAdministratorDesktop20151223182909577.png")
print(img.shape)
# 图片大小
cv2.rectangle(img, (240, 0), (480, 375), (0, 255, 0), 2)
cv2.imshow("fff", img)
- cv2.getRotationMatrix2D((cols/2,rows/2),90,1) 参考,该函数通过设置源图像和目标图像上的三组点以计算仿射变换 参考
for rotate_bbox in oriented_boxes:
coords = rotate_bbox[:8] #为旋转框四个点的坐标列表
center = (w/2, h/2)
#center = (0.5, 0.5)
# 得到实现变换映射的旋转矩阵2*3(为3个点的坐标组成)
matrix = cv2.getRotationMatrix2D(center, angle, scale)
# dx,dy为平移的偏移量
matrix[0, 2] += dx * w
matrix[1, 2] += dy * h
# 将坐标列表进行变换为4*3
trans_coords = np.hstack(
[np.array(coords).reshape(4, 2), np.ones((4, 1))])
# @为矩阵乘法,4*3的坐标列表@3*2的变换矩阵
coords = (trans_coords @ matrix.T).flatten().tolist()
tmp.append(tuple(coords))
oriented_boxes = tmp
- cv.polylines(imgBgr, [arrPt], True, (0, 255, 0), 3) 参考
bbox_point = [] # bbox中为四个点的坐标,1*8,为np数组 for i in range(int(len(bbox)/2)): bbox_point.append(bbox[2*i:2*i+2]) bbox_point = np.array(bbox_point, np.int32) # 将传入polylines的坐标从np数组打包为一个list传入。闭合图形,线条颜色和粗细。 img = cv2.polylines(img, [bbox_point], True, color, thickness)
在图片上绘制框的参考代码: https://albumentations.ai/docs/examples/example_bboxes2/
BOX_COLOR = (255, 0, 0) # Red TEXT_COLOR = (255, 255, 255) # White def visualize_bbox(img, bbox, class_name, color=BOX_COLOR, thickness=2): """Visualizes a single bounding box on the image""" x_min, y_min, w, h = bbox x_min, x_max, y_min, y_max = int(x_min), int(x_min + w), int(y_min), int(y_min + h) cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color=color, thickness=thickness) ((text_width, text_height), _) = cv2.getTextSize(class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.35, 1) cv2.rectangle(img, (x_min, y_min - int(1.3 * text_height)), (x_min + text_width, y_min), BOX_COLOR, -1) cv2.putText( img, text=class_name, org=(x_min, y_min - int(0.3 * text_height)), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.35, color=TEXT_COLOR, lineType=cv2.LINE_AA, ) return img def visualize(image, bboxes, category_ids, category_id_to_name): img = image.copy() for bbox, category_id in zip(bboxes, category_ids): class_name = category_id_to_name[category_id] img = visualize_bbox(img, bbox, class_name) plt.figure(figsize=(12, 12)) plt.axis('off') plt.imshow(img) image = cv2.imread('images/000000386298.jpg') image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) bboxes = [[5.66, 138.95, 147.09, 164.88], [366.7, 80.84, 132.8, 181.84]] category_ids = [17, 18] # We will use the mapping from category_id to the class name # to visualize the class label for the bounding box on the image category_id_to_name = {17: 'cat', 18: 'dog'} visualize(image, bboxes, category_ids, category_id_to_name)
cv2画圈:https://blog.csdn.net/viven_hui/article/details/102807995
最后
以上就是淡然红酒为你收集整理的opencv画预测框的几种方式的全部内容,希望文章能够帮你解决opencv画预测框的几种方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复