How to identify a closed rectangle contour with odometer reading in python opencv?

Question:

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)

Image Input

Asked By: Shiva Prakash

||

Answers:

Do you need the values or is the something that you want to learn image processing with? If the first is the case, my suggestion is you try a different approach.

  1. There might be a small possibility that you can tap into the microcontroller controlling the LCD. Maybe there is some plug in the case or some pins for that on the circuit board. Maybe you can use I2C or SPI to get the values.

  2. If you have to digitize analog instruments and you can’t hack into the chip controlling the LCD you set up a very “controlled environment” to read the image.
    In your case that would mean to build a dark case around the odometer and use the backlight of the LCD as only light source. So there are no reflections, etc. Then fix the camera to the case and make sure you get a sharp image. In that image grab the LCD using a fixed region and feed it into a OCR algorithm. There are OCRs that work like a charm on that LCD font.

Answered By: Joe
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)

Result:
enter image description here

Answered By: Alex Alex