在 OCR 之后从 android 中的图像中检测字体颜色

Detect font color from image in android after OCR

我有一张图片,我正在使用 tesseract 从中查找文本。 我有 rect(left,top,right,bottom)

形式的文本边界框

我想找到我试图遍历 2D 边界框矩阵的文本的字体颜色,我正在将背景颜色与边界框中的每个颜色像素进行比较。无论我在哪里得到背景颜色以外的颜色,我都会返回字体颜色的颜色。

//rectArrayList is an array list of rect for a line in the image 

for(int i=rectArrayList.get(0).left;i<rectArrayList.get(0).right ;i++){
        for(int j=rectArrayList.get(0).top;j<rectArrayList.get(0).bottom;j++){

            pixel=colorbit.getPixel(i,j);
            R = (pixel & 0xff0000) >> 16; //channel the pixel in RGB values
            G = (pixel & 0xff00) >> 8;
            B = pixel & 0xff;

//backColour is the background colour of the image 
            if(backColour!=Color.rgb(R,G,B)){ 
                return Color.rgb(R,G,B);
            }

        }

假设背景颜色是黄色。但是在遍历矩阵时,我得到了不同的黄色阴影,这是字体颜色后面的背景颜色,而不是字体颜色。所以我得到了错误的字体颜色。

我知道这种技术会失败,因为图像的背景颜色会根据亮度等因素具有不同的相同颜色深浅。

我应该怎么做才能从图像中获得准确的字体颜色?

我可以为您提供许多不同的解决方案。这里有一些简单的开始:

当颜色 1 与颜色 2 不相等时不要停止。增加一些公差。 计算两个 RGB 元组之间的欧氏距离。然后检查距离是否大于某个阈值。 您还可以将 RGB 转换为 Hue 并计算两个 Hue 值之间的绝对差值。

当然还有更复杂更好的解决方案,但考虑到您对图像处理的了解,它们暂时对您没有帮助。