-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmeter_reading_processing.py
46 lines (36 loc) · 1.79 KB
/
meter_reading_processing.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
import os
import cv2
import numpy as np
path = "./meter_reading_images/"
files = [file for file in os.listdir(path) if file.endswith('.png')]
print(sorted(files))
def meter_disp_segment(img_path, debug=True):
print(img_path)
imgArr_o = cv2.imread(path + img_path)
imgArr = cv2.cvtColor(imgArr_o, cv2.COLOR_BGR2HSV)
roi_lower = np.array([40, 25, 0])
roi_upper = np.array( [80, 255, 255])
mask = cv2.inRange(imgArr, roi_lower, roi_upper)
# Bitwise-AND mask and original image
imgArr = cv2.bitwise_and(imgArr_o,imgArr_o, mask= mask)
# Find contours
image, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
wbuffer = 0.75 * w
hbuffer = 0.1 * h
imgArr_ext = imgArr_o[y:y + h + int(hbuffer), x:x + w + int(wbuffer)]
imgArr_ext_gray = cv2.cvtColor(imgArr_ext, cv2.COLOR_BGR2GRAY)
imgArr_ext_pp = cv2.adaptiveThreshold(imgArr_ext_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 199, 5)
imgArr_ext_pp = cv2.medianBlur(imgArr_ext_pp, 13)
cv2.rectangle(imgArr_o, (x, y), (x + w + int(wbuffer), y + h + int(hbuffer)), (255, 0, 255), 10)
break
if debug:
cv2.imwrite('./output/meter_disp_ext/' + img_path.split('.')[0] + '_ext.png', imgArr_ext)
cv2.imwrite('./output/mask/' + img_path.split('.')[0] + '_mask.png', mask)
cv2.imwrite('./output/meter_disp_bb/' + img_path.split('.')[0] + '_bb.png', imgArr_o)
cv2.imwrite('./output/meter_disp_ext_pp/' + img_path.split('.')[0] + '_pp.png', imgArr_ext_pp)
print(img_path + '--> DONE')
for meter in files:
meter_disp_segment(meter)