0% found this document useful (0 votes)
12 views1 page

Blurring Smoothing Morphology

The document contains Python code for image processing using OpenCV, including functions to load and display images, apply gamma correction, blurring techniques, and morphological operations. It demonstrates various image manipulations such as Gaussian blur, median blur, and the use of erosion and dilation with kernels. The code also includes handling of noise in images and visualizing the results at each step.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Blurring Smoothing Morphology

The document contains Python code for image processing using OpenCV, including functions to load and display images, apply gamma correction, blurring techniques, and morphological operations. It demonstrates various image manipulations such as Gaussian blur, median blur, and the use of erosion and dilation with kernels. The code also includes handling of noise in images and visualizing the results at each step.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

In [1]: import cv2

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [2]: def load_img():


img = cv2.imread('bricks.jpg').astype(np.float32)/255
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
return img

In [ ]: #load_img()

In [5]: def display_img(img):


fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(111)
ax.imshow(img)

In [6]: i = load_img()
display_img(i)

Aplicamos la Corrección Gamma


In [16]: gamma = 1/4 # 1/4, 1/10, 2, 8

In [17]: results = np.power(i,gamma)


display_img(results)

Emborronado (Blurring) y suavizado


In [18]: img = load_img()
font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img, text='Ladrillos',org=(10,600), fontFace=font, fontScale=10, color=(255,0,0), thickness=4 )
display_img(img)

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

In [19]: kernel = np.ones(shape=(5,5),dtype=np.float32)/25

In [20]: kernel

Out[20]: array([[0.04, 0.04, 0.04, 0.04, 0.04],


[0.04, 0.04, 0.04, 0.04, 0.04],
[0.04, 0.04, 0.04, 0.04, 0.04],
[0.04, 0.04, 0.04, 0.04, 0.04],
[0.04, 0.04, 0.04, 0.04, 0.04]], dtype=float32)

In [21]: dst = cv2.filter2D(img,-1, kernel)


display_img(dst)

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

In [24]: img = load_img()


font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img, text='Ladrillos',org=(10,600), fontFace=font, fontScale=10, color=(255,0,0), thickness=4 )
print('reseteo')

reseteo

In [27]: blurred = cv2.blur(img,ksize=(10,10)) # 5,5 - 10,10


display_img(blurred)

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

In [28]: img = load_img()


font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img, text='Ladrillos',org=(10,600), fontFace=font, fontScale=10, color=(255,0,0), thickness=4 )
print('reseteo')

reseteo

In [29]: blurred_image = cv2.GaussianBlur(img,(5,5), 10)


display_img(blurred_image)

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

In [30]: img = load_img()


font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img, text='Ladrillos',org=(10,600), fontFace=font, fontScale=10, color=(255,0,0), thickness=4 )
print('reseteo')

reseteo

In [32]: median_results = cv2.medianBlur(img,5)


display_img(median_results)

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

In [33]: img = cv2.imread('sammy.jpg')


img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
display_img(img)

In [35]: img = cv2.imread('sammy_noise.jpg')


noise_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
display_img(noise_img)

In [36]: median = cv2.medianBlur(noise_img,5)


display_img(median)

In [37]: img = load_img()


font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img, text='Ladrillos',org=(10,600), fontFace=font, fontScale=10, color=(255,0,0), thickness=4 )
print('reseteo')

reseteo

In [38]: blur = cv2.bilateralFilter(img,9,75,75)


display_img(blur)

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

Operadores Morfológicos
In [54]: def load_img2():
blank_img = np.zeros((600,600))
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(blank_img, text='ABCDE',org=(50,300), fontFace=font, fontScale=5, color=(255,255,255), thickness=25)
return blank_img

In [55]: def display_img2(img):


fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(111)
ax.imshow(img,cmap='gray')

In [73]: img = load_img2()


display_img2(img)

In [60]: kernel = np.ones((5,5),dtype=np.uint8)


kernel

Out[60]: array([[1, 1, 1, 1, 1],


[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]], dtype=uint8)

In [63]: results = cv2.erode(img,kernel,iterations=4) #1, 5


display_img2(results)

In [89]: img = load_img2()

In [92]: white_noise = np.random.randint(low=0,high=2, size=(600,600))


white_noise

Out[92]: array([[0, 0, 0, ..., 1, 1, 1],


[1, 0, 0, ..., 1, 0, 0],
[1, 0, 0, ..., 1, 0, 1],
...,
[0, 0, 1, ..., 0, 1, 0],
[0, 1, 1, ..., 1, 1, 0],
[0, 1, 0, ..., 1, 1, 0]])

In [68]: #display_img2(white_noise)

In [91]: img.max()

Out[91]: 255.0

In [93]: white_noise = white_noise*255


display_img2(white_noise)

In [94]: noise_img = white_noise + img


display_img2(noise_img)

In [97]: openning = cv2.morphologyEx(noise_img, cv2.MORPH_OPEN, kernel)


display_img2(openning)

In [98]: img = load_img2()

In [99]: black_noise = np.random.randint(low=0,high=2,size=(600,600))

In [101… black_noise = black_noise*-255

In [102… black_noise

Out[102… array([[ 0, -255, -255, ..., -255, 0, -255],


[ 0, -255, -255, ..., -255, -255, 0],
[ 0, 0, 0, ..., -255, 0, 0],
...,
[ 0, -255, 0, ..., 0, 0, 0],
[ 0, 0, -255, ..., 0, -255, 0],
[-255, -255, -255, ..., -255, -255, 0]])

In [104… black_noise = img + black_noise


black_noise

Out[104… array([[ 0., -255., -255., ..., -255., 0., -255.],


[ 0., -255., -255., ..., -255., -255., 0.],
[ 0., 0., 0., ..., -255., 0., 0.],
...,
[ 0., -255., 0., ..., 0., 0., 0.],
[ 0., 0., -255., ..., 0., -255., 0.],
[-255., -255., -255., ..., -255., -255., 0.]])

In [105… black_noise[black_noise == -255] = 0

In [106… black_noise.min()

Out[106… 0.0

In [107… display_img2(black_noise)

In [108… closing = cv2.morphologyEx(noise_img, cv2.MORPH_CLOSE, kernel)

In [109… display_img2(closing)

In [111… img = load_img2()


display_img2(img)

In [112… gradient = cv2.morphologyEx(noise_img, cv2.MORPH_GRADIENT, kernel)


display_img2(gradient)

In [ ]:

You might also like