Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to set a flexible threshold value?

for c in contours0: #contours0 contains all the initial contours that includes noise
    [x, y, w, h] = cv2.boundingRect(c)
    current_area = w*h
    temp = temp + current_area
ave_area = temp/len(contours0)
threshold_area = (0.4 *(ave_area))
for c in contours0: 
    [x,y,w,h] = cv2.boundingRect(c)
    if ((w*h) >= threshold_area ):
        contours.append(c) #contours contains the final contours without noise contours

The code above finds the average area of contours0 and compares it later on to the threshold area. Anything lesser than the threshold area is not included in contours. But this doesn't work all the time. The main objective of the code is to delete unnecessary background noise from contours0.

There are other parts in my code that often needs manual fine tuning of the threshold value. They all differ when the size of the image is smaller/bigger.

How to set a flexible threshold value?

for c in contours0: #contours0 contains all the initial contours that includes noise
    [x, y, w, h] = cv2.boundingRect(c)
    current_area = w*h
    temp = temp + current_area
ave_area = temp/len(contours0)
threshold_area = (0.4 *(ave_area))
for c in contours0: 
    [x,y,w,h] = cv2.boundingRect(c)
    if ((w*h) >= threshold_area ):
        contours.append(c) #contours contains the final contours without noise contours

The code above finds the average area of contours0 and compares it later on to the threshold area. Anything lesser than the threshold area is not included in contours. But this doesn't work all the time. The main objective of the code is to delete unnecessary background noise from contours0.

There are other parts in my code that often needs manual fine tuning of the threshold value. They all differ when the size of the image is smaller/bigger.