Page 272 - MDP2020-1
P. 272

|    인천전자마이스터고등학교  ·············································································································
            266

            temp_result  =  np.zeros((height,  width,  channel),  dtype=np.uint8)
            for  r  in  matched_result:
                    for  d  in  r:
            #                  cv2.drawContours(temp_result,  d['contour'],  -1,  (255,  255,  255))
                            cv2.rectangle(temp_result,   pt1=(d['x'],   d['y']),   pt2=(d['x']+d['w'],   d['y']+d['h']),
            color=(255,  255,  255),  thickness=2)
            plt.figure(figsize=(12,  10))
            plt.imshow(temp_result,  cmap='gray')
            PLATE_WIDTH_PADDING  =  1.3  #  1.3
            PLATE_HEIGHT_PADDING  =  1.5  #  1.5
            MIN_PLATE_RATIO  =  3
            MAX_PLATE_RATIO  =  10
            plate_imgs  =  []
            plate_infos  =  []
            for  i,  matched_chars  in  enumerate(matched_result):
                    sorted_chars  =  sorted(matched_chars,  key=lambda  x:  x['cx'])
                    plate_cx  =  (sorted_chars[0]['cx']  +  sorted_chars[-1]['cx'])  /  2
                    plate_cy  =  (sorted_chars[0]['cy']  +  sorted_chars[-1]['cy'])  /  2

                    plate_width  =  (sorted_chars[-1]['x']  +  sorted_chars[-1]['w']  -  sorted_chars[0]['x'])  *
            PLATE_WIDTH_PADDING

                    sum_height  =  0
                    for  d  in  sorted_chars:
                            sum_height  +=  d['h']
                    plate_height  =  int(sum_height  /  len(sorted_chars)  *  PLATE_HEIGHT_PADDING)

                    triangle_height  =  sorted_chars[-1]['cy']  -  sorted_chars[0]['cy']
                    triangle_hypotenus  =  np.linalg.norm(
                            np.array([sorted_chars[0]['cx'],  sorted_chars[0]['cy']])  -
                            np.array([sorted_chars[-1]['cx'],  sorted_chars[-1]['cy']])
                    )

                    angle  =  np.degrees(np.arcsin(triangle_height  /  triangle_hypotenus))

                    rotation_matrix  =  cv2.getRotationMatrix2D(center=(plate_cx,  plate_cy),  angle=angle,  scale=1.0)

                    img_rotated  =  cv2.warpAffine(img_thresh,  M=rotation_matrix,  dsize=(width,  height))

                    img_cropped  =  cv2.getRectSubPix(
                            img_rotated,
                            patchSize=(int(plate_width),  int(plate_height)),
                            center=(int(plate_cx),  int(plate_cy))
                    )
   267   268   269   270   271   272   273   274   275   276   277