Page 273 - MDP2020-1
P. 273

················································································  명장양성프로젝트  【MDP】  과제발표회  자료집  Ⅰ    |  267


                    if   img_cropped.shape[1]   /   img_cropped.shape[0]      <     MIN_PLATE_RATIO        or
            img_cropped.shape[1]  /  img_cropped.shape[0]  <  MIN_PLATE_RATIO  >  MAX_PLATE_RATIO:
                            continue

                    plate_imgs.append(img_cropped)
                    plate_infos.append({
                            'x':  int(plate_cx  -  plate_width  /  2),
                            'y':  int(plate_cy  -  plate_height  /  2),
                            'w':  int(plate_width),
                            'h':  int(plate_height)
                    })

                    plt.subplot(len(matched_result),  1,  i+1)
                    plt.imshow(img_cropped,  cmap='gray')
            longest_idx,  longest_text  =  -1,  0
            plate_chars  =  []
            for  i,  plate_img  in  enumerate(plate_imgs):
                    plate_img  =  cv2.resize(plate_img,  dsize=(0,  0),  fx=1.6,  fy=1.6)
                    _,  plate_img  =  cv2.threshold(plate_img,  thresh=0.0,  maxval=255.0,  type=cv2.THRESH_BINARY
            |  cv2.THRESH_OTSU)

                    #  find  contours  again  (same  as  above)
                    contours,    _        =         cv2.findContours(plate_img,        mode=cv2.RETR_LIST,
            method=cv2.CHAIN_APPROX_SIMPLE)

                    plate_min_x,  plate_min_y  =  plate_img.shape[1],  plate_img.shape[0]
                    plate_max_x,  plate_max_y  =  0,  0
                    for  contour  in  contours:
                            x,  y,  w,  h  =  cv2.boundingRect(contour)

                            area  =  w  *  h
                            ratio  =  w  /  h
                            if  area  >  MIN_AREA  \
                            and  w  >  MIN_WIDTH  and  h  >  MIN_HEIGHT  \
                            and  MIN_RATIO  <  ratio  <  MAX_RATIO:
                                    if  x  <  plate_min_x:
                                            plate_min_x  =  x
                                    if  y  <  plate_min_y:
                                            plate_min_y  =  y
                                    if  x  +  w  >  plate_max_x:
                                            plate_max_x  =  x  +  w
                                    if  y  +  h  >  plate_max_y:
                                            plate_max_y  =  y  +  h

                    img_result  =  plate_img[plate_min_y:plate_max_y,  plate_min_x:plate_max_x]
   268   269   270   271   272   273   274   275   276   277   278