Ask Your Question
1

how to set automatic threshold of image using opencv

asked 2013-12-05 01:24:03 -0600

prachis gravatar image

I am working on Image to Text Conversion so I want to binarize the image by setting automatic threshold because I can not use a static threshold as every image varies from other

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
5

answered 2013-12-05 02:48:33 -0600

Raul Andrew gravatar image

Try looking into adaptive thresholding

void adaptiveThreshold( InputArray src, OutputArray dst,
                        double maxValue, int adaptiveMethod,
                        int thresholdType, int blockSize, double C );

This code shows you the difference between thresholding and adaptive thresholding:

#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>


using namespace std;
using namespace cv;

int main(int argc, char *argv[]) {
    Mat imgInput;
    Mat imgThreshold;
    Mat imgAdThreshold;

    imgInput = imread("yourImage.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    imgThreshold = Mat(imgInput.cols, imgInput.rows, IPL_DEPTH_8U, 1);
    imgAdThreshold = Mat(imgInput.cols, imgInput.rows, IPL_DEPTH_8U, 1);

    namedWindow("Input", CV_WINDOW_AUTOSIZE);
    namedWindow("Threshold", CV_WINDOW_AUTOSIZE);
    namedWindow("Adaptive Threshold", CV_WINDOW_AUTOSIZE);

    //Thresholding
    threshold(imgInput, imgThreshold,100,255,CV_THRESH_BINARY);
    //Adaptive Thresholding
    adaptiveThreshold(imgInput, imgAdThreshold, 255, 
                          CV_ADAPTIVE_THRESH_GAUSSIAN_C, 
                          CV_THRESH_BINARY, 75, 25);

    imshow("Input", imgInput);
    imshow("Threshold", imgThreshold);
    imshow("Adaptive Threshold", imgAdThreshold);
    waitKey(0);
    return 0;
}
edit flag offensive delete link more

Comments

Thanks. But the result is not clear for comparatively dark background.Also will opencv can be burnt in DSK kits like TMS320C6748

prachis gravatar imageprachis ( 2013-12-08 00:15:25 -0600 )edit
3

answered 2013-12-06 15:07:54 -0600

updated 2013-12-07 00:10:17 -0600

One of the best for thresholding the text paper for OCR is sauvola method.I have put the source code here.

Edit:

pixel = ( pixel > mean * ( 1 + k * ( standard_deviation / r - 1 ) ) ) ? object : background

Parameter 1: is the k value. The default value is 0.5. Any other number than 0 will change the default value.

Parameter 2: is the r value. The default value is 128. Any other number than 0 will change the default value

Sauvola, J & Pietaksinen, M (2000), "Adaptive Document Image Binarization", Pattern Recognition 33(2): 225-236,

edit flag offensive delete link more

Comments

1

oh, cool. can you shed some light on what's it doing different from, the adaptive method ?

berak gravatar imageberak ( 2013-12-06 15:40:30 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-12-05 01:24:03 -0600

Seen: 9,662 times

Last updated: Dec 07 '13