이 문서는 AI 기반 음식 잔반 분석 시스템의 핵심 기술과 구현 방식을 설명합니다. 시스템은 세 가지 주요 기술(역투영 알고리즘, MiDaS 깊이 추정, ResNet 분류)을 융합하여 음식 잔반량을 정확하게 추정합니다. 이 기술들이 어떻게 작동하고 조합되는지 상세히 분석합니다.
역투영 알고리즘은 색상 기반 분할 기법으로, 특정 색상 패턴을 가진 영역을 찾아내는 방식입니다.
HSV 색공간 변환
python
hsv_t = cv2.cvtColor(target_img, cv2.COLOR_BGR2HSV)
hsv_r = cv2.cvtColor(reference_img, cv2.COLOR_BGR2HSV)
히스토그램 생성 및 정규화
python
roi_hist = cv2.calcHist([hsv_r], ch_idx, None, hist_size, ranges)
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
역투영 수행
python
dst = cv2.calcBackProject([hsv_t], ch_idx, roi_hist, ranges, 1)
후처리 과정
python
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (blur_kernel, blur_kernel))
cv2.filter2D(dst, -1, disc, dst)
이진화 및 임계값 적용
python
if use_otsu:
_, mask = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
elif use_percentile:
percentile = 100 - food_percent
T = np.percentile(dst, percentile)
_, mask = cv2.threshold(dst, T, 255, cv2.THRESH_BINARY)
else:
_, mask = cv2.threshold(dst, thresh, 255, 0)
모폴로지 연산
python
if morph_op == 'close':
k = np.ones((morph_kernel, morph_kernel), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, k, iterations=morph_iter)
작은 객체 제거
python
mask_bool = remove_small_objects(mask_bool, min_size=min_size)
반사광 마스크 적용(선택적)
python
if use_specular_mask:
hsv = cv2.cvtColor(target_img, cv2.COLOR_BGR2HSV)
specular_mask = (hsv[:,:,2] > specular_v_thresh) & (hsv[:,:,1] < specular_s_thresh)
mask_bool = mask_bool & (~specular_mask)
음식 비율 계산
python
black_ratio = mask_bool.mean() * 100