반응형
OpenCV를 사용하는 Python 또는 이미지에 Gaussian 또는 salt and pepper 노이즈를 추가하는 다른 Python 이미지 처리 라이브러리가있는 일부 기능이 있는지 궁금합니다. 예를 들어, MATLAB에는 동일한 작업을 수행하는 간단한 함수가 있습니다.
또는 OpenCV와 함께 Python을 사용하여 이미지에 노이즈를 추가하는 방법은 무엇입니까?
해결 방법
이 함수는 이미지에 가우스, 소금 후추, 푸 아송 및 얼룩 노이즈를 추가합니다.
Parameters
----------
image : ndarray
Input image data. Will be converted to float.
mode : str
One of the following strings, selecting the type of noise to add:
'gauss' Gaussian-distributed additive noise.
'poisson' Poisson-distributed noise generated from the data.
's&p' Replaces random pixels with 0 or 1.
'speckle' Multiplicative noise using out = image + n*image,where
n is uniform noise with specified mean & variance.
import numpy as np
import os
import cv2
def noisy(noise_typ,image):
if noise_typ == "gauss":
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 noisy
elif noise_typ == "s&p":
row,col,ch = image.shape
s_vs_p = 0.5
amount = 0.004
out = np.copy(image)
# Salt mode
num_salt = np.ceil(amount * image.size * s_vs_p)
coords = [np.random.randint(0, i - 1, int(num_salt))
for i in image.shape]
out[coords] = 1
# Pepper mode
num_pepper = np.ceil(amount* image.size * (1. - s_vs_p))
coords = [np.random.randint(0, i - 1, int(num_pepper))
for i in image.shape]
out[coords] = 0
return out
elif noise_typ == "poisson":
vals = len(np.unique(image))
vals = 2 ** np.ceil(np.log2(vals))
noisy = np.random.poisson(image * vals) / float(vals)
return noisy
elif noise_typ =="speckle":
row,col,ch = image.shape
gauss = np.random.randn(row,col,ch)
gauss = gauss.reshape(row,col,ch)
noisy = image + image * gauss
return noisy
참조 페이지 https://stackoverflow.com/questions/22937589
반응형
'파이썬' 카테고리의 다른 글
파이썬 scikit-learn에서 CountVectorizer를 사용하여 토큰을 추출하는 데 사용되지 않은 문서의 빈도를 계산할 수 있습니까? (0) | 2020.12.16 |
---|---|
파이썬 시간과 분 단위로 두 열 간의 Pandas DataFrame 시간 차이 계산 (0) | 2020.12.15 |
파이썬 Creating a zero-filled pandas data frame (0) | 2020.12.15 |
파이썬 How to read a file in reverse order? (0) | 2020.12.15 |
파이썬 Parsing HTTP Response in Python (0) | 2020.12.15 |
댓글