How to get threshold value from histogram?

前端 未结 3 1418
無奈伤痛
無奈伤痛 2021-02-01 09:08

I\'m writing an Android app in OpenCV to detect blobs. One task is to threshold the image to differentiate the foreground objects from the background (see image).

It wo

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 09:53

    The following is a C++ implementation of Abid's answer that works with OpenCV 3.x:

    // Convert the source image to a 1 channel grayscale:
    Mat gray;
    cvtColor(src, gray, CV_BGR2GRAY);
    // Apply the threshold function with the CV_THRESH_OTSU setting as well
    // You can skip having it return the value, but I include it for showing the
    // results from OTSU
    double thresholdValue = threshold(gray, gray, 0, 255, CV_THRESH_BINARY+CV_THRESH_OTSU);
    // Present the threshold value
    printf("Threshold value: %f\n", thresholdValue);
    

    Running this against the original image, I get the following:

    OpenCV calculated a threshold value of 122 for it, close to the value Abid found in his answer.

    Just to verify, I altered the original image as seen here:

    And produced the following, with a new threshold value of 178:

提交回复
热议问题