简体   繁体   English

OpenCV 2.4查找连接的组件

[英]OpenCV 2.4 finding connected components

I am trying to implement an OCR application. 我正在尝试实现OCR应用程序。 I want to find groups of words and for each word, I want to then find the contour of each individual character. 我想找到一组单词,然后为每个单词找到每个单独字符的轮廓。 I have found the contour for each word but I am having trouble displaying the contour for each individual character. 我已经找到了每个单词的轮廓,但是我无法显示每个字符的轮廓。 My code so far: 到目前为止,我的代码:

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);

输入

扩张

最终输出

Everything is correct, there is just a slight bug : Change your 2nd cv2.rectangle (in the subcontours) 一切都正确,只有一个小错误:更改您的第二个cv2.rectangle(在子轮廓中)

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

Not to be mean or petty, but this is a bug you could have solved yourself ;) Debugging for this sort of code is trying only for the first word, then the first subcontour, saving the images, checking values of pointX, intX, width... nothing too complicated, and it's something you'll often need to do as a programmer. 别太刻薄或卑鄙,但这是您可以解决自己的错误;)调试此类代码仅尝试第一个单词,然后尝试第一个子轮廓,保存图像,检查pointX,intX,width的值...没什么太复杂的,这是您作为程序员经常需要做的事情。

Good luck ! 祝好运 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM