最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)圖像的隨機增強變換

 更新時間:2024年11月12日 15:14:59   作者:Ysn0719  
這篇文章主要為大家介紹了如何利用pythons制作一個小工具工具,可以實現(xiàn)圖像的隨機增強變換,可用于分類訓(xùn)練數(shù)據(jù)的增強,有需要的可以參考下

從文件夾中隨機選擇一定數(shù)量的圖像,然后對每個選定的圖像進行一次隨機的數(shù)據(jù)增強變換。

import os
import random
import cv2
import numpy as np
from PIL import Image, ImageEnhance, ImageOps

# 定義各種數(shù)據(jù)增強方法
def random_rotate(image, angle_range=(-30, 30)):
    angle = random.uniform(angle_range[0], angle_range[1])
    (h, w) = image.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, angle, 1.0)
    rotated = cv2.warpAffine(image, M, (w, h), borderMode=cv2.BORDER_REFLECT)
    return rotated

def random_translate(image, translate_range=(-50, 50)):
    tx = random.randint(translate_range[0], translate_range[1])
    ty = random.randint(translate_range[0], translate_range[1])
    (h, w) = image.shape[:2]
    M = np.float32([[1, 0, tx], [0, 1, ty]])
    translated = cv2.warpAffine(image, M, (w, h), borderMode=cv2.BORDER_REFLECT)
    return translated

def random_flip(image):
    flip_code = random.choice([-1, 0, 1])
    flipped = cv2.flip(image, flip_code)
    return flipped

def random_scale(image, scale_range=(0.8, 1.2)):
    scale = random.uniform(scale_range[0], scale_range[1])
    (h, w) = image.shape[:2]
    new_dim = (int(w * scale), int(h * scale))
    scaled = cv2.resize(image, new_dim, interpolation=cv2.INTER_LINEAR)
    return scaled

def random_crop(image, crop_size=(224, 224)):
    (h, w) = image.shape[:2]
    if crop_size[0] > h or crop_size[1] > w:
        # 當(dāng)裁剪尺寸大于圖像尺寸時,拋出異常或調(diào)整裁剪尺寸
        raise ValueError("Crop size is larger than image size.")
    top = random.randint(0, h - crop_size[0])
    left = random.randint(0, w - crop_size[1])
    cropped = image[top:top+crop_size[0], left:left+crop_size[1]]
    return cropped

def random_color_jitter(image):
    pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    color_jitter = ImageEnhance.Color(pil_image).enhance(random.uniform(0.6, 1.4))
    contrast_jitter = ImageEnhance.Contrast(color_jitter).enhance(random.uniform(0.5, 1.5))
    brightness_jitter = ImageEnhance.Brightness(contrast_jitter).enhance(random.uniform(0.6, 1.4))
    sharpness_jitter = ImageEnhance.Sharpness(brightness_jitter).enhance(random.uniform(0.6, 1.4))
    jittered = cv2.cvtColor(np.array(sharpness_jitter), cv2.COLOR_RGB2BGR)
    return jittered

def random_add_noise(image):
    row, col, ch = image.shape
    mean = 0
    var = 0.1
    sigma = var ** 0.5
    gauss = np.random.normal(mean, sigma, (row, col, ch))
    gauss = gauss.reshape(row, col, ch)
    noisy = image + gauss
    return np.clip(noisy, 0, 255).astype(np.uint8)

