I am working on a project to find all straws in the images. My thought is to use the Thresholding, Morphology, and ConnectedComponents to get the contours, see the following code and attached figures. It is easy to try some parameters and make them work for a specific image, but I have thousands of images, it is hard to find a set of universal parameters. I did try the Histogram Equalization/CLAHE, but it is still hard to find a model to fit all. Are there any other techniques or trained models I can try? Any suggestions or comments are welcomed! Thanks!
def segment(im_bgr, k=3, open_i=2, dilate_i=8): im_gray = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2GRAY) # Reomve noises and get the forground ret, thresh = cv2.threshold( im_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU ) kernel = np.ones((k, k), np.uint8) opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=open_i) sure_bg = cv2.dilate(opening, kernel, iterations=dilate_i) msk_interest = ~sure_bg # Marker labelling ret, im_marker = cv2.connectedComponents(msk_interest) # Draw bounding boxes im_marker = np.uint8(im_marker) contours, h = cv2.findContours(im_marker, 1, 2) for contour in contours: rect = cv2.minAreaRect(contour) box = cv.boxPoints(rect).astype(int) cv2.drawContours(im_draw, [box], -1, (0, 255, 0), 1)