查看yolov5标注

这篇博客展示了如何使用Python的OpenCV和PIL库处理图像,包括读取图像、从文本文件获取坐标数据、进行图像旋转、绘制矩形框以及保存切片。代码示例涵盖了对图像的标注和切片操作,适用于训练数据集的准备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import cv2
from PIL import Image
import numpy as np
import os

for file in os.listdir('/home/lixuan/Downloads/payactions/scanandpay/images/train'):
    imgpath = '/home/lixuan/Downloads/payactions/scanandpay/images/train/{}'.format(file)
    txtpath = '/home/lixuan/Downloads/payactions/scanandpay/labels/train/{}'.format(file.replace('jpg','txt'))

    img = cv2.imread(imgpath)
    datas = open(txtpath).read().strip().split(' ')[1:]
    datas = [float(data) for data in datas]
    datas[0] *= img.shape[1]
    datas[1] *= img.shape[0]
    datas[2] *= img.shape[1]
    datas[3] *= img.shape[0]
    x = img.shape[1] - datas[0]
    y = img.shape[0] - datas[1]
    w = datas[2]
    h = datas[3]
    img = Image.fromarray(img)
    img = img.transpose(Image.ROTATE_180)
    img = np.array(img)
    x1 = int(x - w / 2)
    y1 = int(y - h / 2)
    x2 = int(x1 + w)
    y2 = int(y1 + h)
    cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,255),3)
    cv2.namedWindow('img',0)
    cv2.imshow('img',img)
    cv2.waitKey(0)
import os
import cv2

count = 0
for file in os.listdir('/home/lixuan/workspace/dataset/scenesDec/images/train'):
    img = cv2.imread('/home/lixuan/workspace/dataset/scenesDec/images/train/{}'.format(file))
    H,W,_ = img.shape
    with open('/home/lixuan/workspace/dataset/scenesDec/labels/train/{}'.format(file.replace('jpg','txt'))) as f:
        datas = f.readlines()
        for data in datas:
            data = data.strip().split(' ')
            label = int(data[0]) + 1
            x = float(data[1]) * W
            y = float(data[2]) * H
            w = float(data[3]) * W
            h = float(data[4]) * H
            x1 = int(x - w / 2)
            y1 = int(y - h / 2)
            x2 = int(x1 + w)
            y2 = int(y1 + h)
            patch = img[y1:y2,x1:x2]
            cv2.imwrite('/home/lixuan/workspace/gz/{}/patch_{}.jpg'.format(label,count),patch)
            count += 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值