How to get threshold value from histogram?

前端 未结 3 1454
無奈伤痛
無奈伤痛 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:34

    If you say that the background is dark (black) and the foreground is lighter, then I recommend to use the YUV color space (or any other YXX like YCrCb, etc.), because the first component of such color spaces is luminance (or lightning).

    light channel

    So after the Y channel is extracted (via the extractChennel function) we need to analyse the histogram of this channel (image):

    histogram

    See the first (left) hump? It represents dark areas (the background in your situation) on your image. So our aim now is to find a segment (on abscissa, it's red part in the image) that contains this hump. Obviously the left point of this segment is zero. The right point is the first point where:

    • the (local) maximum of histogram is from the left of the point
    • the value of histogram is less than some small epsilon (you can set it to 10)

    I drew a green vertical line to show the location of the right point of the segment in this histogram.

    And that's it! This right point of the segment is the needed threshold. Here's the result (epsilon is 10 and the calculated threshold is 50):

    result

    I think that it's not a problem for you to delete the noise in the image above.

提交回复
热议问题