LoginSignup
11
6

More than 5 years have passed since last update.

画像にガウシアンノイズを加える with python2.7

Last updated at Posted at 2016-05-19

#【環境】
windows8.1
Anaconda(python2.7)
あらかじめopencv3をインストールしておく必要があります。

#【概要】
指定したフォルダ内にある複数の画像データにランダムなガウシアンノイズを加えた画像データを作成して別の指定したフォルダに保存します。

フォルダ構成
|---gaussian
    |---before_images(ノイズを加える前の画像フォルダ)
    |---before_images(ノイズを加えた後の画像フォルダ)
    |---gaussian.py
gaussian.py
# -*- coding: utf-8 -*-

import cv2
import numpy as np
import sys
import os
import glob

# ガウシアンノイズ
def addGaussianNoise(src):
    row,col,ch= src.shape
    mean = 0
    var = 0.1
    sigma = 15
    gauss = np.random.normal(mean,sigma,(row,col,ch))
    gauss = gauss.reshape(row,col,ch)
    noisy = src + gauss
    
    return noisy

# プログラムが存在するディレクトリの代入
current_dir = os.getcwd()
# 画像が存在するディレクトリの代入
before_images = glob.glob(current_dir + "\\before_images\\*") 

i = 0

for image in before_images:
    if image == current_dir + "\\before_images\\Thumbs.db":
        continue
    else:
        # 画像の読み込み
        img = cv2.imread(image)
        
        # ノイズ付加
        after_image = addGaussianNoise(img)
        
        # 画像保存   
        cv2.imwrite(current_dir + '\\after_images\\' + str(i) + '.jpg', after_image) 
        i += 1

#【参考URL】
https://github.com/bohemian916/deeplearning_tool/blob/master/increase_picture.py
opencv3のインストール

11
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
6