2013년 7월 24일 수요일

[openCV, Python] Test Median Blur by using Salt&Pepper Noise

Median Filter : http://en.wikipedia.org/wiki/Median_filter (Wiki)

You can remove drastic noise efficiently.


import cv2
import numpy as np
import random

def MakeSaltAndPepperNoise (Image, SaltNum, PepperNum):
CopyImage = Image.copy()
nChannel = 0

# Get Image size
Width = CopyImage.shape[0]
Height = CopyImage.shape[1]

# If image is grayscale, it not have Image.shape[2]
# so it raise IndexError exception
try:
nChannel = CopyImage.shape[2]
except IndexError:
nChannel = 1

# Make Salt Noise
for Salt in range(0, SaltNum):
# Generate Random Position
RWidth = random.randrange(0, Width)
RHeight = random.randrange(0, Height)
# Make Noise
if nChannel > 1:
for c in range(0, nChannel):
CopyImage[RWidth, RHeight, c] = 255
else:
CopyImage[RWidth, RHeight] = 255

# Make Pepper Noise
for Pepper in range(0, PepperNum):
# Generate Random Position
RWidth = random.randrange(0, Width)
RHeight = random.randrange(0, Height)
# Make Noise
if nChannel > 1:
for c in range(0, nChannel):
CopyImage[RWidth, RHeight, c] = 0
else:
CopyImage[RWidth, RHeight] = 0

return CopyImage



In IDE



NoiseImage



Proccesed Image

댓글 없음:

댓글 쓰기