scott huang
scott huang

Reputation: 2678

opencv binary threshold with C API: set threshold very small will filter out most pixel

Here is the picture in grayscale mode:

enter image description here

if I apply a Thresholding and set threshold to 0. According to my understanding, the thesholded image will be mostly white. but the result is opposite.

Result is:

enter image description here

I also tried this:

build a image and set all pixel to 255. then apply the 0 threshold thresholding, the returned image is all 255.

The question is: in the picture is mostly zero (black) after apply thresholding.

Here are the code:

IplImage*   g_image = NULL;
IplImage*   g_gray = NULL;
int         g_thresh = 100;
CvMemStorage*   g_storage = NULL;


void on_tracker(int){
    if(g_storage == NULL){
        g_gray = cvCreateImage(cvGetSize(g_image), 8, 1);
        g_storage = cvCreateMemStorage(0);
    }else{
        cvClearMemStorage(g_storage);
    }

    CvSeq* contours = 0;
    cvCvtColor(g_image, g_gray, CV_BGR2GRAY);
    cvNamedWindow("Gray");
    cvShowImage("Gray", g_gray);
    cvThreshold(g_gray, g_gray, g_thresh, 255, CV_THRESH_BINARY);
    cvFindContours(g_gray, g_storage, &contours);
    cvShowImage("Contours", g_gray);
}
int main(int argc, char** argv){
    if( argc !=2 || !(g_image = cvLoadImage(argv[1]))){
        return -1;
    }

    cvNamedWindow("Contours", CV_WINDOW_AUTOSIZE);
    cvCreateTrackbar(
        "Threshold",
        "Contours",
        &g_thresh,
        255,
        on_tracker
    );
    on_tracker(0);
    cvWaitKey();
    return 0;
}

Upvotes: 0

Views: 240

Answers (3)

scott huang
scott huang

Reputation: 2678

Accoring to @Miki's comments. this is caused by C API. I tried the same process with python API. the result is normal: if I do thresholding with 0 threshold, most of pixel will be set to 255.

Upvotes: 0

Itzik Chaimov
Itzik Chaimov

Reputation: 99

The basic Thresholding is to check the pixels value (say from 0 to 255) to be above the Threshold value and to assign to the pixel a value of maximum value (high intensity: black) this called Binary Thresholding. In your case, when setting a value of 0 to the threshold, you actually filtering all your pixels since all of them (the low intensities and the higher intensities) have values above zero (0).

Maybe you would like to make a brighter picture - in this case use Inverted Binary Thresholding: in this case, you will get white picture when value is 0.

Upvotes: 0

Lamar Latrell
Lamar Latrell

Reputation: 1669

Have a read of the different types of thresholding available to you in the documentation.

Starting with a 1D 'image' with a range of values (the black line) and threshold (the blue line):

enter image description here

...we can visualise the outcome of the different modes:


Threshold Binary

enter image description here

enter image description here


Threshold Binary Inverted

enter image description here

enter image description here


Truncate

enter image description here

enter image description here


Threshold to Zero

enter image description here

enter image description here


Threshold to Zero Inverted

enter image description here

enter image description here


Please update your question with your code so we know what mode you're using if this answer doesn't help already ;)

Upvotes: 1

Related Questions