티스토리 뷰

목차



    반응형

    12월 31일로 같은 날이지만 쳐 자고 일어났음 ^^

    눈뜨자마자 컴 켜고 어제 테스트 마친거 소스분석 하고 결과 포스팅

    나란년...쥰내 부지런함 ㅇㅈ? ㅇㅇ

     

    전 글에서 이거 하고 싶다고 했었고 해본 결과 적어보려함

    https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html

     

    Template Matching — OpenCV-Python Tutorials 1 documentation

    Template Matching Goals In this chapter, you will learn To find objects in an image using Template Matching You will see these functions : cv2.matchTemplate(), cv2.minMaxLoc() Theory Template Matching is a method for searching and finding the location of a

    opencv-python-tutroals.readthedocs.io

     

     

    아래 사진으로 테스트 해볼건데

    난 연애인 누가 누군지 잘 모름..그 쪽에 관심 없고 걍 아무거나 쳐서 이런게 나왔길래 간택함

    target image
    template image

     

     

    *소스코드 보기

    더보기

     

    # 패키지 쳐 넣기

    import cv2
    import numpy as np
    from matplotlib import pyplot as plt

     

    # 사진 쳐 읽어옴, 템플릿 이미지 크기 저장
    img = cv2.imread('auction.png',0)
    img2 = img.copy()
    template = cv2.imread('item.png',0)
    w, h = template.shape[::-1]

    # All the 6 methods for comparison in a list , 방식이 6개 있고 matchTemplate 인자로 쓰임
    methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
                'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']

    for meth in methods:
        img = img2.copy()
        method = eval(meth)

        # Apply template Matching
        res = cv2.matchTemplate(img,template,method)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

        # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
        if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
            top_left = min_loc
        else:
            top_left = max_loc
        bottom_right = (top_left[0] + w, top_left[1] + h)

        cv2.rectangle(img, top_left, bottom_right, 255, 2)

        plt.subplot(121),plt.imshow(res,cmap = 'gray')
        plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
        plt.subplot(122),plt.imshow(img,cmap = 'gray')
        plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
        plt.suptitle(meth)

        plt.show()

     

    결과 >>>

     

     

     

     

    3번째꺼만 애꾸됬다

     

    뭔지 모르겠지만 3번째꺼는 쓰지 않는걸로 ㅇㅋ

     

     

     

     

    반응형