물체 검출과 오검출 평가 · 학습
실행 조건
opencv-python-headless·NumPy·Matplotlib을 사용합니다.
이번에 배울 내용
검출 개수가 많다고 좋은 설정은 아닙니다. 알고 있는 원의 위치·크기와 비교해 오검출을 판단합니다.
전체 흐름
정답 영상 → Hough 검출 → 일대일 대응 → 누락·오검출
검출과 정답을 대응시키기
원을 찾았다는 결과에는 중심과 반지름이 포함됩니다. 정답을 아는 작은 영상을 먼저 만들면 누락·중복·오검출을 분리할 수 있습니다. 원 하나와 여러 검출을 동시에 정답으로 대응시키지 않습니다.
임계값과 전처리의 역할
HoughCircles의 검출 기준을 낮추면 더 많은 후보를 얻을 수 있지만 잘못된 검출도 늘 수 있습니다. 잡음을 줄이는 Filter는 경계를 흐릴 수도 있습니다.
검출 수·위치 오차·처리 시간을 같은 조건에서 비교합니다.
풀이 예제
160×200 Gray 영상에 중심 (50,50), (140,70), (90,120), 반지름 15,20,12인 원을 그립니다.
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
truth=[(50,50,15),(140,70,20),(90,120,12)]
image=np.full((160,200),230,dtype=np.uint8)
for x,y,r in truth:cv.circle(image,(x,y),r,30,-1)
blur=cv.GaussianBlur(image,(5,5),1)
def detect(frame,threshold=20):
return cv.HoughCircles(frame,cv.HOUGH_GRADIENT,dp=1,minDist=25,param1=100,param2=threshold,minRadius=8,maxRadius=25)
found=detect(blur);print(found)
plt.imshow(image,cmap='gray')
if found is not None:
for x,y,r in found[0]:plt.gca().add_patch(plt.Circle((x,y),r,fill=False,color='tab:red'))
[[[140.5 70.5 19.6] [ 50.5 50.5 14.7] [ 90.5 120.5 11.3]]]