2017-01-11 5 views
0

OCR 응용 프로그램을 구현하려고합니다. 단어 그룹을 찾고 각 단어에 대해 각 개인의 윤곽을 찾고 싶습니다. 각 단어의 윤곽을 찾았지만 각 문자의 윤곽을 표시하는 데 문제가 있습니다. 지금까지 내 코드 :OpenCV 2.4 연결된 구성 요소 찾기

imgInput = cv2.imread("inputImage.jpg")    

# convert image to grayscale 
imgGray = cv2.cvtColor(imgInput, cv2.COLOR_BGR2GRAY)   

# invert black and white 
newRet, binaryThreshold = cv2.threshold(imgGray,127,255,cv2.THRESH_BINARY_INV) 

# dilation 
rectkernel = cv2.getStructuringElement(cv2.MORPH_RECT,(15,10)) 

rectdilation = cv2.dilate(binaryThreshold, rectkernel, iterations = 1) 

outputImage = imgInput.copy() 

npaContours, npaHierarchy = cv2.findContours(rectdilation.copy(),   
              cv2.RETR_EXTERNAL,     
              cv2.CHAIN_APPROX_SIMPLE)   

for npaContour in npaContours:       
    if cv2.contourArea(npaContour) > MIN_CONTOUR_AREA:   

     [intX, intY, intW, intH] = cv2.boundingRect(npaContour)   

     cv2.rectangle(outputImage,   
       (intX, intY),     # upper left corner 
       (intX+intW,intY+intH),  # lower right corner 
       (0, 0, 255),     # red 
       2)       # thickness 

     # Get subimage of word and find contours of that word 
     imgROI = binaryThreshold[intY:intY+intH, intX:intX+intW] 


     subContours, subHierarchy = cv2.findContours(imgROI.copy(),   
              cv2.RETR_EXTERNAL,     
              cv2.CHAIN_APPROX_SIMPLE) 

     # This part is not working as I am expecting 
     for subContour in subContours: 

      [pointX, pointY, width, height] = cv2.boundingRect(subContour) 

      cv2.rectangle(outputImage, 
         (intX+pointX, intY+pointY),    
         (intX+width, intY+height),  
         (0, 255, 0), 
         2) 



cv2.imshow("original", imgInput) 
cv2.imshow("rectdilation", rectdilation) 
cv2.imshow("threshold", binaryThreshold) 
cv2.imshow("outputRect", outputImage) 

cv2.waitKey(0); 

input

dilation

final output

답변

1

모두가 올바른지, 단지 약간의 버그가은 (하위 윤곽에서) 당신의 2 cv2.rectangle 변경

cv2.rectangle(outputImage,(intX+pointX, intY+pointY),(intX+pointX+width, intY+pointY+height), (0, 255, 0),2) 

의미가 없거나 사소한 것은 아닙니다. 그러나 이것은 스스로 해결할 수있는 버그입니다.)이런 종류의 코드에 대한 디버깅은 첫 번째 단어, 첫 번째 하위 윤곽, 이미지 저장, pointX 값 검사, intX, width ... 아무것도 복잡하지 않습니다. 프로그래머로서 자주해야 할 일입니다.

행운을 빈다.

관련 문제