Python - 识别文本图像

时间:2015-04-28 19:55:26

标签: python opencv image-recognition

我编写此代码的前提是识别图像中的单词。此方法查找链接中显示的一些但不是所有单词。

我的代码是:

def methods_text_recognition_and_delete(self, path_floorplans, f, path_fw):

        image = cv2.imread(path_floorplans +f)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # grayscale
        _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)  # threshold
        kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
        dilated = cv2.dilate(thresh, kernel, iterations = 1)  # dilate
        contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  # get contours

        # for each contour found, draw a rectangle around it on original image
        for contour in contours:
            # get rectangle bounding contour
            [x, y, w, h] = cv2.boundingRect(contour)

            # discard areas that are too large
            if h > 300 and w > 300:
                continue

            # discard areas that are too small
            if h < 10 or w < 10:
                continue

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

        # write original image with added contours to disk
        cv2.imwrite(path_fw + 'text_recognition_' + f, image)

有人建议改善这种方法的质量吗?

图片示例:http://imageshack.com/a/img673/1598/QFaZ3i.png

0 个答案:

没有答案