Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图从颗粒状图像中提取文本_Python_Opencv_Python Tesseract_Adaptive Threshold - Fatal编程技术网

Python 试图从颗粒状图像中提取文本

Python 试图从颗粒状图像中提取文本,python,opencv,python-tesseract,adaptive-threshold,Python,Opencv,Python Tesseract,Adaptive Threshold,我一直在尝试从一张粗糙的图像中提取文本, 这是原始图像 下面是我用来尝试和处理此图像的代码 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imshow('img_gray',img_gray) cv2.waitKey(0) #img_bin = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINA

我一直在尝试从一张粗糙的图像中提取文本, 这是原始图像

下面是我用来尝试和处理此图像的代码

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imshow('img_gray',img_gray)
    cv2.waitKey(0)
    #img_bin = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 15) # 21 and 15 need to be set for image12
    img_bin = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 27, 15) # 21 and 15 need to be set for image12

    cv2.imshow('img_bin',img_bin)
    cv2.waitKey(0)

    fig, axs = plt.subplots(3)
    axs[0].imshow(img_gray, cmap="gray")
    axs[1].imshow(img_bin, cmap="gray")
    # Merge dots into characters using erosion
    kernel = np.ones((5, 5), np.uint8)
    #kernel = np.ones((15, 15), np.uint8)   
    img_eroded = cv2.erode(img_bin, kernel, iterations=1)
    axs[2].imshow(img_eroded, cmap="gray")
    cv2.imshow('img_eroded',img_bin)
    cv2.waitKey(0)
    fig.show()

    # Obtain string using psm 8 (treat the image as a single word)
    ocr_string = pytesseract.image_to_string(img_eroded, lang= 'eng', config="--psm 6")
    return ocr_string
这是将背景变成灰色后的灰色图像

这是应用自适应阈值后的图像

这是腐蚀后的图像


在最终的图像(
img\u腐蚀
)中,实际文本周围仍然有很多点,这可能导致
image\u to\u string
函数抛出一些垃圾值。是否有办法进一步处理此图像,或改进现有代码以提取文本Pac=2665.7W

您可以尝试使用
pytesseract.image\u to\u data
功能,并根据相关的置信度、位置和大小过滤掉不好的单词。更复杂的方法是首先确定“Pac=”和“W”的位置(例如使用tesseract或模板匹配)。然后,可以计算每个数字的边界框,并使用tesseract或模板匹配来确定每个数字的值。假设始终显示“Pac=”和“W”,并且它们之间的位数是固定的(或者可以使用“Pac=W”的平均字符宽度导出)。