Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 14 segment display decoder and viewer (simulator)
- # the segment image is taken from below link
- # https://www.youtube.com/channel/UCXBrLU1A-J51eVtTOEsIbAw/community?lb=UgkxViWPs-1e6Ii89cxlRPN2EHmUE_I0ngLM
- import cv2
- import numpy as np
- import ctypes
- ctypes.windll.user32.SetProcessDPIAware()
- # 14 segment chars image
- img = cv2.imread('segment.png')
- seg_area = [(90, 114), (774, 1935)] # first and last display center coords
- h_count = 8
- v_count = 12
- # pixels lower than this grayscale value will be considered as "ON"
- threshold = 100
- inverse_threshold = True
- # position of each segments (clockwise, from outer to inner, centered)
- seg_coords = [(1, -67), (36, -37), (38, 32), (2, 71), (-33, 37), (-34, -35), (-15, 3), (20, 2), (-16, -33), (2, -33), (18, -32), (20, 37), (2, 36), (-15, 37)]
- seg_datas = []
- # preprocess the image
- img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- threshold_type = cv2.THRESH_BINARY_INV if inverse_threshold else cv2.THRESH_BINARY
- ret, img = cv2.threshold(img, threshold, 255, threshold_type)
- # parse 14 segment data - detect segment by distance
- seg_w = (seg_area[1][0] - seg_area[0][0]) / (h_count-1)
- seg_h = (seg_area[1][1] - seg_area[0][1]) / (v_count-1)
- for v in range(v_count):
- for h in range(h_count):
- # current char index
- index = v*h_count + h
- seg_data = 0 # 14-bit binary data
- # current display center coord
- cx = ((seg_area[1][0] - seg_area[0][0]) / (h_count-1)) * h + seg_area[0][0]
- cy = ((seg_area[1][1] - seg_area[0][1]) / (v_count-1)) * v + seg_area[0][1]
- # crop current display image
- x0, x1 = int(cx - seg_w/2), int(cx + seg_w/2)
- y0, y1 = int(cy - seg_h/2), int(cy + seg_h/2)
- segment_img = img[y0:y1, x0:x1]
- # find contours and measure each distance to the nearest segment center
- contours, _ = cv2.findContours(segment_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- for contour in contours:
- dists = []
- for coord in seg_coords:
- x, y = coord
- x = int(x+seg_w/2)
- y = int(y+seg_h/2)
- dist = cv2.pointPolygonTest(contour, (x, y), True)
- dists.append(dist)
- # find the nearest segment index
- seg_index = np.argmax(np.array(dists))
- seg_data |= 1 << seg_index
- # append to the list
- seg_datas.append(seg_data)
- # print char segment datas
- for data in seg_datas:
- print(f'0x{data:04x}, ', end='')
- # segment display simulator
- # the last image is used as "full segment display" reference
- x0, x1 = int(seg_area[1][0] - seg_w/2), int(seg_area[1][0] + seg_w/2)
- y0, y1 = int(seg_area[1][1] - seg_h/2), int(seg_area[1][1] + seg_h/2)
- segment_img = img[y0:y1, x0:x1]
- # find each segment with segment coordinates
- segment_contours = []
- contours, _ = cv2.findContours(segment_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- for coord in seg_coords:
- x, y = coord
- x = int(x+seg_w/2)
- y = int(y+seg_h/2)
- for contour in contours:
- if cv2.pointPolygonTest(contour, (x, y), False) > 0:
- segment_contours.append(contour)
- break
- def draw_segment(segment_img, char_index, offset):
- seg_data = seg_datas[char_index]
- for seg_index in range(14):
- if seg_data & (1 << seg_index):
- segment_img = cv2.drawContours(segment_img, [segment_contours[seg_index]], -1, 255, -1, offset=offset)
- return segment_img
- # main view loop
- window = '14 segment viewer'
- char_count = 10
- text_list = list(' '*char_count)
- auto_text_list = list('')
- while True:
- img = np.zeros((int(seg_h), int(seg_w*char_count)), 'uint8')
- for i, char in enumerate(text_list):
- x = int(i*seg_w)
- img = draw_segment(img, ord(char)-32, (x, 0))
- cv2.imshow(window, img)
- if len(auto_text_list) != 0:
- key = cv2.waitKey(500)
- print(key)
- if key > 0:
- auto_text_list = []
- else:
- key = ord(auto_text_list.pop(0))
- else:
- key = cv2.waitKey(0)
- if cv2.getWindowProperty(window, cv2.WND_PROP_VISIBLE) < 1:
- break
- if key == 8: # backspace
- text_list = text_list[:-1]
- text_list.insert(0, ' ')
- elif key == 13: # enter
- text_list = list(' '*char_count)
- elif(key & 0xFF): # other keys
- text_list.pop(0)
- text_list.append(chr(key))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement