Dear Aneesh,

How are you even opening that image file on Matlab? I get this error:
>> img = imread('control.mvd2 - XY point 3-1.tif', 1);
Error using rtifc
TIFF library error: 'TIFFReadDirectory: Incorrect count for
"ColorMap"; tag ignored.'.

Error in readtif (line 49)
[X, map, details] = rtifc(args);

Error in imread (line 434)
    [X, map] = feval(fmt_s.read, filename, extraArgs{:});


In contrast, julia's Images opens it without trouble :-). But one important 
thing to note: your file is a multitiff (there are 11 images in it), and in 
Julia the image is of size 512x512x11. If in Matlab you're just processing one 
512x512 image, that fact alone will explain an 11-fold speed difference.

That said, there was a problem with Julia's `hist` function; an attempt at 
making it better is found here: https://github.com/JuliaLang/julia/pull/8952. 
With that pull request, running hist as
   e, counts = hist(in_img, 65536)
is rather faster than what you're citing. (You also don't need all those 
`convert` and `g[:]` calls, and in fact they slow you down.)

--Tim

On Saturday, November 08, 2014 07:34:24 AM Aneesh Sathe wrote:
> Hello!
> 
> I use matlab for image processing and was thinking of moving some of my
> code to julia. But i found that the matlab function graythresh() was
> missing. So, i wrote my own version.
> 
> Since I've been using julia for less than a week i doubt my code and/or
> algorithm is the best. So any input is much appreciated
> 
> I've used the Images package to read my tif file. and then passed it to the
> graythresh() function pasted below
> 
> Comparisons for a test image:
> Matlab: Elapsed time is 0.013389 seconds. thresh value: 0.1294
> Julia: elapsed time: 1.035032847 seconds thresh value: 0.13013116644890854
> 
> Clearly, the julia code is slower but seems to give the right answer.
> 
> where i think it might be slowing down:
> 1) the function is not a .jl file
> 2) the conversion to Array from the image format is inefficient
> 3) i'm doing loops wrong
> 4) the float values are slowing things down
> 
> i tried using the profiler but couldn't quite get it to work for either
> just @profile graythresh()   or   @profile graythresh(img)
> 
> Cheers!
> -Aneesh
> 
> reference link for Otsu's thresholding:
> http://www.labbookpages.co.uk/software/imgProc/otsuThreshold.html
> 
> 
> Attached files:
> 1) the test code for Otsu's method with the graythresh function(remember to
> run that cell before the rest)
> 2) the tif image i used for testing
> 
> The relevant part of code from the ijulia notebook
> 
> function graythresh(in_img)
> 
>     g=convert(Array,in_img);
>     step=65536;
> 
>     #get histogram values
>     e,counts=hist(g[:],linrange(minfinite(g[:]),maxfinite(g[:]),step))
> 
>    im_len=length(in_img);
> 
>     tot_sum=0;
>     for t=1:step-1
>         tot_sum=tot_sum+t*counts[t];
>     end
> 
>     sumB = 0;
>     wB = 0;
>     wF = 0;
>     varMax = 0;
>     threshold = 0;
>     for t = 1:step-1
> 
>         wB=wB + counts[t]; #weight background
> 
>         wF=im_len - wB; #weight foreground
> 
>         sumB=sumB+t*counts[t];
> 
>         mB = sumB / wB;            #Mean Background
>         mF = (tot_sum - sumB) / wF;    # Mean Foreground
> 
>         varBetween = wB * wF * (mB - mF)^2;
>         # Check if new maximum found
>         if (varBetween > varMax)
>             varMax = varBetween;
>             threshold = t;
>         end
>     end
>     return e[threshold]
> end

Reply via email to