在这里,我使用下面的脚本来消除图像附近的黑点,并消除数字上方的直通线,但它会消除噪点,但无法正常进行。

def get_string(img_path):
    # Read image with opencv
    img = cv2.imread(img_path)

    # Convert to gray
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Apply dilation and erosion to remove some noise
    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=12)
    img = cv2.erode(img, kernel, iterations=12)

    # Write image after removed noise
    cv2.imwrite(src_path + "removed_noise.png", img)

    #  Apply threshold to get image with only black and white
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)

    # Write the image after apply opencv to do some ...
    cv2.imwrite(src_path + "thres.png", img)

    # Recognize text with tesseract for python
    result = pytesseract.image_to_string(Image.open(src_path + "vertical_final.jpg"))

    # Remove template file
    #os.remove(temp)

    return result

但它不能正常工作。

输入图片:

输出图像:-

我需要有人帮助我解决这些问题,对此深表感谢。
源代码:-

def get_string(img_path):
    # Read image with opencv
    img = cv2.imread(img_path)

    # Convert to gray
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Apply dilation and erosion to remove some noise
    kernel = np.ones((1,20), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)

    #img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

    kernel = np.ones((1, 1), np.uint8)
    #img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

    cv2.imwrite(src_path + "removed_noise.png", img)

    img3 = cv2.subtract(cv2.imread(src_path + "removed_noise.png"),cv2.imread(src_path + "tax_amount.png"))

    cv2.imwrite(src_path + "removed_noise_makes_00.png", img3)

    lower_black = np.array([0,0,0], dtype = "uint16")
    upper_black = np.array([70,70,70], dtype = "uint16")
    black_mask = cv2.inRange(img3, lower_black, upper_black)
    black_mask[np.where((black_mask == [0] ).all(axis = 1))] = [255]

    opening = cv2.morphologyEx(black_mask, cv2.MORPH_CLOSE, kernel)

    cv2.imwrite(src_path + "removed_noise_makes_00_1.png", opening)

    # Recognize text with tesseract for python
    result = pytesseract.image_to_string(Image.open(src_path + "removed_noise_makes_00_1.png"))

    # Remove template file
    #os.remove(temp)

    return result

最佳答案

你在哪里

kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=12)

您使用1x1结构元素(SE)进行了12次膨胀。除非OpenCV对此类SE进行特殊处理,否则此代码根本不会更改您的镜像。

您应该创建更大的SE:
kernel = np.ones((7, 7), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)

这将首先扩大然后侵 eclipse 结果。这完成的是小的(薄)黑色区域消失了。这些是SE不适合的区域。这和
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

要删除长线,您想使用拉长的SE进行闭合:
kernel = np.ones((1, 30), np.uint8)
line = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

这仅留下水平线。 imgline的区别是没有行的文本。

如果您认为imglinetext的总和,那么img - line将是text。但是,仍然存在一个小问题:img具有白色背景(255)和黑色前景。因此,实际上是img = 255 - text - line,并且您在上面找到的line图像实际上是255 - line,因为它也有白色背景。因此,直接采取差异不会产生预期的效果。

解决方案是先反转图像:
img = 255 - img;
line = 255 - line;
text = img - line;

关于python - 如何使用opencv从图像中去除其他噪音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48718034/

10-12 23:06