import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
img = cv2.imread('Lena.png')
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,3,kernel) # ddepth can set to -1, then automatically match with source
plt.figure(figsize=(20,20))
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging with box filter')
plt.xticks([]), plt.yticks([])
plt.show()
nimg=img.copy()
nimg=cv2.cvtColor(nimg,cv2.COLOR_RGB2GRAY)
nimg[np.random.randn(*nimg.shape)>2]=0
plt.figure(figsize=(15,15))
plt.imshow(nimg,cmap='gray')
<matplotlib.image.AxesImage at 0x7f225d7bc7f0>
plt.figure(figsize=(15,15))
# plt.imshow(cv2.GaussianBlur(nimg,(5,5),2),cmap='gray')
plt.imshow(cv2.medianBlur(nimg,5),cmap='gray')
# plt.imshow(cv2.bilateralFilter(nimg,9,75,75),cmap='gray')
<matplotlib.image.AxesImage at 0x7f225d798400>
nimg=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
nimg=nimg+15*np.random.randn(*nimg.shape) # add Gaussian noise to image
nimg=np.uint8(nimg) # be sure to convert that back to uint8
plt.figure(figsize=(15,15))
plt.imshow(np.uint8(nimg),cmap='gray')
<matplotlib.image.AxesImage at 0x7f225d6f6518>
plt.figure(figsize=(15,15))
plt.imshow(cv2.GaussianBlur(nimg,(5,5),2),cmap='gray')
# plt.imshow(cv2.medianBlur(nimg,5),cmap='gray')
# plt.imshow(cv2.bilateralFilter(nimg,9,75,75),cmap='gray')
<matplotlib.image.AxesImage at 0x7f225d6d4710>
img=cv2.imread('wiki.jpg')
plt.figure(figsize=(15,15))
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f225d62f748>
img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([img],[0],None,[256],[0,256])
plt.plot(hist)
[<matplotlib.lines.Line2D at 0x7f225d61e5c0>]
# Plot histogram after histogram equalization
nimg=cv2.equalizeHist(img)
hist = cv2.calcHist([nimg],[0],None,[256],[0,256])
plt.plot(hist)
[<matplotlib.lines.Line2D at 0x7f225d57d240>]
plt.imshow(np.hstack((img, cv2.equalizeHist(img))),cmap='gray')
<matplotlib.image.AxesImage at 0x7f225d550c50>