Python/OpenCV
[OpenCV] convexHull 활용 이미지 텍스트 검출
hotelshoe
2023. 1. 27. 18:19
반응형
OpenCV에서 제공되는 convexHull을 활용하여 이미지 속 텍스트를 검출하는 테스트를 진행해 보겠습니다.
OpenCV 공식 튜토리얼 문서를 기반으로 작성되었으며 자세한 사항은 하단 링크를 참조 바랍니다.
https://docs.opencv.org/3.4/d9/df8/tutorial_root.html
소스 코드
import cv2
img = cv2.imread('./image.png') #- 검출할 이미지 경로
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(5, 5),0)
mser = cv2.MSER_create()
regions,_ = mser.detectRegions(gray)
clone = img.copy()
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
remove1 = []
for i,c1 in enumerate(hulls):
x, y, w, h = cv2.boundingRect(c1)
r1_start = (x, y)
r1_end = (x+w, y+h)
for j,c2 in enumerate(hulls):
if i == j:
continue
x, y, w, h = cv2.boundingRect(c2)
r2_start = (x, y)
r2_end = (x+w, y+h)
if r1_start[0]> r2_start[0] and r1_start[1] > r2_start[1] and r1_end[0] < r2_end[0] and r1_end[1] < r2_end[1]:
remove1.append(i)
for j,cnt in enumerate(hulls):
if j in remove1: continue
x, y, w, h = cv2.boundingRect(cnt)
margin = 10
cv2.rectangle(clone, (x-margin, y-margin), (x + w + margin, y + h + margin), (0, 255, 0), 1)
cv2.imshow('Text Detection', clone)
cv2.waitKey(0)
테스트
- 적용전
- 적용후
반응형