On Saturday, November 8, 2014 1:52:09 PM UTC-6, Jason Merrill wrote:
>
> Here's a sketch of a different algorithm that I believe converges to the
> same value:
>
> 1. Initialize thresh to the mean value of the image pixels
> 2. Compute the mean of the pixels that are larger than thresh, mplus, and
> smaller than thresh, mminus.
> 3. Set thresh to the mean of mminus and mplus, and then loop back to 1.
> Iterate to a fixed point.
>
> You can implement this version without allocating any extra data
> structures, so it may well be much faster.
>
> Sorry I don't have any references here. Writing from a phone.
>
Here's a little bit of follow up on this algorithm. It turns out it is just
the "standard algorithm" for k-means clustering, specialized to 2 clusters
in 1 dimension:
https://en.wikipedia.org/wiki/K-means_clustering#Standard_algorithm
Here's an implementation:
https://gist.github.com/jwmerrill/179aae6c3d2d4e770614
I wasn't able to benchmark against your version because it fails with
`linrange` has no method matching
linrange(::Gray{UfixedBase{Uint16,16}}, ::Gray{UfixedBase{Uint16,16}},
::Int64)
for me.
This code works on either in_img or data(in_img), but it is much faster on
data(in_img). I'm not totally sure why.
@time graythresh_kmeans(data(in_img))
elapsed time: 0.072612812 seconds (132 bytes allocated)
@time graythresh_kmeans(in_img)
elapsed time: 4.849397962 seconds (3737126436 bytes allocated, 55.69%
gc time)