-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanny Edge Detection.py
92 lines (72 loc) · 2.89 KB
/
Canny Edge Detection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from pkgutil import walk_packages
import cv2
import matplotlib.pyplot as plt
import json
import numpy as np
from skimage import data, io, filters
backSub_KNN = cv2.createBackgroundSubtractorKNN(detectShadows=False)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
backSub_MOG2 = cv2.createBackgroundSubtractorMOG2(detectShadows=False)
set_ratio = 0.055
img = cv2.imread('C:/Users/user/Desktop/test_images/non_blur1.jpg')
# img = cv2.imread('C:/Users/user/Desktop/test_images/blurred_4.png')
# img = cv2.imread('C:/Users/user/Desktop/test_images/blurred_2.jpg')
# img = cv2.imread('C:/Users/user/Desktop/test_images/hi.jpg')
# img = cv2.imread('C:/Users/user/Desktop/test_images/blurred_3.jpg')
# img = cv2.imread('C:/Users/user/Desktop/test_images/blurred_5.jpg')
# Resize image to 360 x 480
cv2.imshow("Original image : ",img)
f = open('C:/Users/user/Desktop/test_images/non_blur1.json')
# f = open('C:/Users/user/Desktop/test_images/blurred_4.json')
# f = open('C:/Users/user/Desktop/test_images/blurred_1.json')
# f = open('C:/Users/user/Desktop/test_images/blurred_2.json')
# f = open('C:/Users/user/Desktop/test_images/hi.json')
# f = open('C:/Users/user/Desktop/test_images/blurred_3.json')
# f = open('C:/Users/user/Desktop/test_images/blurred_5.json')
data = json.load(f)
f.close()
input_points = data['shapes'][0]['points']
input_points = np.float32(input_points)
output_points = np.float32([
[0,0],
[224,0],
[224,224],
[0,224]]
)
M = cv2.getPerspectiveTransform(input_points,output_points)
# print(M)
warped_image = cv2.warpPerspective(img,M,(224,224),flags=cv2.INTER_LINEAR)
cv2.imshow('Warped Image', warped_image)
cv2.imwrite('warped_nonblurr.jpg',warped_image)
crop_img_blur = cv2.GaussianBlur(warped_image, (3,3),0)
cv2.imshow("blur", crop_img_blur)
crop_img_gray = cv2.cvtColor(crop_img_blur, cv2.COLOR_BGR2GRAY)
cv2.imshow("grey", crop_img_gray)
# Automatic Thresholding
# t = filters.threshold_otsu(crop_img_gray)
t, res = cv2.threshold(crop_img_gray, 0, 255, cv2.THRESH_OTSU)
mask = crop_img_gray > t
mask = np.logical_not(mask)
# Generating Mask
mask = np.array(mask,dtype=np.float64) + 0
cv2.imshow("Mask", mask)
# Dtect the edges
edges = cv2.Canny(image=crop_img_gray, threshold1 = 85, threshold2= 255,apertureSize= 3,L2gradient=1)
# print(edges.shape)
cv2.imshow('Edge Image ', edges)
final = edges * mask
# final = cv2.bitwise_and(edges,mask,mask = t)
cv2.imshow("Final ", final)
# Gt width and height of image
height, width = final.shape
#Ge number of egde/white pixelS
number_of_white_pix = np.sum(final!=0)
print(number_of_white_pix)
ratio = round(number_of_white_pix / (height * width),3)
print(ratio)
if ratio <= set_ratio:
print("Blurred")
else:
print("Not Blurred")
cv2.waitKey(0)
cv2.destroyAllWindows()