如何使用 OpenCV Python 在图像中仅保留黑色文本?

问题描述 投票:0回答:1

我有以下图片:

.

我想只保留黑色文本 0790 并从图片中删除所有文本。 这个 stackoverflow 问题教你如何删除颜色。但是,我需要保留颜色,而不是删除它。

python opencv colors
1个回答
14
投票

一种可能的解决方案是将图像转换为

CMYK
颜色空间并提取
K
(关键 - 黑色)通道,对其进行阈值处理并应用一些形态学来清理二值图像。

OpenCV没有实现从

BGR
CMYK
的转换,所以我们必须手动计算
K
通道。代码如下所示:

# Imports
import cv2
import numpy as np

# Read image
imagePath = "D://opencvImages//"
inputImage = cv2.imread(imagePath + "A6RXi.png")

# Conversion to CMYK (just the K channel):

# Convert to float and divide by 255:
imgFloat = inputImage.astype(np.float) / 255.

# Calculate channel K:
kChannel = 1 - np.max(imgFloat, axis=2)

# Convert back to uint 8:
kChannel = (255 * kChannel).astype(np.uint8)

这是K(黑色)通道:

现在,使用固定值对图像进行阈值处理。在这种情况下,我将阈值设置为

190

# Threshold image:
binaryThresh = 190
_, binaryImage = cv2.threshold(kChannel, binaryThresh, 255, cv2.THRESH_BINARY)

这是二值图像:

它有点吵,但如果我们实现区域过滤器,我们可以去除较小的斑点。该函数在本文末尾定义。让我们应用 minimum 值为

100
的过滤器。所有小于此的斑点都将被删除:

# Filter small blobs:
minArea = 100
binaryImage = areaFilter(minArea, binaryImage)

这是过滤后的图像:

酷。让我们用封闭过滤器改善斑点的形态:

# Use a little bit of morphology to clean the mask:
# Set kernel (structuring element) size:
kernelSize = 3
# Set morph operation iterations:
opIterations = 2
# Get the structuring element:
morphKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernelSize, kernelSize))
# Perform closing:
binaryImage = cv2.morphologyEx(binaryImage, cv2.MORPH_CLOSE, morphKernel, None, None, opIterations, cv2.BORDER_REFLECT101)

cv2.imshow("binaryImage [closed]", binaryImage)
cv2.waitKey(0)

这是最终结果:

这就是

areaFilter
功能。它接收一个最小区域和一个二值图像,它返回没有小斑点的图像:

def areaFilter(minArea, inputImage):
    # Perform an area filter on the binary blobs:
    componentsNumber, labeledImage, componentStats, componentCentroids = \
        cv2.connectedComponentsWithStats(inputImage, connectivity=4)

    # Get the indices/labels of the remaining components based on the area stat
    # (skip the background component at index 0)
    remainingComponentLabels = [i for i in range(1, componentsNumber) if componentStats[i][4] >= minArea]

    # Filter the labeled pixels based on the remaining labels,
    # assign pixel intensity to 255 (uint8) for the remaining pixels
    filteredImage = np.where(np.isin(labeledImage, remainingComponentLabels) == True, 255, 0).astype('uint8')

    return filteredImage
© www.soinside.com 2019 - 2024. All rights reserved.