【发布时间】:2013-01-23 09:25:25
【问题描述】:
我正在尝试通过Derek Bradley 使用Android 实现自适应阈值算法。但它一直在返回黑色像素。这是我的代码 sn-p。请建议我该怎么做。提前致谢。
public static Bitmap GrayscaleToBin(Bitmap bm2)
{
Bitmap bm;
bm=bm2.copy(Config.ARGB_8888, true);
final int width = bm.getWidth();
final int height = bm.getHeight();
int[] pixels;
pixels = new int[width*height];
bm.getPixels(pixels,0,width,0,0,width,height);
//Bradley AdaptiveThrsholdging
int []intImg= new int[width*height];
int sum=0;
for(int i=0;i<width;++i){
sum=0;
for(int j=0;j<height;++j)
{
sum=sum+pixels[i+j*width];
if(i==0){intImg[i+j*width]=sum;}
else
{
intImg[i+j*width]= intImg[i-1+j*width]+sum;
}
}
}
int x1,x2,y1,y2=0,count=0;
int s=width >> 3;
int t=15;
for(int i=0;i<width;++i)
{
for(int j=0;j<height;++j)
{
x1=i-s/2;
x2=i+s/2;
y1=j-s/2;
y2=j+s/2;
if (x1 <0) x1 = 0;
if (x2>= width) x2 = width-1;
if (y1 <0) y1 = 0;
if (y2>= height) y2 = height-1;
count = (x2-x1) * (y2-y1);
sum = intImg [y2 * width + x2] -
intImg [y1 * width + x2] -
intImg [y2 * width + x1] +
intImg [y1 * width + x1];
if((pixels[i+j*width]*count)<=(sum*(100-t)/100))
{
pixels[i+j*width]=0;
}
else
{
pixels[i+j*width]=255;
}
}
}
/*---------------------------------------------------------------------------*/
bm.setPixels(pixels,0,width,0,0,width,height);
// Log.d("cdsfss","afterloop");
return bm;
}
【问题讨论】:
-
那篇论文涉及二值化,所以我对您的代码的第一个问题是函数的名称“RGBtoGrayScale”。这立即引发了另一个问题,该论文不关心 RGB 图像,但您的代码似乎并不能确保输入是灰度图像。最后,您正在使用一个公式来查找与纸上(或任何处理完整图像的文本)上的不同的
s x s平均值。你检查过这些问题吗? -
抱歉,函数名写得不正确。我已经改变了它。我的输入位图图像是灰度图像,是使用 R = G = B = (int)(0.299 * R + 0.587 * G + 0.114 * B) 从 RGB 转换得到灰度后得到的;同样,它不适用于 s=width/8;
标签: android image-processing threshold