Lab5.ipynb - Colaboratory
Lab5.ipynb - Colaboratory
ipynb - Colaboratory
#VENKATESH GAURI SHANKAR
##Exp 5: To execute data normalization and segmentation methods and their different variants.
# importing packages
import pandas as pd
# create·data
df = pd.DataFrame([
[180000, 110, 18.9, 1400],
[360000, 905, 23.4, 1800],
[230000, 230, 14.0, 1300],
[60000, 450, 13.5, 1500]],
columns=['Col A', 'Col B',
'Col C', 'Col D'])
# view data
display(df)
import matplotlib.pyplot as plt
df.plot(kind = 'bar')
<matplotlib.axes._subplots.AxesSubplot at 0x7f3157513cd0>
#Using The maximum absolute scaling- maximum absolute value/decimal scaling
# copy the data
https://colab.research.google.com/drive/17fGm-ez1cEMd3Dyk29obkuLRjbodJUvX#scrollTo=qfopj46wRN2A&printMode=true 1/8
01/12/2021, 01:57 Lab6.ipynb - Colaboratory
df_max_scaled = df.copy()
# apply normalization techniques
for column in df_max_scaled.columns:
df_max_scaled[column] = df_max_scaled[column] / df_max_scaled[column].abs().max()
# view normalized data
display(df_max_scaled)
import matplotlib.pyplot as plt
df_max_scaled.plot(kind = 'bar')
<matplotlib.axes._subplots.AxesSubplot at 0x7f3151425690>
##Using The min-max feature scaling-subtracting the minimum value of the feature then dividing by th
# copy the data
df_min_max_scaled = df.copy()
# apply normalization techniques
for column in df_min_max_scaled.columns:
df_min_max_scaled[column] = (df_min_max_scaled[column] - df_min_max_scaled[column].min()) / (df_mi
# view normalized data
print(df_min_max_scaled)
import matplotlib.pyplot as plt
df min max scaled.plot(kind = 'bar')
https://colab.research.google.com/drive/17fGm-ez1cEMd3Dyk29obkuLRjbodJUvX#scrollTo=qfopj46wRN2A&printMode=true 2/8
01/12/2021, 01:57 Lab6.ipynb - Colaboratory
_ _ _ p ( )
<matplotlib.axes._subplots.AxesSubplot at 0x7f315091b690>
##Using The z-score method-distribution with a mean of 0 and a typical deviation of 1
# copy the data
df_z_scaled = df.copy()
# apply normalization techniques
for column in df_z_scaled.columns:
df_z_scaled[column] = (df_z_scaled[column] -
df_z_scaled[column].mean()) / df_z_scaled[column].std()
# view normalized data
display(df_z_scaled)
import matplotlib.pyplot as plt
df_z_scaled.plot(kind='bar')
<matplotlib.axes._subplots.AxesSubplot at 0x7f315b1de3d0>
https://colab.research.google.com/drive/17fGm-ez1cEMd3Dyk29obkuLRjbodJUvX#scrollTo=qfopj46wRN2A&printMode=true 3/8
01/12/2021, 01:57 Lab6.ipynb - Colaboratory
# Segmentation
##Laplacian Operator for segmentation
import matplotlib.pyplot as plt
%matplotlib inline
Laplacian=plt.imread("download.png")
plt.imshow(Laplacian)
<matplotlib.image.AxesImage at 0x7f314ecafd50>
import cv2
import matplotlib.pyplot as plt
# Open the image
img = cv2.imread('download.png')
# Apply gray scale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply gaussian blur
blur_img = cv2.GaussianBlur(gray_img, (3, 3), 0)
# Positive Laplacian Operator
laplacian = cv2.Laplacian(blur_img, cv2.CV_64F)
plt.figure()
plt.title('Laplacian')
plt.imsave('Laplacian.png', laplacian, cmap='gray', format='png')
plt.imshow(laplacian, cmap='gray')
plt.show()
##Canny Operator for segmentation
import cv2
import matplotlib pyplot as plt
https://colab.research.google.com/drive/17fGm-ez1cEMd3Dyk29obkuLRjbodJUvX#scrollTo=qfopj46wRN2A&printMode=true 4/8
01/12/2021, 01:57 Lab6.ipynb - Colaboratory
import matplotlib.pyplot as plt
# Open the image
img = cv2.imread('download.png')
# Apply Canny
edges = cv2.Canny(img, 100, 200, 3, L2gradient=True)
plt.figure()
plt.title('canny')
plt.imsave('canny.png', edges, cmap='gray', format='png')
plt.imshow(edges, cmap='gray')
plt.show()
#Gray scale brain image
##Laplacian Operator for segmentation- B&W
import matplotlib.pyplot as plt
%matplotlib inline
Laplacian=plt.imread("brain.png")
plt.imshow(Laplacian)
<matplotlib.image.AxesImage at 0x7f314506aed0>
import cv2
import matplotlib.pyplot as plt
# Open the image
img = cv2.imread('brain.png')
# Apply gray scale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
https://colab.research.google.com/drive/17fGm-ez1cEMd3Dyk29obkuLRjbodJUvX#scrollTo=qfopj46wRN2A&printMode=true 5/8
01/12/2021, 01:57 Lab6.ipynb - Colaboratory
# Apply gaussian blur
blur_img = cv2.GaussianBlur(gray_img, (3, 3), 0)
# Positive Laplacian Operator
laplacian = cv2.Laplacian(blur_img, cv2.CV_64F)
plt.figure()
plt.title('Laplacian')
plt.imsave('Laplacian.png', laplacian, cmap='gray', format='png')
plt.imshow(laplacian, cmap='gray')
plt.show()
##Canny Operator for segmentation-B&W
import cv2
import matplotlib.pyplot as plt
# Open the image
img = cv2.imread('brain.png')
# Apply Canny
edges = cv2.Canny(img, 100, 200, 3, L2gradient=True)
plt.figure()
plt.title('canny')
plt.imsave('canny.png', edges, cmap='gray', format='png')
plt.imshow(edges, cmap='gray')
plt.show()
import matplotlib.pyplot as plt
%matplotlib inline
img=plt.imread("mr_bean.jpeg")
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f31443a65d0>
import random
import cv2
def add_noise(img):
# Getting the dimensions of the image
row , col = img.shape
# Randomly pick some pixels in the
# image for coloring them white
# Pick a random number between 300 and 10000
number_of_pixels = random.randint(300, 10000)
for i in range(number_of_pixels):
# Pick a random y coordinate
y_coord=random.randint(0, row - 1)
# Pick a random x coordinate
x_coord=random.randint(0, col - 1)
# Color that pixel to white
img[y_coord][x_coord] = 255
# Randomly pick some pixels in
# the image for coloring them black
# Pick a random number between 300 and 10000
number_of_pixels = random.randint(300 , 10000)
for i in range(number_of_pixels):
# Pick a random y coordinate
y_coord=random.randint(0, row - 1)
# Pick a random x coordinate
x_coord=random.randint(0, col - 1)
# Color that pixel to black
img[y_coord][x_coord] = 0
t i
https://colab.research.google.com/drive/17fGm-ez1cEMd3Dyk29obkuLRjbodJUvX#scrollTo=qfopj46wRN2A&printMode=true 7/8
01/12/2021, 01:57 Lab6.ipynb - Colaboratory
return img
# salt-and-pepper noise can
# be applied only to grayscale images
# Reading the color image in grayscale image
img = cv2.imread('mr_bean.jpeg',
cv2.IMREAD_GRAYSCALE)
#Storing the image
cv2.imwrite('salt-and-pepper-bean.jpeg',
add_noise(img))
True
plt.imshow(img)
plt.show()
https://colab.research.google.com/drive/17fGm-ez1cEMd3Dyk29obkuLRjbodJUvX#scrollTo=qfopj46wRN2A&printMode=true 8/8