Help for Otsu implementation from C

A

azrael

Can somone look at this

def otsu(hi):
fmax=-1.0
border=len(hi)
for i in range(border):
if hi!=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
sb = n1 * n2 * (m2 - m1)
if sb > fmax:
fmax = sb
V=k+1
print V

I try to implement it from C from this location.

http://www.google.com/codesearch?hl...ocr-0.41.tar.bz2&cs_f=gocr-0.41/src/otsu.c#a0

It gives me till now the closest threshold value but still not the
exact

Testing Histogram given to the function as an atribute is:
hi = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 2, 0, 0, 2, 1, 2, 0, 1, 3, 3, 3, 3, 1, 3, 1, 3, 5,
2, 3, 3, 3, 6, 3, 4, 5, 4, 9, 6, 11, 6, 10, 3, 11, 9, 9, 12, 22, 18,
34, 22, 28, 32, 25, 34, 38, 34, 54, 65, 106, 160, 167, 203, 282, 364,
446, 637, 816, 1022, 1264, 1456, 1646, 1753, 1845, 1922, 2203, 2231,
1973, 2245, 2369, 2349, 2258, 2130, 2066, 1835, 1640, 1554, 1414,
1179, 1024, 974, 938, 838, 785, 756, 803, 921, 952, 865, 722, 625,
608, 547, 498, 412, 438, 408, 413, 415, 339, 366, 330, 320, 293, 315,
368, 411, 434, 500, 531, 538, 552, 665, 811, 869, 998, 1021, 1075,
1080, 1030, 934, 926, 1074, 942, 941, 1014, 1440, 2966, 5301, 2729,
3400, 5563, 13096, 9068, 6045, 2813, 686, 180]

it gives me 221 value but it should give me 218

Thanks in advance
 
P

Peter Otten

azrael said:
Can somone look at this
def otsu(hi):
fmax=-1.0
border=len(hi)
for i in range(border):
if hi!=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
 
A

azrael

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.



azrael said:
Can somone look at this
def otsu(hi):
fmax=-1.0
border=len(hi)
for i in range(border):
if hi!=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
 
A

azrael

Thanks Man, I did it. It works fantastic.


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.

azrael said:
Can somone look at this
def otsu(hi):
fmax=-1.0
border=len(hi)
for i in range(border):
if hi!=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
with the original
sb = n1 * n2 * (m1 - m2) * (m1 - m2)
that has been commented out.
Personally, I would start with a literal translation and not try to
improve it until it works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top