이미지에서 찾아낸 다음 컨투어 영역들의 유사도를 모두 비교하여 다르게 생긴 이미지 하나를 찾아 녹색으로 표시해주세요. [code]
- User
이미지에서 찾아낸 다음 컨투어 영역들의 유사도를 모두 비교하여 다르게 생긴 이미지 하나를 찾아 녹색으로 표시해주세요.
import cv2 import numpy as np def extract_word_clusters(image_path, min_area=2000): # 이미지 로드 img = cv2.imread(image_path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 이진화 (검은색 글자를 흰색 배경에서 인식) _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV) # 팽창(Dilation)으로 글자들을 연결하여 하나의 덩어리로 만들기 kernel = np.ones((10, 10), np.uint8) dilated = cv2.dilate(binary, kernel, iterations=2) # 컨투어 찾기 contours, _ = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 원본 이미지에서 글자 컨투어만 그리기 contour_img = img.copy() filtered_contours = [] for contour in contours: area = cv2.contourArea(contour) if area > min_area: # 면적 기준 필터링 x, y, w, h = cv2.boundingRect(contour) # 배경 색상 기준 확인 (흰색 배경) mask = np.zeros(gray.shape, dtype=np.uint8) cv2.drawContours(mask, [contour], -1, 255, -1) # 컨투어 영역을 흰색으로 mean_val = cv2.mean(gray, mask=mask)[0] # 평균 색상 계산 # 평균 색상이 흰색에 가까운지 확인 if mean_val > 210: # 흰색 배경의 기준 # 컨투어 내부만 크롭하여 다시 이진화 cropped_gray = gray[y:y+h, x:x+w] _, cropped_binary = cv2.threshold(cropped_gray, 200, 255, cv2.THRESH_BINARY_INV) # 내부 컨투어 찾기 internal_contours, _ = cv2.findContours(cropped_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 내부 컨투어들을 하나의 사각형으로 합치기 if internal_contours: all_points = np.vstack(internal_contours) # 모든 점들을 합침 ix, iy, iw, ih = cv2.boundingRect(all_points) cv2.rectangle(contour_img, (x + ix, y + iy), (x + ix + iw, y + iy + ih), (0, 0, 255), 2) # 빨간 박스 filtered_contours.append((x, y, w, h, None)) return filtered_contours, contour_img def highlight_different_letter(image, different_letter_position): x, y, w, h = different_letter_position[:4] cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) # 빨간색 박스 if __name__ == "__main__": image_path = 'path_to_image.png' # 이미지 경로 설정 letters, contour_img = extract_word_clusters(image_path) cv2.imshow('Highlighted Different Letter', contour_img) cv2.waitKey(0) # 키 입력 대기 cv2.destroyAllWindows() - Assistant
- User
Traceback (most recent call last): File "/home/asitis/Coding/Toss/hangul.py", line 67, in highlight_different_letter(contour_img, letters[different_index]) ~~~~~~~^^^^^^^^^^^^^^^^^ IndexError: list index out of range
- Assistant
- User
컨투어 개수는 25, hu moments list는 29가 나오는데?
- Assistant