I am trying to use cv2
for identifying the odometer reading - I am trying to extract a contour where the odometer reading is present. I am unable to identify the region accurately. I am trying to get a area / rectangle contour from an image. The code is for max area from a region. Can anyone help me here ?
Following is the code
import numpy as np
import matplotlib.pyplot as plt
# assuming you have the result image store in median
median = cv2.imread("odo_2.jpg", 0)
image_gray = median
binary = cv2.bitwise_not(image_gray)
edged = cv2.Canny(binary, 50, 80, 255)
#threshold = cv2.adaptiveThreshold(edged,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
contours = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2]
rect_cnts = []
for cnt in contours:
peri = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.04 * peri, True)
(x, y, w, h) = cv2.boundingRect(cnt)
ar = w / float(h)
if (len(approx) == 4) & (ar >= 0.95 and ar <= 1.05) : # shape filtering condition
pass
else :
rect_cnts.append(cnt)
max_area = 0
football_square = None
for cnt in rect_cnts:
(x, y, w, h) = cv2.boundingRect(cnt)
if max_area < w*h:
max_area = w*h
football_square = cnt
# Draw the result
image = cv2.cvtColor(image_gray, cv2.COLOR_GRAY2RGB)
cv2.drawContours(image, [football_square], -1, (0, 0,255), 3)
cv2.imshow("Result Preview", image)
#cv2.imshow("Result Preview", edged)
cv2.waitKey(0)
import cv2 as cv
low_H = 8
low_S = 106
low_V = 156
high_H = 25
high_S = 231
high_V = 237
frame = cv.imread('J.jpg')
frame_HSV = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
frame_threshold = cv.inRange(frame_HSV, (low_H, low_S, low_V), (high_H, high_S, high_V))
cv.imwrite('out_odo.png', frame_threshold)