2017-01-11 8 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

すべてがちょうど若干のバグがあり、正確である:(subcontoursに)あなたの第二cv2.rectangleを変更

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

これは意味がありませんが、これはあなた自身で解決できたバグです) この種のコードのデバッグは、最初の単語だけを試してから、最初のサブコンセプト、画像の保存、pointXの値のチェック、 intX、width ...複雑すぎるものはありません。プログラマとしてしばしば必要となるものです。

幸運を祈る!

関連する問題