该代码为imageJ marco code 将8bit灰度图片文件导入后可用此代码算出adaptive threshold
height=getHeight(); width=getWidth(); histo=newArray(256); cutpre=0; cutnow=0; for(i=0;i<width;i++){ for (ii=0;ii<height;ii++){ v=getPixel(i,ii); histo[v]+=1; cutnow+=v; } } cutnow=cutnow/width/height; while(cutnow != cutpre){ cutpre=cutnow; up=0; down=0; upc=0; downc=0; for(i=0;i<256;i++){ if (i<cutpre){ down+=i*histo[i]; downc+=histo[i];} else{ up+=i*histo[i]; upc+=histo[i];} } cutnow=up/upc/2+down/downc/2; } print(cutnow);
文章来源: https://blog.csdn.net/qq_30117591/article/details/89737787