Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding bg option to watershed #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

rtobar
Copy link

@rtobar rtobar commented Oct 31, 2018

The work in this pull request comes from the fact that we are currently doing a watershed over slightly big images that need to be filtered first to cancel out some background noise.

To do that we do something like this:

> image[image < 1.5] = 0
> EBImage::watershed(image, tolerance=0)

The problem is that the cost of doing this filtering ourselves is a fair amount of the total cost:

> dim(image)
[1] 2848 2848

> microbenchmark({image[image < 1.5]=0}, times=100)
Unit: milliseconds
                       expr     min      lq     mean   median       uq     max neval
 { image[image < 1.5] = 0 } 62.7114 70.7209 105.7809 88.69216 101.6094 276.556   100

> microbenchmark(EBImage::watershed(image, tolerance = 0))
Unit: milliseconds
                                     expr      min       lq     mean   median       uq      max neval
 EBImage::watershed(image, tolerance = 0) 629.0318 660.6444 687.8925 676.6159 699.1388 865.2657   100

Based on the median and mean values of these measurements we are observing an approximate filtering cost of 10%-15%.

Doing the watershed with the internal filtering (what we are proposing in this pull request) we should be able to do:

> EBImage::watershed(image, tolerance = 0, bg=1.5)

The cost is negligible, as the current watershed algorithm is already filtering positive pixels:

> microbenchmark(EBImage::watershed(image, tolerance = 0, bg=1.5))
Unit: milliseconds
                                          expr      min       lq     mean   median       uq      max neval
 EBImage::watershed(image, tolerance=0, bg=1.5) 581.5359 645.1499 664.3851 660.9021 675.6802 770.4231   100

This option replaces the hardcoded BG macro at the C level, giving users
flexibility on the pixel value threshold to apply when finding pixels.
This reduces the number of pixels to be inspected in images with low
background noises. Tolerance is still defined in absolute terms.

Note that we use the bg value not only to filter out which pixels are to
be considered, but also to populate the frame array. This is important
as it gives a more stable set of values to the sorting routine, yielding
improved speed.

I also took the opportunity of correctly aligning a couple of lines of
code that had tabs in them and therefore were a bit off.

Signed-off-by: Rodrigo Tobar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant