基于卷积操作的图像增强
文章目录
- 基于卷积操作的图像增强
-
- 1. 基于低通滤波的图像去噪
- 2. 基于卷积操作的边界增强
1. 基于低通滤波的图像去噪
from PIL import Image, ImageFilter
import matplotlib.pylab as plt
import numpy as np
i = 1
plt.figure(figsize=(10,35))
for prop_noise in np.linspace(0.05,0.3,6):
im = Image.open('images/mandrill.jpg')
n = int(im.width * im.height * prop_noise)
x, y = np.random.randint(0, im.width, n), np.random.randint(0, im.height, n)
for (x,y) in zip(x,y):
im.putpixel((x, y), ((0,0,0) if np.random.rand() < 0.5 else (255,255,255)))
plt.subplot(6,2,i)
plt.imshow(im)
plt.title('Original Image with ' + str(int(100*prop_noise)) + '% added noise', size=15)
i += 1
im1 = im.filter(ImageFilter.BLUR)
plt.subplot(6,2,i)
plt.imshow(im1)
plt.title('Blurred Image', size=20)
i += 1
im.show()
plt.figure(figsize=(20,7))
plt.subplot(1,3,1)
plt.imshow(im)
plt.title('Original Image', size=30)
plt.axis('off')
for n in [3,5]:
box_blur_kernel = np.reshape(np.ones(n*n),(n,n)) / (n*n)
im1 = im.filter(ImageFilter.Kernel((n,n), box_blur_kernel.flatten()))
plt.subplot(1,3,(2 if n==3 else 3))
plt.imshow(im1)
plt.title('Blurred with kernel size = ' + str(n) + 'x' + str(n), size=20)
plt.axis('off')
plt.suptitle('PIL Mean Filter (Box Blur) with different Kernel size', size=30)
plt.show()
2. 基于卷积操作的边界增强
from PIL import Image
from PIL import ImageFilter
from PIL.ImageFilter import (FIND_EDGES, EDGE_ENHANCE, EDGE_ENHANCE_MORE)
im = Image.open('images/building.jpg')
plt.figure(figsize=(18,25))
plt.subplot(221)
plt.imshow(im)
plt.title('original (UMBC library)', size=20)
plt.axis('off')
i = 2
for f in (FIND_EDGES, EDGE_ENHANCE, EDGE_ENHANCE_MORE):
plt.subplot(2,2,i)
im1 = im.filter(f)
plt.imshow(im1)
plt.title(str(f), size=20)
plt.axis('off')
i += 1
plt.show()