opencv - Getting blobs from similar colored contours in grayscale image -


i want select staff lines individual blobs in either of these images:

is possible?

edit: regions want select shown in image (roughly, selected them manually):

it's ok if other blobs getting selected (i can filter them area afterwards).

here's started. i'm working on first image showed. see included scikit-image tag, i'm assuming developing in python. such, i'll using python wrappers opencv this. advised parameters each step of algorithm chose particular first image. you'll have play around parameters other image work.

here did:

  1. read in image , convert grayscale via cv2.cvtcolor
  2. perform adaptive threshold via cv2.adaptivethreshold best segment out shapes background. trains , i'm going call these shapes train here on in.
  3. perform morphological binary closing close gaps resulted thresholding via cv2.morphologyex , used elliptical structuring element
  4. fill in holes. basically, can consider hole area of black pixels surrounded white. common way perform flood filling operation seed pixel starting @ top left of image , inverting results these holes. once have these holes, we'll use mask index image step #3 , set these pixels white. use cv2.floodfill. take note input image such remove upper , lower rows , leftmost , rightmost columns , have initialize output image stores flood filled results before calling function.
  5. find contours in image via cv2.findcontours.
  6. go through each contour , filter out have area less amount. use cv2.contourarea me this.
  7. for contours remaining, fill in contours white cv2.drawcontours

assuming first image called train1.png, here's code!

# import relevant libraries import numpy np import cv2  # read in image , convert grayscale im1 = cv2.imread('train1.png') im1 = cv2.cvtcolor(im1, cv2.color_bgr2gray)  # adaptive threshold thresh = cv2.adaptivethreshold(im1, 255,                             adaptivemethod=cv2.adaptive_thresh_mean_c,                             thresholdtype=cv2.thresh_binary_inv,                             blocksize=21,                             c=2)  # morphology close gaps se = cv2.getstructuringelement(cv2.morph_rect, (15,15)) out = cv2.morphologyex(thresh, cv2.morph_close, se)  # find holes mask = np.zeros_like(im1) cv2.floodfill(out[1:-1,1:-1].copy(), mask, (0,0), 255) mask = (1 - mask).astype('bool')  # fill holes out[mask] = 255  # find contours contours,_ = cv2.findcontours(out.copy(),                               cv2.retr_external,                               cv2.chain_approx_none)  # filter out contours less area area = 50000 filtered_contours = filter(lambda x: cv2.contourarea(x) > area,                            contours)  # draw final contours final = np.zeros_like(im1) cv2.drawcontours(final, filtered_contours, -1, 255, -1)  cv2.imshow('shapes', final) cv2.waitkey(0) cv2.destroyallwindows() cv2.imwrite('train1_final.png', final) 

this image get. isn't perfect, it's more enough started.

enter image description here


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -