python CV2 int8 和 float转换

python CV2 int8 和 float转换

在使用时有些像素点表达是使用的float,有些是int8,试过x255的方法会导致输出图像出现差异,不能直接转换,查阅很多资料后总结该方法可以有效的修改类型

# Float to int8
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR, cv2.CV_8UC3)
#
#int8 to float
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR, cv2.CV_32FC3)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Yolo格式中,每个目标都用一个包含其类别、中心点、宽度和高度的矩形来表示。在COCO格式中,每个目标都用一个包含其所有像素的掩码来表示。因此,将Yolo格式转换为COCO格式需要通过计算目标的像素掩码来实现。 以下是将Yolo格式转换为COCO格式的Python代码示例: ``` import numpy as np import cv2 # Yolo格式坐标转换为COCO格式坐标 def yolo2coco(bbox, img_width, img_height): x, y, w, h = bbox left = int((x - w / 2) * img_width) right = int((x + w / 2) * img_width) top = int((y - h / 2) * img_height) bottom = int((y + h / 2) * img_height) return [left, top, right, bottom] # COCO格式坐标转换为Yolo格式坐标 def coco2yolo(bbox, img_width, img_height): left, top, right, bottom = bbox x = (left + right) / 2 / img_width y = (top + bottom) / 2 / img_height w = (right - left) / img_width h = (bottom - top) / img_height return [x, y, w, h] # 读取Yolo格式标注文件 def read_yolo_annotation(annotation_file): with open(annotation_file, 'r') as f: lines = f.readlines() annotations = [] for line in lines: line = line.strip() if not line: continue parts = line.split() class_id = int(parts[0]) bbox = [float(x) for x in parts[1:]] annotations.append((class_id, bbox)) return annotations # 将Yolo格式标注转换为COCO格式标注 def yolo2coco_annotation(yolo_annotation, img_width, img_height): coco_annotation = [] for class_id, bbox in yolo_annotation: coco_bbox = yolo2coco(bbox, img_width, img_height) mask = np.zeros((img_height, img_width), dtype=np.uint8) cv2.rectangle(mask, (coco_bbox[0], coco_bbox[1]), (coco_bbox[2], coco_bbox[3]), 1, thickness=-1) coco_annotation.append({'category_id': class_id, 'bbox': coco_bbox, 'segmentation': mask}) return coco_annotation # 将COCO格式标注转换为Yolo格式标注 def coco2yolo_annotation(coco_annotation, img_width, img_height): yolo_annotation = [] for ann in coco_annotation: class_id = ann['category_id'] coco_bbox = ann['bbox'] yolo_bbox = coco2yolo(coco_bbox, img_width, img_height) yolo_annotation.append((class_id, yolo_bbox)) return yolo_annotation ``` 其中,`yolo2coco`函数将Yolo格式的坐标转换为COCO格式的坐标,`coco2yolo`函数将COCO格式的坐标转换为Yolo格式的坐标。`read_yolo_annotation`函数从Yolo格式的标注文件中读取标注信息,`yolo2coco_annotation`函数将Yolo格式的标注转换为COCO格式的标注,`coco2yolo_annotation`函数将COCO格式的标注转换为Yolo格式的标注。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值