python opencv裁剪数据增强

目录

按照面积筛选随机裁剪:

左右裁剪0.05-0.95范围:


按照面积筛选随机裁剪:

                max_area = a_w* h_o
                max_xywh = (x, y, a_w, h_o)

                img_crop = source[y:y + h_o, x:x + a_w]
                # cv2.imshow("img_crop", img_crop)
                img_h, img_w = img_crop.shape[:2]

                while True:
                    x_1 = random.randint(0,int(img_w*0.7 ))
                    y_1 = random.randint(0, int(img_h*0.7))
                    x_2 = random.randint(int(img_w * (0.35)), int(img_w ))
                    y_2 = random.randint(int(img_h * 0.35), int(img_h))
                    if (x_2-x_1)*(y_2-y_1)>max_area*0.5:
                        break
                img_crop = img_crop[y_1:y_2, x_1:x_2]

左右裁剪0.05-0.95范围:

import glob
import random

import cv2

import numpy as np


dir=r'F:\data'

files=glob.glob(dir+'/*.jpg')


big_count=0
file_len=len(files)
for file in files:

    img_raw = cv2.imread(file)

    area_o=f"{img_raw.shape[1]*img_raw.shape[0]}_{max(img_raw.shape[1],img_raw.shape[0])}"
    if img_raw.shape[1]*img_raw.shape[0] > 200*300:
        x_scale = 224 / max(img_raw.shape[1],img_raw.shape[0])

        img_raw = cv2.resize(img_raw, None, fx=x_scale, fy=x_scale, interpolation=cv2.INTER_AREA)

    cv2.imshow("img_raw", img_raw)

    img_h,img_w=img_raw.shape[:2]

    x_1=random.randint(0, int(img_w*0.05))
    y_1=random.randint(0, int(img_h*0.05))

    x_2=random.randint( int(img_w*0.95),img_w)
    y_2=random.randint( int(img_h*0.95),img_h)
    img_crop=img_raw[y_1:y_2,x_1:x_2] #子区域

    t_h, t_w = img_crop.shape[:2]

    if t_h*t_w>50*200:
        big_count+=1

    print(file_len,big_count)
    a_w = max(t_w, t_h)
    a_w = max(a_w,200)
    img_b = np.zeros((a_w, a_w, 3), dtype=np.uint8)
    img_b[:t_h, :t_w, :] = img_crop
    img = cv2.resize(img_b,(128,128))

    cv2.imwrite(r'F:\chuli/'+area_o+".jpg",img_b)
    # cv2.imshow("img_crop", img)
    # cv2.waitKey()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值