我使用 cv2.connectedComponentsWithStats 时的噪音

noise when i use cv2.connectedComponentsWithStats

我想从这张图片中提取字母:

当我尝试使用 connectedComponentsWithStats 时,我得到了很多标签,但它们很吵,就像这个例子: 这是我的代码中应该提取字母的部分:

def ExtractLetters(img):
    blur_radius = 0.1
    nlabel, labels,stats,centroids = cv2.connectedComponentsWithStats(img)
    thresh_size=30
    for i in range(1, nlabel):
        img = np.zeros(img.shape, dtype=np.uint8)
        if (stats[i, cv2.CC_STAT_WIDTH] > thresh_size) or (stats[i, cv2.CC_STAT_HEIGHT]>thresh_size):
            img[labels == i] = 255
            img = 255 - img
            cv2.imshow("test", img)
            cv2.waitKey()

你知道为什么会出现这些噪音吗?

在找到connectedComponentsWithStats之前,需要将图像RGBA转换为灰度和阈值。

您的二进制图像似乎已保存为 JPEG,引入了压缩瑕疵。读回图像时,它不再只有两个不同的值。

在进行连通分量分析之前对图像进行阈值处理可以解决您的问题。