-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber_Plate_Detection_2.py
More file actions
131 lines (97 loc) · 3.25 KB
/
Number_Plate_Detection_2.py
File metadata and controls
131 lines (97 loc) · 3.25 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 18 10:04:40 2019
@author: Raghav
"""
import numpy as np
import cv2
cropped_image = cv2.imread('./output/ROI.jpg')
gray = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
bilateral_filtered = cv2.bilateralFilter(gray, 11, 50, 50)
cv2.imshow('Bilateral Filtered', bilateral_filtered)
ret, plate_inverse_threshold = cv2.threshold(bilateral_filtered, 115, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('Threshold', plate_inverse_threshold)
# Vertical and Horizontal Scanning to reduce the region of interest
middle_x = int(plate_inverse_threshold.shape[0]/2)
middle_y = int(plate_inverse_threshold.shape[1]/2)
def getTopCoordinate():
for x in range(middle_x,-1,-1):
black_count = 0
white_count = 0
for y in range(0, plate_inverse_threshold.shape[1]):
if(plate_inverse_threshold[x][y] == 255):
white_count += 1
else:
black_count += 1
ratio = 400
if(black_count != 0):
ratio = white_count/black_count
#print((white_count,black_count, ratio))
if(ratio > 10 or ratio < 0.3):
return x
return 0
def getBottomCoordinate():
for x in range(middle_x,250,1):
black_count = 0
white_count = 0
for y in range(0, plate_inverse_threshold.shape[1]):
if(plate_inverse_threshold[x][y] == 255):
white_count += 1
else:
black_count += 1
ratio = 400
if(black_count != 0):
ratio = white_count/black_count
print(ratio)
if(ratio > 10 or ratio < 0.3):
return x
return 150
def getLeftCoordinate():
for y in range(middle_y,-1,-1):
black_count = 0
white_count = 0
for x in range(0, plate_inverse_threshold.shape[0]):
if(plate_inverse_threshold[x][y] == 255):
white_count += 1
else:
black_count += 1
ratio = 150
if(black_count != 0):
ratio = white_count/black_count
if(ratio > 30):
return y
return 0
def getRightCoordinate():
for y in range(middle_y,450,1):
black_count = 0
white_count = 0
for x in range(0, plate_inverse_threshold.shape[0]):
if(plate_inverse_threshold[x][y] == 255):
white_count += 1
else:
black_count += 1
ratio = 150
if(black_count != 0):
ratio = white_count/black_count
if(ratio > 40):
return y
return plate_inverse_threshold.shape[1]
top = getTopCoordinate()
bottom = getBottomCoordinate()
left = getLeftCoordinate()
right = getRightCoordinate()
#img1 = cropped_image[top:bottom, left:right]
#cv2.imshow('cropped', img1)
for l in range(1,7):
if(top-l >= 0):
top = top-l
if(bottom+l < cropped_image.shape[0]):
bottom = bottom+l
if(left-l >= 0):
left = left-l
if(right+l < cropped_image.shape[1]):
right = right+l
img = cropped_image[top:bottom, left:right]
cv2.imshow('crop', img)
cv2.imwrite('./output/plate_ROI.jpg', img)