본문 바로가기

PYTHON/자동화

opencv 템플릿 이미지비교

728x90

미리 올바른 결과이미지를 저장해두고 (template)

다른 이미지와 비교해서 해당영역에 네모를 그려 저장.

일정 확률 이상 이미지가 비슷하면 True

(흑백으로 바꾸어 비교함)

 

```python

# 템플릿 이미지 유사도 측정
def templatematching_TF(checkobject,result_list): 

    imageNDArray = cv2.imread("./automation_screenshot/"+checkobject+".png") # 캡처 이미지 업로드
    grayscaleImageNDArray = cv2.cvtColor(imageNDArray, cv2.COLOR_BGR2GRAY) # 흑백
    templateImageNDArray = cv2.imread("./automation_img_template/"+checkobject+"_template.png") # 예상된 결과 이미지 업로드
    grayscaleTemplateImageNDArray = cv2.cvtColor(templateImageNDArray, cv2.COLOR_BGR2GRAY) # 흑백

    imageWidth, imageHeight = grayscaleTemplateImageNDArray.shape[::-1] # 템플릿 이미지 크기 저장
    outputNDArray = cv2.matchTemplate(grayscaleImageNDArray, grayscaleTemplateImageNDArray, cv2.TM_CCOEFF_NORMED) # 두 개의 이미지 비교
     
    minimumValue, maximumValue, minimumLocation, maximumLocation = cv2.minMaxLoc(outputNDArray)
    leftTopLocation = maximumLocation
    rightBottomLocation = (leftTopLocation[0] + imageWidth, leftTopLocation[1] + imageHeight)

    cv2.rectangle(imageNDArray, leftTopLocation, rightBottomLocation, 255, 2) # 같은 부분 네모로 
    cv2.imwrite("./automation_img_result/"+checkobject+"_result.jpg", imageNDArray) # 네모로 표시해 저장

    threshold = 0.8 # 정확도 조절가능
    flag = False
    if np.amax(outputNDArray) > threshold:
        flag = True
    print(checkobject+": "+str(flag))
    result_list.append(flag)

```

'PYTHON > 자동화' 카테고리의 다른 글

actionchain  (0) 2019.12.19
순차적 thread  (0) 2019.12.19
selenium 속도향상 옵션추가  (0) 2019.12.19
회사컴도 과부하 걸리는..  (0) 2019.11.22
Implicit Waits vs explicitly wait (Expected Conditions)  (0) 2019.11.22