# Help for Otsu implementation from C

azrael jura.grozni at gmail.com
Wed Sep 19 21:33:08 CEST 2007

```I know. The translation is not so important to me, because this is
going to just a little function that is working in the last step of
the whole proces. The whole implementation wont be published and has
just the meaning to implement a first alpha version of a face
recognition system. It is nearly finished, and this is the only
function I am missing. For this purpose I need it to work "EXACTLY".

I know that it doesnt look pythonic. After I know that it works
correctly, I am going to write a total Rewrite for the Beta Version.

On Sep 19, 1:55 pm, Peter Otten <__pete... at web.de> wrote:
> azrael wrote:
> > Can somone look at this
> > def otsu(hi):
> >     fmax=-1.0
> >     border=len(hi)
> >     for i in range(border):
> >         if hi[i]!=0:break
> >     for j in range(border-1,0-1,-1):
> >         if hi[j] != 0:break
> >     s = sum([k*hi[k] for k in range(border)]) n = sum(hi) #
> >     product(im.size)
> >     n1=n2=csum=0.0
> >     for k in range(i,j):
> >         n1 += hi[k]
> >         n2  = n - n1
> >         csum+= k * hi[k]
> >         m1 = csum/ n1
> >         m2 = (s - csum)/n2
>
> As I said in my previous post, try replacing this line
>
> >         sb = n1 * n2 * (m2 - m1)
>
> with the original
>
>           sb = n1 * n2 * (m1 - m2) * (m1 - m2)
>
> that has been commented out.
>
> >         if sb > fmax:
> >             fmax = sb
> >             V=k+1
> >     print V
> > I try to implement it from C from this location.
>
> Personally, I would start with a literal translation and not try to
> improve it until it works.
>
> Peter

```