├── README.md ├── car1.jpg ├── car10.jpg ├── car11.JPG ├── car12.jpg ├── car13.jpg ├── car2.jpg ├── car3.JPG ├── car4.jpg ├── car5.jpg ├── car6.JPG ├── car7.jpg ├── car8.jpg ├── car9.jpg ├── cn_edge.jpg ├── dilated_img.jpg ├── imutils.py ├── number_plate.jpg ├── plate_detedted.jpg ├── question14.py └── thresh.jpg /README.md: -------------------------------------------------------------------------------- 1 | # opencv-car-number-plate-detection- 2 | this project is made using python and opencv . i haven't used anything machine learning or rendering stuff to detect number plate . It is based on contour in opencv . it's dosent give you numbers directly it's just highlights (gives you the coordinate ) the rectangular area of number plate in image 3 | -------------------------------------------------------------------------------- /car1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car1.jpg -------------------------------------------------------------------------------- /car10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car10.jpg -------------------------------------------------------------------------------- /car11.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car11.JPG -------------------------------------------------------------------------------- /car12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car12.jpg -------------------------------------------------------------------------------- /car13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car13.jpg -------------------------------------------------------------------------------- /car2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car2.jpg -------------------------------------------------------------------------------- /car3.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car3.JPG -------------------------------------------------------------------------------- /car4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car4.jpg -------------------------------------------------------------------------------- /car5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car5.jpg -------------------------------------------------------------------------------- /car6.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car6.JPG -------------------------------------------------------------------------------- /car7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car7.jpg -------------------------------------------------------------------------------- /car8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car8.jpg -------------------------------------------------------------------------------- /car9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/car9.jpg -------------------------------------------------------------------------------- /cn_edge.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/cn_edge.jpg -------------------------------------------------------------------------------- /dilated_img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/dilated_img.jpg -------------------------------------------------------------------------------- /imutils.py: -------------------------------------------------------------------------------- 1 | # Import the necessary packages 2 | import numpy as np 3 | import cv2 4 | 5 | def translate(image, x, y): 6 | # Define the translation matrix and perform the translation 7 | M = np.float32([[1, 0, x], [0, 1, y]]) 8 | shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) 9 | 10 | # Return the translated image 11 | return shifted 12 | 13 | def rotate(image, angle, center = None, scale = 1.0): 14 | # Grab the dimensions of the image 15 | (h, w) = image.shape[:2] 16 | 17 | # If the center is None, initialize it as the center of 18 | # the image 19 | if center is None: 20 | center = (w / 2, h / 2) 21 | 22 | # Perform the rotation 23 | M = cv2.getRotationMatrix2D(center, angle, scale) 24 | rotated = cv2.warpAffine(image, M, (w, h)) 25 | 26 | # Return the rotated image 27 | return rotated 28 | 29 | def resize(image, width = None, height = None, inter = cv2.INTER_AREA): 30 | # initialize the dimensions of the image to be resized and 31 | # grab the image size 32 | dim = None 33 | (h, w) = image.shape[:2] 34 | 35 | # if both the width and height are None, then return the 36 | # original image 37 | if width is None and height is None: 38 | return image 39 | 40 | # check to see if the width is None 41 | if width is None: 42 | # calculate the ratio of the height and construct the 43 | # dimensions 44 | r = height / float(h) 45 | dim = (int(w * r), height) 46 | 47 | # otherwise, the height is None 48 | else: 49 | # calculate the ratio of the width and construct the 50 | # dimensions 51 | r = width / float(w) 52 | dim = (width, int(h * r)) 53 | 54 | # resize the image 55 | resized = cv2.resize(image, dim, interpolation = inter) 56 | 57 | # return the resized image 58 | return resized -------------------------------------------------------------------------------- /number_plate.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/number_plate.jpg -------------------------------------------------------------------------------- /plate_detedted.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/plate_detedted.jpg -------------------------------------------------------------------------------- /question14.py: -------------------------------------------------------------------------------- 1 | __author__ = 'anish' 2 | 3 | import cv2 4 | import numpy as np 5 | import imutils 6 | 7 | 8 | img_org = cv2.imread('car1.jpg') 9 | size = np.shape(img_org) 10 | if size[0] <= 776: 11 | img_org = imutils.resize(img_org , 900) 12 | 13 | img_org2 = img_org.copy() 14 | img_bw = cv2.cvtColor(img_org , cv2.COLOR_BGR2GRAY) 15 | 16 | 17 | ret3,img_thr = cv2.threshold(img_bw,125,255,cv2.THRESH_BINARY) 18 | 19 | 20 | cv2.imwrite('thresh.jpg',img_thr) 21 | 22 | img_edg = cv2.Canny(img_thr ,100,200) 23 | 24 | cv2.imwrite('cn_edge.jpg' , img_edg) 25 | 26 | 27 | 28 | kernel = cv2.getStructuringElement(cv2.MORPH_DILATE, (7, 7)) 29 | img_dil = cv2.dilate(img_edg, kernel, iterations = 1) 30 | 31 | cv2.imwrite('dilated_img.jpg',img_dil) 32 | 33 | 34 | #if you are using opencv 2.X then make sure to remove "something_else " variable from list below 35 | 36 | (somethig_else,contours ,hierarchye) = cv2.findContours(img_dil.copy(), 1, 2) 37 | cnts = sorted(contours, key = cv2.contourArea, reverse = True)[:10] 38 | 39 | screenCnt = None 40 | 41 | for c in cnts: 42 | # approximate the contour 43 | peri = cv2.arcLength(c, True) 44 | approx = cv2.approxPolyDP(c, 0.02 * peri, True) 45 | 46 | # if our approximated contour has four points, then 47 | # we can assume that we have found our screen 48 | if len(approx) == 4: 49 | screenCnt = approx 50 | break 51 | 52 | 53 | 54 | 55 | 56 | mask = np.zeros(img_bw.shape, dtype=np.uint8) 57 | roi_corners = np.array(screenCnt ,dtype=np.int32) 58 | ignore_mask_color = (255,)*1 59 | cv2.fillPoly(mask, roi_corners , ignore_mask_color) 60 | cv2.drawContours(img_org, [screenCnt], -40, (100, 255, 100), 9) 61 | cv2.imshow('original image with boundry' , img_org) 62 | cv2.imwrite('plate_detedted.jpg',img_org) 63 | 64 | 65 | ys =[screenCnt[0,0,1] , screenCnt[1,0,1] ,screenCnt[2,0,1] ,screenCnt[3,0,1]] 66 | xs =[screenCnt[0,0,0] , screenCnt[1,0,0] ,screenCnt[2,0,0] ,screenCnt[3,0,0]] 67 | 68 | ys_sorted_index = np.argsort(ys) 69 | xs_sorted_index = np.argsort(xs) 70 | 71 | x1 = screenCnt[xs_sorted_index[0],0,0] 72 | x2 = screenCnt[xs_sorted_index[3],0,0] 73 | 74 | y1 = screenCnt[ys_sorted_index[0],0,1] 75 | y2 = screenCnt[ys_sorted_index[3],0,1] 76 | 77 | 78 | 79 | img_plate = img_org2[y1:y2 , x1:x2] 80 | 81 | 82 | # for i in screenCnt: 83 | # print(i) 84 | # 85 | # print xs , ys 86 | # 87 | # print x1,x2,y1,y2 88 | 89 | 90 | 91 | 92 | 93 | cv2.imshow('number plate',img_plate) 94 | 95 | cv2.imwrite('number_plate.jpg',img_plate) 96 | cv2.waitKey(0) -------------------------------------------------------------------------------- /thresh.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gpu-poor/opencv-car-number-plate-detection-/88a36554284438dde1dc78a9dba0a23d70871d4b/thresh.jpg --------------------------------------------------------------------------------