1

我有一个基于 OCR 的 iPhone 应用程序,它接收灰度图像并将它们阈值设置为黑白以查找文本(使用 opencv)。这适用于在白色背景上带有黑色文本的图像。当图像是黑色背景上的白色文本时,我遇到了自动切换到反向阈值的问题。是否有一种广泛使用的算法来检查图像以确定它是否是深色背景上的浅色文本,反之亦然?谁能推荐一个干净的工作方法?请记住,我只使用来自 iPhone 相机的灰度图像。

非常感谢。

4

2 回答 2

1

由于此时我正在处理灰度IplImage,因此我无法计算黑色或白色像素,但必须计算高于给定“亮度”阈值的像素数。我只是使用了边框像素,因为这更便宜,并且仍然为我提供了足够的信息来做出合理的决定。

IplImage *image;
int sum = 0; // Number of light pixels
int threshold = 135; // Light/Dark intensity threshold

/* Count number of light pixels at border of image. Must convert to unsigned char type to make range 0-255. */
// Check every other pixel of top and bottom
for (int i=0; i<(image->width); i+=2) {
    if ((unsigned char)image->imageData[i] >= threshold) { // Check top
        sum++;
    }
    if ((unsigned char)image->imageData[(image->width)*(image->height)
                       - image->width + i] >= threshold) { // Check bottom
        sum++;
    }
}

//Check every other pixel of left and right Sides
for (int i=0; i<(image->height); i+=2) {
    if ((unsigned char)image->imageData[i*(image->width)] >= threshold) { // Check left
        sum++;
    }
    if ((unsigned char)image->imageData[i*(image->width) + (image->width) - 1] >= threshold) { // Check right
        sum++;
    }
}

// If more than half of the border pixels are light, use inverse threshold to find dark characters
if (sum > ((image->width/2) + (image->height/2))) {
    // Use inverse binary threshold because background is light
}
else {
    // Use standard binary threshold because background is dark
}
于 2012-04-09T16:52:30.223 回答
0

我会检查每个像素并检查它是亮还是暗。如果暗像素的数量大于亮像素,则必须反转图片。

在这里查看如何确定亮度: Detect black pixel in image iOS

这就是如何绘制一个 UIImage 倒置:

[imyImage drawInRect:theImageRect blendMode:kCGBlendModeDifference alpha:1.0];
于 2012-03-28T10:14:49.663 回答