数据预处理_图像扩充

图像扩充

# coding = utf-8
import cv2
from PIL import Image
from PIL import ImageEnhance
from numpy.ma import array
import numpy as np
import os
# 批量处理代码
rootdir = 'E://TeacherZZYdata//class//D2//100-4//' # 指明被遍历的文件夹

def operate(currentPath, filename, targetPath):
    # 读取图像
    image = Image.open(currentPath)
    image_cv = cv2.imread(currentPath)
    # image.show()
    # 增强亮度 bh_
    enh_bri = ImageEnhance.Brightness(image)
    brightness = 1.07
    image_brightened_h = enh_bri.enhance(brightness)
    # image_brightened_h.show()
    image_brightened_h.save(targetPath + 'D2/a/' + filename)  # 保存

    # 降低亮度 bl_
    enh_bri_low = ImageEnhance.Brightness(image)
    brightness = 0.87
    image_brightened_low = enh_bri_low.enhance(brightness)
    # image_brightened_low.show()
    image_brightened_low.save(targetPath + 'D2/b/' + filename)

    # 改变色度 co_
    enh_col = ImageEnhance.Color(image)
    color = 0.8
    image_colored = enh_col.enhance(color)
    # image_colored.show()
    image_colored.save(targetPath + 'D2/c/' + filename)

    # 改变对比度 cont_
    enh_con = ImageEnhance.Contrast(image)
    contrast = 0.8
    image_contrasted = enh_con.enhance(contrast)
    # image_contrasted.show()
    image_contrasted.save(targetPath + 'D2/d/' + filename)

    # 改变锐度 sha_
    enh_sha = ImageEnhance.Sharpness(image)
    sharpness = 3.0
    image_sharp = enh_sha.enhance(sharpness)
    # image_sharp.show()
    image_sharp.save(targetPath + 'D2/e/' + filename)

    # y方向上的缩放 yre_
    # image.show()
    w = image.width
    h = image.height
    print(w, h)
    out_ww = image.resize((w, h + 40))  # 拉伸成高为h的正方形
    # out_ww.show()
    out_ww_1 = np.array(out_ww)
    out_w_2 = out_ww_1[30:(h - 10), 0:w]  # 开始的纵坐标,开始的横坐标
    out_w_2 = Image.fromarray(out_w_2)
    # out_w_2.show()
    out_w_2.save(targetPath + 'D2/f/' + filename)

    # x方向上的缩放 xre_
    # image.show()
    out_hh = image.resize((w + 80, h))  # 拉伸成宽为w的正方形,width,height
    # out_hh.show()
    out_hh_1 = array(out_hh)
    out_h_2 = out_hh_1[0:h, 40:(w + 40)]
    out_h_2 = Image.fromarray(out_h_2)
    # out_h_2.show()
    out_h_2.save(targetPath + 'D2/g/' + filename)

    # x左方向的平移 xl_
    # 平移矩阵[[1,0,-10][0,1,-12]]
    # image.show()
    w = image.width
    h = image.height
    M = np.array([[1, 0, -80], [0, 1, 0]], dtype=np.float32)
    image_cv_change = cv2.warpAffine(image_cv, M, (w, h))
    image_cv_change_RGB = cv2.cvtColor(image_cv_change, cv2.COLOR_BGR2RGB)
    image_cv_change = Image.fromarray(image_cv_change_RGB)
    # image_cv_change.show()
    image_cv_change.save(targetPath + 'D2/h/' + filename)

    # x右方向的平移 xr_
    # image.show()
    w = image.width
    h = image.height
    M = np.array([[1, 0, 80], [0, 1, 0]], dtype=np.float32)
    image_cv_change = cv2.warpAffine(image_cv, M, (w, h))
    image_cv_change_RGB = cv2.cvtColor(image_cv_change, cv2.COLOR_BGR2RGB)
    image_cv_change = Image.fromarray(image_cv_change_RGB)
    # image_cv_change.show()
    image_cv_change.save(targetPath + 'D2/i/' + filename)


for parent, dirnames, filenames in os.walk(rootdir):
    for filename in filenames:
        # print('parent is:' + parent)
        print('filename is: ' + filename)
        # 把文件名添加到一起后输出
        currentPath = os.path.join(parent, filename)
        # print('the full name of file is :' + currentPath)
        # 保存处理后的图片的目标文件夹
        targetPath = 'E:/picture/'
        # 进行处理
        operate(currentPath, filename, targetPath)
### 图像检测中的数据预处理方法和技术 在图像检测领域,数据预处理是一个至关重要的环节。它直接影响到模型的性能和最终的结果准确性。以下是几种常见的数据预处理技术和方法: #### 1. 归一化 归一化是一种常用的数据标准化技术,其目的是将图像像素值调整到特定范围(通常是[0, 1]或[-1, 1]),以便于神经网络更好地学习特征[^2]。通过这种方式,可以减少数值差异对模型的影响。 ```python import numpy as np def normalize_image(image): image = image.astype(np.float32) normalized_image = (image - np.min(image)) / (np.max(image) - np.min(image)) return normalized_image ``` #### 2. 数据增强 为了提高模型的泛化能力,可以通过多种方式对原始数据集进行扩充。这些方法包括但不限于旋转、平移、缩放、翻转等操作。数据增强能够有效增加训练样本的数量和多样性。 ```python from imgaug import augmenters as iaa seq = iaa.Sequential([ iaa.Fliplr(0.5), # 水平翻转 iaa.Affine(rotate=(-20, 20)), # 随机旋转角度 ]) augmented_images = seq(images=original_images) ``` #### 3. 尺寸调整 由于大多数深度学习框架要求输入图像具有固定的大小,因此通常需要对不同分辨率的图像进行统一尺寸调整。这一步骤可通过插值法实现,例如双线性插值或最近邻插值[^3]。 ```python import cv2 def resize_image(image, target_size=(416, 416)): resized_image = cv2.resize(image, target_size, interpolation=cv2.INTER_LINEAR) return resized_image ``` #### 4. 边界框标注转换 对于目标检测任务而言,除了处理图像本身外,还需要同步修改对应的边界框位置信息。当执行诸如裁剪或者变换的操作时,务必更新相应的标签文件以匹配新的坐标系。 ```python def preprocess_annotation(image, bboxes, size): ih, iw = image.shape[:2] h, w = size scale = min(w/iw, h/ih) nw, nh = int(iw*scale), int(ih*scale) dx = (w-nw)//2 dy = (h-nh)//2 new_bboxes = [] for bbox in bboxes: x_min, y_min, x_max, y_max = bbox x_min = max(int((x_min * scale) + dx), 0) y_min = max(int((y_min * scale) + dy), 0) x_max = min(int((x_max * scale) + dx), w-1) y_max = min(int((y_max * scale) + dy), h-1) new_bboxes.append([x_min, y_min, x_max, y_max]) return new_bboxes ``` #### 总结 上述提到的各种技术均能在一定程度上改善图像质量并优化后续分析流程的效果。然而需要注意的是,在实际应用过程中可能还需针对具体应用场景做出适当调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值