-
Notifications
You must be signed in to change notification settings - Fork 5
/
question14.py
96 lines (55 loc) · 2.14 KB
/
question14.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
93
94
95
96
__author__ = 'anish'
import cv2
import numpy as np
import imutils
img_org = cv2.imread('car1.jpg')
size = np.shape(img_org)
if size[0] <= 776:
img_org = imutils.resize(img_org , 900)
img_org2 = img_org.copy()
img_bw = cv2.cvtColor(img_org , cv2.COLOR_BGR2GRAY)
ret3,img_thr = cv2.threshold(img_bw,125,255,cv2.THRESH_BINARY)
cv2.imwrite('thresh.jpg',img_thr)
img_edg = cv2.Canny(img_thr ,100,200)
cv2.imwrite('cn_edge.jpg' , img_edg)
kernel = cv2.getStructuringElement(cv2.MORPH_DILATE, (7, 7))
img_dil = cv2.dilate(img_edg, kernel, iterations = 1)
cv2.imwrite('dilated_img.jpg',img_dil)
#if you are using opencv 2.X then make sure to remove "something_else " variable from list below
(somethig_else,contours ,hierarchye) = cv2.findContours(img_dil.copy(), 1, 2)
cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None
for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
# if our approximated contour has four points, then
# we can assume that we have found our screen
if len(approx) == 4:
screenCnt = approx
break
mask = np.zeros(img_bw.shape, dtype=np.uint8)
roi_corners = np.array(screenCnt ,dtype=np.int32)
ignore_mask_color = (255,)*1
cv2.fillPoly(mask, roi_corners , ignore_mask_color)
cv2.drawContours(img_org, [screenCnt], -40, (100, 255, 100), 9)
cv2.imshow('original image with boundry' , img_org)
cv2.imwrite('plate_detedted.jpg',img_org)
ys =[screenCnt[0,0,1] , screenCnt[1,0,1] ,screenCnt[2,0,1] ,screenCnt[3,0,1]]
xs =[screenCnt[0,0,0] , screenCnt[1,0,0] ,screenCnt[2,0,0] ,screenCnt[3,0,0]]
ys_sorted_index = np.argsort(ys)
xs_sorted_index = np.argsort(xs)
x1 = screenCnt[xs_sorted_index[0],0,0]
x2 = screenCnt[xs_sorted_index[3],0,0]
y1 = screenCnt[ys_sorted_index[0],0,1]
y2 = screenCnt[ys_sorted_index[3],0,1]
img_plate = img_org2[y1:y2 , x1:x2]
# for i in screenCnt:
# print(i)
#
# print xs , ys
#
# print x1,x2,y1,y2
cv2.imshow('number plate',img_plate)
cv2.imwrite('number_plate.jpg',img_plate)
cv2.waitKey(0)