0

I have the following function to identify blobs in an image, and remove them if they are under a certain size.

With the for-loop the removal is of course very slow, if there are a lot of blobs, now my question is, is it possible to replace the for-loop?

function clean_regions, input, max_size

  output = input

  tmp = size(input)

  input_labels = LABEL_REGION(input, /ULONG)
  hist = histogram(input_labels, binsize=1, locations=loc, /nan, /l64)
  to_remove = loc(where(hist le max_size))
  result_map = MAKE_ARRAY(tmp[1:2], /ULONG, VALUE=0)

  to_keep = mg_complement(to_remove, n_elements(loc))

  for i=0,n_elements(to_keep)-1 do begin
    result_map[where(input_labels EQ to_keep[i])] = 1
  endfor


  output *= result_map
  output = boolean(output gt 0)

  return, output

END
DavidH
  • 415
  • 4
  • 21
Matty
  • 63
  • 1
  • 9
  • Can you use `ERODE` and `DILATE` for your problem? That can remove blobs of smaller sizes, though the exact shape will matter. There would be no loops, though. Also, have you timed the above on example data? Is it really too slow? How many blobs to keep vs to remove? – mgalloy Apr 28 '17 at 05:02
  • I'm using ERODE and DILATE to get rid of blobs of only a few pixels size, but don't want to get any further than that, as I don't want to change the general shape too much. At the moment there are around <4000 elements to keep, and <10000 to delete. – Matty May 08 '17 at 18:12

0 Answers0