【无标题】

参考链接

#由原标签得出检测矩形框左上角和右下角的坐标分别为:xmin,ymin,xmax,ymax
def getBox(img_path, txt_path):
    """
    :param img_path: 图片文件的路径
    :param txt_path: 标签文件的路径
    :return:
    """
    img = cv2.imread(img_path)
    # 获取图片的高宽
    h, w, _ = img.shape
    # 读取TXT文件 中的中心坐标和框大小
    with open(txt_path, "r") as fp:
        # 以空格划分
        contline = fp.readline().split(' ')
    if len(contline)>1:
        # 计算框的左上角坐标和右下角坐标,使用strip将首尾空格去掉
        xmin = float((contline[1]).strip()) - float(contline[3].strip()) / 2
        xmax = float(contline[1].strip()) + float(contline[3].strip()) / 2

        ymin = float(contline[2].strip()) - float(contline[4].strip()) / 2
        ymax = float(contline[2].strip()) + float(contline[4].strip()) / 2

        # 将坐标(0-1之间的值)还原回在图片中实际的坐标位置
        xmin, xmax = w * xmin, w * xmax
        ymin, ymax = h * ymin, h * ymax

        return [xmin, ymin, xmax, ymax]
    else:
        return [0, 0, 0, 2, 2]
import os
import cv2

# 指定文件夹路径
img_folder = '/share1/luli/yolov8/test/images'
label_folder = '/share1/luli/yolov8/test/labels'

# 获取文件夹中所有jpg图片的文件名
jpg_files = [file for file in os.listdir(img_folder) if file.lower().endswith('.jpg')]

selected_images = jpg_files[:]
for image in selected_images:
    file_nmae = image.split('.')[0]
    img1 = os.path.join(img_folder, image)
    print(img1)
    img2 = cv2.imread(img1)
    img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)

    label_name = file_nmae + '.txt'
    label = os.path.join(label_folder, label_name)
    print(label)
    box = getBox(img1, label)

    cv2.rectangle(img2, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 255, 0), 2)
    temp = img_folder.replace('images','img_label')
    temp_image_path = os.path.join(temp, f"{file_nmae}_annotated.jpg")
    plt.imsave(temp_image_path,img2)
    # plt.imshow(img)
    # plt.show()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值