Created
September 17, 2021 17:37
-
-
Save AdroitAnandAI/19205c2bf829c93a73c2c2aac84153c1 to your computer and use it in GitHub Desktop.
To detect the license plate rectangle from an image with car
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
img = cv2.imread("car.png") | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
ret, gray = cv2.threshold(gray, 250,255,0) | |
# applying different thresholding techniques on the input image | |
# all pixels value above 120 will be set to 255 | |
ret, thresh2 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY_INV) | |
gray = cv2.cvtColor(thresh2, cv2.COLOR_BGR2GRAY) | |
contours, _ = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | |
for cnt in contours: | |
approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True) | |
# The area constant is computed by the expectation of | |
# how near the car can come near the security barrier | |
if len(approx) == 4 and cv2.contourArea(cnt) > 1000: | |
# Draw the License Plate Contour | |
cv2.drawContours(img, [approx], 0, (0), 5) | |
cv2.imshow("shapes", img) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment