Help, I'm going mad with this

A

azrael

Meanwhile I tried about 5 different implementations of the otsu
threshold algorithm. I'll go mad.
Please help me. I don't know what to do. I even tried to implement it
from c and java, but no way. nothing.
I've been reading about 5 ppt presentations and 4 pdf's and I failed.
Can someone look at this code.
If needed I can paste 3 other codes.

Testing list:


def otsu(hi):
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
li=[]
for k in range(i+1,j+1):
q1=sum(hi[i:k])
q2=sum(hi[k:j+1])
v1=var(hi[i:k])
v2=var(hi[k:j+1])
a=q1*v1 + q2*v2
li.append(a)

m,n=li[0],i
for k in range(len(li)):
if li[k]>m:
m=li[k]
n=k



if __name__=="__main__":
otsu(histogram)
histogram= [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]


Well, this list is a example list that i have exported. for this list
the result the threshold should be at 218.

Thanks
 
P

Peter Otten

azrael said:
Help, I'm going mad with this

Is "Otsu threshold algorithm" to boring as a subject?
Meanwhile I tried about 5 different implementations of the otsu
threshold algorithm. I'll go mad.
Please help me. I don't know what to do. I even tried to implement it
from c and java, but no way. nothing.
I've been reading about 5 ppt presentations and 4 pdf's and I failed.
Can someone look at this code.
If needed I can paste 3 other codes.

Hmm, you just learned that quality is more important than quantity then.
Choose the text that seems most accessible while still complete and try to
understand it.
if __name__=="__main__":
otsu(histogram)

You are using a parameter before defining it. It seems you have not yet
groked the basics of cut and past then.
histogram= [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]


Well, this list is a example list that i have exported. for this list
the result the threshold should be at 218.

If you need the result and don't want to bother with understanding the
algorithm -- I found an implementation in C via Google Codesearch

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

It gives the desired result if you use the line that is commented out

// orig: sb ...

instead of its current replacement. Translation to Python should be
straightforward, but you can ask on c.l.py if you run into problems.

Peter
 
A

azrael

I didn't found this one. Looks interesting and complex because I ran
away from C to Python because of the developing speed and pointers.
Thanks mate, I'll try it.

Other C implementations I found were much shorter, at least this is
going to be a little challenge. :D
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top