# 數(shù)據(jù)增強主函數(shù)
def augment_random_images(src_folder, dst_folder, num_images_to_select, num_augmentations_per_image):
    if not os.path.exists(dst_folder):
        os.makedirs(dst_folder)

    # 獲取所有圖像文件名
    all_filenames = [f for f in os.listdir(src_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]

    # 如果選擇的圖像數(shù)量大于總圖像數(shù)量,則只處理全部圖像
    num_images_to_process = min(num_images_to_select, len(all_filenames))

    # 隨機選擇圖像
    selected_filenames = random.sample(all_filenames, num_images_to_process)

    # 創(chuàng)建一個增強方法列表
    augmentation_methods = [
        random_rotate,
        #random_translate,
        random_flip,
        random_scale,
        #random_crop,
        random_color_jitter,
        random_add_noise
    ]

    for filename in selected_filenames:
        img_path = os.path.join(src_folder, filename)
        image = cv2.imread(img_path)

        for i in range(num_augmentations_per_image):
            # 隨機選擇一種增強方法
            augmentation_method = random.choice(augmentation_methods)
            
            # 應(yīng)用選中的增強方法
            augmented_img = augmentation_method(image)

            # 保存增強后的圖像
            base_name, ext = os.path.splitext(filename)
            save_path = os.path.join(dst_folder, f"{base_name}_aug_{i}{ext}")
            cv2.imwrite(save_path, augmented_img)

if __name__ == "__main__":
    src_folder = 'path/to/source/folder'  # 替換為你的源文件夾路徑
    dst_folder = 'path/to/destination/folder'  # 替換為你要保存增強圖像的文件夾路徑
    num_images_to_select = 10  # 從源文件夾中隨機選擇的圖像數(shù)量
    num_augmentations_per_image = 5  # 每張圖像生成的增強圖像數(shù)量

    augment_random_images(src_folder, dst_folder, num_images_to_select, num_augmentations_per_image)
    print(f"圖像增強完成,增強后的圖像已保存到 {dst_folder}")

說明

  • 隨機選擇圖像:從源文件夾中隨機選擇num_images_to_select數(shù)量的圖像。
  • 隨機選擇一種增強方法:對于每張選定的圖像,隨機選擇一種數(shù)據(jù)增強方法。
  • 應(yīng)用增強方法:對每張選定的圖像應(yīng)用所選的增強方法。
  • 保存增強后的圖像:將增強后的圖像保存到目標(biāo)文件夾中。

參數(shù)

•src_folder:源文件夾路徑。

•dst_folder:目標(biāo)文件夾路徑。

•num_images_to_select:從源文件夾中隨機選擇的圖像數(shù)量。

•num_augmentations_per_image:每張選定的圖像生成的增強圖像數(shù)量。

請確保將src_folder和dst_folder變量設(shè)置為您實際使用的文件夾路徑,并根據(jù)需要調(diào)整num_images_to_select和num_augmentations_per_image的值。運行這段代碼后,將得到從源文件夾中隨機選擇的圖像,并對這些圖像進行了隨機的數(shù)據(jù)增強變換。

到此這篇關(guān)于python實現(xiàn)圖像的隨機增強變換的文章就介紹到這了,更多相關(guān)python圖像隨機增強內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python進程使用內(nèi)存后不釋放的解決

    Python進程使用內(nèi)存后不釋放的解決

    這篇文章主要介紹了Python進程使用內(nèi)存后不釋放的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 簡單理解Python中的裝飾器

    簡單理解Python中的裝飾器

    這篇文章主要介紹了Python中的裝飾器,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-07-07
  • 使用python如何實現(xiàn)泛型函數(shù)

    使用python如何實現(xiàn)泛型函數(shù)

    這篇文章主要介紹了使用python如何實現(xiàn)泛型函數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 如何利用Python合并兩張圖片

    如何利用Python合并兩張圖片

    在Python中可以使用PIL庫(Python Imaging Library)來合并兩張圖片,這篇文章主要給大家介紹了關(guān)于如何利用Python合并兩張圖片的相關(guān)資料,文中給了詳細的代碼示例,需要的朋友可以參考下
    2024-03-03
  • PyQt使用QPropertyAnimation開發(fā)簡單動畫

    PyQt使用QPropertyAnimation開發(fā)簡單動畫

    這篇文章主要介紹了PyQt使用QPropertyAnimation開發(fā)簡單動畫,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • pyenv與virtualenv安裝實現(xiàn)python多版本多項目管理

    pyenv與virtualenv安裝實現(xiàn)python多版本多項目管理

    這篇文章主要介紹了pyenv與virtualenv安裝實現(xiàn)python多版本多項目管理過程,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • Python實現(xiàn)投影法分割圖像示例(二)

    Python實現(xiàn)投影法分割圖像示例(二)

    今天小編就為大家分享一篇Python實現(xiàn)投影法分割圖像示例(二),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • 使用python來調(diào)用CAN通訊的DLL實現(xiàn)方法

    使用python來調(diào)用CAN通訊的DLL實現(xiàn)方法

    今天小編就為大家分享一篇使用python來調(diào)用CAN通訊的DLL實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • pyodps中的apply用法及groupby取分組排序第一條數(shù)據(jù)

    pyodps中的apply用法及groupby取分組排序第一條數(shù)據(jù)

    這篇文章主要介紹了pyodps中的apply用法及groupby取分組排序第一條數(shù)據(jù),問綻放圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值需要的小伙伴可以參考一下
    2022-05-05
  • Python 正則表達式進階用法之字符集與字符范圍詳解

    Python 正則表達式進階用法之字符集與字符范圍詳解

    本文詳細介紹了Python正則表達式中的字符集和字符范圍,包括字符集的基本概念、特殊字符、示例和注意事項,通過這些進階用法,我們可以更高效地處理復(fù)雜的文本模式,感興趣的朋友跟隨小編一起看看吧
    2024-11-11

最新評論

新干县| 越西县| 屯门区| 东方市| 尼木县| 新闻| 桃江县| 祁连县| 巴楚县| 城固县| 沾化县| 绥中县| 得荣县| 荔浦县| 南昌县| 宁武县| 云安县| 遵化市| 枣庄市| 崇明县| 元氏县| 苍山县| 姚安县| 新野县| 新疆| 鲜城| 崇阳县| 高阳县| 南昌市| 句容市| 噶尔县| 新竹县| 乐陵市| 厦门市| 安远县| 宁南县| 梓潼县| 洪湖市| 宿迁市| 凉山| 成安县|