Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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;
}