How to get threshold value from histogram?

前端 未结 3 1453
無奈伤痛
無奈伤痛 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条回答
  •  旧时难觅i
    2021-02-01 09:49

    If all your images are like this, or can be brought to this style, i think cv2.THRESHOLD_OTSU, ie otsu's tresholding algorithm is a good shot.

    Below is a sample using Python in command terminal :

    >>> import cv2
    >>> import numpy as np
    >>> img2 = cv2.imread('D:\Abid_Rahman_K\work_space\sofeggs.jpg',0)
    
    >>> ret,thresh = cv2.threshold(img2,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    
    >>> ret
    122.0
    

    ret is the threshold value which is automatically calculated. We just pass '0' as threshold value for this.

    I got 124 in GIMP ( which is comparable to result we got). And it also removes the noise. See result below:

    enter image description here

提交回复
热议问题