2016-09-08 6 views
1

OpenCV Python을 처음 접했습니다. 정말 도움이 필요합니다.OpenCV로 손글씨 글자 추출하기

여기에서 내가하려는 것은 아래 이미지에서이 단어들을 추출하는 것입니다.

hand drawn image

단어와 모양이 그려진 모든 손, 그래서 그들은 완벽하지. 아래에 코딩을 했어. 모든

첫째는, 내가, 내가

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3 , 3)) 
dilated = cv2.dilate(new_img,kernel,iterations = 3) 

내가 팽창 내용을 팽창

ret, new_img = cv2.threshold(image_final, 100 , 255, cv2.THRESH_BINARY_INV) 

후 내용을 보여 THRESH_INV을 사용하여 다음 이미지

img_final = cv2.imread(file_name) 
img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 

를 그레이 스케일 텍스트를 하나의 클러스터로 식별 할 수 있기 때문에 이미지가 있습니다.

그 후, 나는 윤곽 주위 boundingRect을 적용하고 이것은 내가 그 후 가지고 무엇 사각형

contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # get contours 
index = 0 
for contour in contours: 

    # get rectangle bounding contour 
    [x,y,w,h] = cv2.boundingRect(contour) 

    #Don't plot small false positives that aren't text 
    if w < 10 or h < 10: 
     continue 

    # draw rectangle around contour on original image 
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,255),2) 

주위에 그립니다.

result image

나는 텍스트 중 하나를 감지 할 경우에만 수 있어요. 나는 다른 많은 방법을 시도해 왔지만 이것은 내가 가진 옷장 결과이며 요구 사항을 충족시키지 못합니다.

내가 텍스트를 식별하는 이유는 경계 사각형 인 "boundingRect()"를 배치하여이 이미지의 각 텍스트의 X 및 Y 좌표를 얻을 수 있기 때문입니다.

도와주세요. 정말 고마워요

+0

당신이 이미지에 OCR을 수행하는 정팔 포체를 사용할 수 있습니까? – James

+0

@ Kells1986 이유는 다른 목적으로이 단어들 각각의 좌표를 알아야하기 때문입니다. 식별 된 단어 주위에 boundingRect()를 사용하면이 이미지 자체에서 단어의 X 및 Y 좌표를 얻을 수 있습니다. – PengGusto

답변

4

문자의 연결된 구성 요소가 나머지 다이어그램의 큰 획보다 훨씬 작다는 사실을 사용할 수 있습니다.

코드에서 opencv3 연결 구성 요소를 사용했지만 findContours를 사용하여 동일한 작업을 수행 할 수 있습니다.

코드 :

import cv2 
import numpy as np 

# Params 
maxArea = 150 
minArea = 10 

# Read image 
I = cv2.imread('i.jpg') 

# Convert to gray 
Igray = cv2.cvtColor(I,cv2.COLOR_RGB2GRAY) 

# Threshold 
ret, Ithresh = cv2.threshold(Igray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 

# Keep only small components but not to small 
comp = cv2.connectedComponentsWithStats(Ithresh) 

labels = comp[1] 
labelStats = comp[2] 
labelAreas = labelStats[:,4] 

for compLabel in range(1,comp[0],1): 

    if labelAreas[compLabel] > maxArea or labelAreas[compLabel] < minArea: 
     labels[labels==compLabel] = 0 

labels[labels>0] = 1 

# Do dilation 
se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(25,25)) 
IdilateText = cv2.morphologyEx(labels.astype(np.uint8),cv2.MORPH_DILATE,se) 

# Find connected component again 
comp = cv2.connectedComponentsWithStats(IdilateText) 

# Draw a rectangle around the text 
labels = comp[1] 
labelStats = comp[2] 
#labelAreas = labelStats[:,4] 

for compLabel in range(1,comp[0],1): 

    cv2.rectangle(I,(labelStats[compLabel,0],labelStats[compLabel,1]),(labelStats[compLabel,0]+labelStats[compLabel,2],labelStats[compLabel,1]+labelStats[compLabel,3]),(0,0,255),2) 

enter image description here

+1

thats amazing bro !! 내가 가서 코드를 읽고 이해하도록하겠습니다. – PengGusto

+0

connectedComponentsWithStats()의 의미는 무엇입니까? 나는 이것의 의미를 찾기 위해 주위를 둘러 보았지만 그 설명은 너무 이해하기 힘들다. – PengGusto

+0

이것을보십시오 : http://stackoverflow.com/questions/35854197/how-to-use-opencvs-connected-components-with-stats-in-python –