Skip to content

Commit

Permalink
Kernel operation added
Browse files Browse the repository at this point in the history
  • Loading branch information
gautam-dev-maker committed Sep 4, 2020
1 parent 159e978 commit e8bc463
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
8 changes: 4 additions & 4 deletions 1.Image_Rotation/image_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def rot(image,angle):
cosine=math.cos(angle)
sine=math.sin(angle)

image1=np.zeros_like(image)
#image1=np.zeros_like(image)

# Define the height and width of the new image that is to be formed
# new_height = floating_point(abs(image.shape[0]*cosine)+abs(image.shape[1]*sine))
# new_width = floating_point(abs(image.shape[1]*cosine)+abs(image.shape[0]*sine))
new_height = floating_point(abs(image.shape[0]*cosine)+abs(image.shape[1]*sine))
new_width = floating_point(abs(image.shape[1]*cosine)+abs(image.shape[0]*sine))

# image1=np.zeros((new_width,new_height,image.shape[2]))
image1=np.zeros((new_width,new_height,image.shape[2]))

# Find the centre of the image about which we have to rotate the image
centre_row = round(((image.shape[0]+1)/2)-1)
Expand Down
12 changes: 2 additions & 10 deletions 2.Applying_Kernels/blurring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

def convolve3d(image, kernel):
threshold=200
#kernel = np.flipud(np.fliplr(kernel))
output = np.zeros_like(image)
image_padded = np.zeros((image.shape[0]+4,image.shape[1] + 4,image.shape[2]))
image_padded[3:-1:,3:-1:,:] = image
Expand All @@ -19,21 +18,14 @@ def convolve3d(image, kernel):
[4, 16, 24, 16, 4],
[6, 24, 36, 24, 6],
[4, 16, 24, 16, 4],
[1, 4, 6, 4, 1]])
[1, 4, 6, 4, 1]])/256

box_blur=np.array([[1,1,1],
[1,1,1],
[1,1,1]])
# Sharpen=np.array([[0 ,-1, 0],
# [-1, 5,-1],
# [0 ,-1, 0]])
# Edge=np.array([[-1 ,-1, -1],
# [-1, 8,-1],
# [-1 ,-1, -1]])
#file_name=input("Enter the name of the image to blurr:- ")
file_name="test_blur.jpg"
im = np.array(Image.open(file_name))
pil_img=Image.fromarray(convolve3d(im, gaussian_blurr/256))
pil_img=Image.fromarray(convolve3d(im, gaussian_blurr))
pil_img.save('result_blur.jpg')


Binary file modified 2.Applying_Kernels/result_sharpen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 12 additions & 17 deletions 2.Applying_Kernels/sharpen.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
from PIL import Image
import numpy as np
def convolve3d(image, kernel):
threshold=100
kernel = np.flipud(np.fliplr(kernel))
output = np.zeros_like(image)
image_padded = np.zeros((image.shape[0]+2,image.shape[1] + 2,image.shape[2]))
image_padded[1:-1:,1:-1:,:] = image
image_padded = np.zeros((image.shape[0]+kernel.shape[0]-1,image.shape[1] + kernel.shape[1]-1,image.shape[2]))
image_padded[kernel.shape[0]-2:-1:,kernel.shape[1]-2:-1:,:] = image
image_padded[0,0,:]=image[0,0,:]
image_padded[-1,-1,:]=image[-1,-1,:]
for x in range(image.shape[1]):
for y in range(image.shape[0]):
for z in range(image.shape[2]):
summation=(kernel * image_padded[y: y+3, x: x+3,z]).sum()
if summation<threshold :
output[y, x,z]=50
else:
output[y, x,z]=summation
summation=(kernel * image_padded[y: y+kernel.shape[0], x: x+kernel.shape[1],z]).sum()
output[y, x,z]=summation
output[:,:,3]=image[:,:,3]
return output

Sharpen1=np.array([[-1, -1, -1, -1, -1],
[-1, 2, 2, 2, -1],
[-1, 2, 8, 2, -1],
[-1, 2, 2, 2, -1],
[-1, -1, -1, -1, -1,]])

Sharpen=np.array([[-1,-1, -1],
[-1, 9,-1],
[-1 ,-1, -1]])
# Edge=np.array([ [0 ,-1, 0],
# [-1, 4,-1],
# [0 ,-1, 0]])

Sharpen=np.array([[-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1],[-1,-1,25,-1,-1],[-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1]])/-25
file_name="test_sharpen.png"
im = np.array(Image.open(file_name))
pil_img=Image.fromarray(convolve3d(im,Sharpen*0.9))
im=convolve3d(im,Sharpen)
pil_img=Image.fromarray(im.astype(np.uint8))
pil_img.save('result_sharpen.png')


Expand Down

0 comments on commit e8bc463

Please sign in to comment.