[PYTHON] Blur median Gaussian treatment

Execution environment

Google Colaboratory

Preparing to load images with Google Colaboratory

python


from google.colab import files
from google.colab import drive
drive.mount('/content/drive')

Loading the required libraries

python


import cv2 #opencv
import matplotlib.pyplot as plt 
%matplotlib inline

Image preparation

python


img = plt.imread("/content/drive/My Drive/Colab Notebooks/img/Lenna.bmp")
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

processing

python


plt.figure(figsize=(9, 9), dpi=100,
           facecolor='w', linewidth=0, edgecolor='w')

plt.gray()

#Original image
plt.subplot(2,3,1)
plt.axis('off') 
plt.title("Original", fontsize=10)
plt.imshow(gray)

#Shrink(The filter is easy to see)
plt.subplot(2,3,2)
plt.axis('off') 
gray = cv2.resize(gray,(5,5))
plt.title("shukusyo", fontsize=10)
plt.imshow(gray)

size = (3,3)
size2 = 3 

#blur(Average value)
plt.subplot(2,3,4)
plt.axis('off') 
plt.title("blur", fontsize=10)
dst = cv2.blur(gray,size)
plt.imshow(dst)

#medianBlur(Median)
plt.subplot(2,3,5)
plt.axis('off') 
plt.title("medianBlur", fontsize=10)
dst = cv2.medianBlur(gray,size2)#size2 must be odd to take the median
plt.imshow(dst)

#GAUSSIAN Calculation by changing the weight according to the distance to the pixel to be processed
plt.subplot(2,3,6)
plt.axis('off') 
plt.title("GAUSSIAN", fontsize=10)
sigmaX = 11
sigmaY = 11
dst = cv2.GaussianBlur(gray,size,sigmaX,sigmaY)
plt.imshow(dst)

plt.show()

result

image.png

To make the changes easier to understand After compressing the image to 5x5, various processes were performed.

The calculation image looks like this. (Example of cv2.blur (gray, (3,3))) The average value of the 3x3 area is entered as the processed value.

image.png

This time, since anchor is not specified, it is processed based on the center. It seems that the processing position can be changed by specifying it.

In addition, the following link was easy to understand for the details of the calculation method that goes beyond the outer frame. This time it is not specified border = Type = cv2.BORDER_DEFAULT (mirrored at the center of the boundary pixel) Is running on. http://www5d.biglobe.ne.jp/~noocyte/Programming/OpenCV.html#cv::borderInterpolate

Recommended Posts

Blur median Gaussian treatment