Skip to content

Commit

Permalink
Masking added
Browse files Browse the repository at this point in the history
  • Loading branch information
gautam-dev-maker committed Sep 7, 2020
1 parent c5c0025 commit d3ddeaa
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 38 deletions.
38 changes: 26 additions & 12 deletions 3.Edge_Detection/horizontal_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,34 @@ def rgb2gray(rgb):
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray

def vertical_edge_detector(image):
output=np.zeros_like(image)
image_padded=np.zeros((image.shape[0]+2,image.shape[1]))
image_padded[1:-1,:]=image
image_padded[0,:]=image[0,:]
image_padded[-1,:]=image[-1,:]
for i in range(image.shape[0]):
for j in range(image.shape[1]):
resultant=image_padded[i+2,j]-image_padded[i,j]
resultant=round((resultant+255)/2)
output[i,j]=resultant
def convolve3d_grayscale(image, kernel):
output = np.zeros_like(image)
image_padded = np.zeros((image.shape[0]+kernel.shape[0]-1,image.shape[1] + kernel.shape[1]-1))
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]):
output[y,x]=(kernel * image_padded[y: y+kernel.shape[0], x: x+kernel.shape[1]]).sum()
return output

gaussian_blurr=np.array([[1, 4, 6, 4, 1],
[4, 16, 24, 16, 4],
[6, 24, 36, 24, 6],
[4, 16, 24, 16, 4],
[1, 4, 6, 4, 1]])/256

x_direction_kernel=np.array([[-1,0,1],
[-2,0,2],
[-1,0,1]])

y_direction_kernel=np.array([[-1,-2,-1],
[ 0, 0, 0],
[ 1, 2, 1]])

file_name="edge-detection1.png"
im = rgb2gray(np.array(Image.open(file_name)))
pil_img=Image.fromarray(vertical_edge_detector(im)).convert('RGB')
im=convolve3d_grayscale(im,gaussian_blurr)
im=convolve3d_grayscale(im,x_direction_kernel)
pil_img=Image.fromarray(im).convert('RGB')
pil_img.save('result_horizontal_edge.jpg')
Binary file modified 3.Edge_Detection/result_horizontal_edge.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified 3.Edge_Detection/result_verical_edge.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions 3.Edge_Detection/sobel.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def convolve3d_grayscale(image, kernel):
[ 0, 0, 0],
[ 1, 2, 1]])

file_name="result_dilation.png"
file_name="edge-detection1.png"
im = rgb2gray(np.array(Image.open(file_name)))
im=convolve3d_grayscale(im,gaussian_blurr)
im_x=convolve3d_grayscale(im,x_direction_kernel)
im_y=convolve3d_grayscale(im,y_direction_kernel)
im=np.hypot(im_x,im_y)
pil_img=Image.fromarray(im).convert('RGB')
pil_img.save('result_morphology.jpg')
pil_img.save('result_sobel.jpg')
38 changes: 26 additions & 12 deletions 3.Edge_Detection/vertical_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,35 @@ def rgb2gray(rgb):
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray

def vertical_edge_detector(image):
output=np.zeros_like(image)
image_padded=np.zeros((image.shape[0],image.shape[1]+2))
image_padded[:,1:-1]=image
image_padded[:,0]=image[:,0]
image_padded[:,-1]=image[:,-1]
for i in range(image.shape[0]):
for j in range(image.shape[1]):
resultant=image_padded[i,j+2]-image_padded[i,j]
resultant=round((resultant+255)/2)
output[i,j]=resultant
def convolve3d_grayscale(image, kernel):
output = np.zeros_like(image)
image_padded = np.zeros((image.shape[0]+kernel.shape[0]-1,image.shape[1] + kernel.shape[1]-1))
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]):
output[y,x]=(kernel * image_padded[y: y+kernel.shape[0], x: x+kernel.shape[1]]).sum()
return output

gaussian_blurr=np.array([[1, 4, 6, 4, 1],
[4, 16, 24, 16, 4],
[6, 24, 36, 24, 6],
[4, 16, 24, 16, 4],
[1, 4, 6, 4, 1]])/256

x_direction_kernel=np.array([[-1,0,1],
[-2,0,2],
[-1,0,1]])

y_direction_kernel=np.array([[-1,-2,-1],
[ 0, 0, 0],
[ 1, 2, 1]])

file_name="edge-detection1.png"
im = np.array(Image.open(file_name))
im=rgb2gray(im)
pil_img=Image.fromarray(vertical_edge_detector(im)).convert('RGB')
im=convolve3d_grayscale(im,gaussian_blurr)
im=convolve3d_grayscale(im,y_direction_kernel)
pil_img=Image.fromarray(im).convert('RGB')
pil_img.save('result_verical_edge.jpg')
3 changes: 0 additions & 3 deletions 4.Morphological_Transformation/morphological_dilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ def dilation(image, kernel):
return output





structuring_element=np.array([[0, 1, 0],
[1, 1, 1],
[0, 1, 0]])
Expand Down
Binary file modified 5.Masking/masked.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 20 additions & 9 deletions 5.Masking/masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@
import numpy as np
from PIL import Image

def RGB2BGR(image):
image1=np.zeros_like(image)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
image1[i][j][0]=image[i][j][2]
image1[i][j][1]=image[i][j][1]
image1[i][j][2]=image[i][j][0]
return image1

def masking_image(image,lower,upper):
image = cv2.cvtColor(image.astype(np.uint8), cv2.COLOR_BGR2HSV)
image_copy=image
for x in range(image.shape[0]):
for y in range(image.shape[1]):
for z in range(image.shape[2]):
if image[x,y,z]>=lower[z] and image[x,y,z]<=upper[z]:
image[x,y,z]=360
else:
image[x,y,z]=0
image=~(image_copy ^ image)
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask=np.zeros((image.shape[0],image.shape[1],image.shape[2]),dtype=np.uint8)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
if image[i][j][0] >= lower[0] and image[i][j][1] >= lower[1] and image[i][j][2] >= lower[2] and image[i][j][0] <= upper[0] and image[i][j][1] <= upper[1] and image[i][j][2] <= upper[2]:
mask[i][j][0] = image[i][j][0]
mask[i][j][1] = image[i][j][1]
mask[i][j][2] = image[i][j][2]
image=np.bitwise_and(mask,image)
image= cv2.cvtColor(image, cv2.COLOR_HSV2RGB)

return image


Expand All @@ -39,6 +49,7 @@ def convolve3d(image, kernel):
im = np.array(Image.open(file_name))
lower_blue = np.array([94,130,38])
upper_blue = np.array([179,255,255])
im=RGB2BGR(im)
im=masking_image(convolve3d(im,gaussian_blurr),lower_blue,upper_blue)
pil_img=Image.fromarray(im.astype(np.uint8))
pil_img.save('masked.jpeg')

0 comments on commit d3ddeaa

Please sign in to comment.