c++iphoneopencvimage-processingthreshold

Method For Checking Image Pixel Intensities


I have an OCR based iPhone app that takes in grayscale images and thresholds them to black and white to find the text (using opencv). This works fine for images with black text on a white background. I am having an issue with automatically switching to an inverse threshold when the image is white text on a black background. Is there a widely used algorithm for checking the image to determine if it is light text on a dark background or vice versa? Can anyone recommend a clean working method? Keep in mind, I am only working with the grayscale image from the iPhone camera.

Thanks a lot.


Solution

  • Since I am dealing with a grayscale IplImage at this point, I could not count black or white pixels but had to count the number of pixels above a given "brightness" threshold. I just used the border pixels as this is less expensive and still gives me enough information to make a sound decision.

    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
    }