Page 269 - MDP2020-1
P. 269

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


                다.  라즈베리파이  4
            버전  :  Raspberry  Pi  4  Model  B  REV  1.2  /  Python  2.7.16  /  Thonny  3.2.6


            <car_nuver.py>  //  차량번호  인식  시스템

            import  cv2
            import  numpy  as  np
            import  matplotlib.pyplot  as  plt
            import  pytesseract
            plt.style.use('dark_background')
            img_ori  =  cv2.imread('123.jpg')
            height,  width,  channel  =  img_ori.shape
            plt.figure(figsize=(12,  10))
            plt.imshow(img_ori,  cmap='gray')
            gray  =  cv2.cvtColor(img_ori,  cv2.COLOR_BGR2GRAY)
            plt.figure(figsize=(12,  10))
            plt.imshow(gray,  cmap='gray')
            structuringElement  =  cv2.getStructuringElement(cv2.MORPH_RECT,  (3,  3))
            imgTopHat  =  cv2.morphologyEx(gray,  cv2.MORPH_TOPHAT,  structuringElement)
            imgBlackHat  =  cv2.morphologyEx(gray,  cv2.MORPH_BLACKHAT,  structuringElement)
            imgGrayscalePlusTopHat  =  cv2.add(gray,  imgTopHat)
            gray  =  cv2.subtract(imgGrayscalePlusTopHat,  imgBlackHat)`
            img_blurred  =  cv2.GaussianBlur(gray,  ksize=(5,  5),  sigmaX=0)
            img_thresh  =  cv2.adaptiveThreshold(
                    img_blurred,
                    maxValue=255.0,
                    adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                    thresholdType=cv2.THRESH_BINARY_INV,
                    blockSize=19,
                    C=9
            )plt.figure(figsize=(12,  10))
            plt.imshow(gray,  cmap='gray')
            contours,  _  =  cv2.findContours(
                    img_thresh,
                    mode=cv2.RETR_LIST,
                    method=cv2.CHAIN_APPROX_SIMPLE
            )temp_result  =  np.zeros((height,  width,  channel),  dtype=np.uint8)
            cv2.drawContours(temp_result,  contours=contours,  contourIdx=-1,  color=(255,  255,  255))
            plt.figure(figsize=(12,  10))
            plt.imshow(temp_result)
            temp_result  =  np.zeros((height,  width,  channel),  dtype=np.uint8)
            contours_dict  =  []
            for  contour  in  contours:
                    x,  y,  w,  h  =  cv2.boundingRect(contour)
                    cv2.rectangle(temp_result,  pt1=(x,  y),  pt2=(x+w,  y+h),  color=(255,  255,  255),  thickness=2)
   264   265   266   267   268   269   270   271   272   273   274