You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android平台BoofCV的threshold()方法返回全黑图像问题

Android中BoofCV阈值化后输出全黑问题解决

问题重现

执行以下代码进行图像处理时,阈值化后的输出图像为全黑:

Bitmap blurredBitmap = GaussianBlur.with(sApp.getApplicationContext()).radius(5).render(R.drawable.test_img_divider);
GrayU8 imageGray = ConvertBitmap.bitmapToGray(blurredBitmap,(GrayU8) null, null);
GrayU8 imageThreshold = GThresholdImageOps.threshold(imageGray, null, GThresholdImageOps.computeEntropy(imageGray, 0, 255), true);

Bitmap output = Bitmap.createBitmap(imageThreshold.width, imageThreshold.height, Bitmap.Config.ARGB_8888);
ConvertBitmap.grayToBitmap(imageThreshold, output, null); // output is totally black.

各阶段图像:

  • 原始图片:原始图片
  • 高斯模糊后图片:高斯模糊后图片
  • 转灰度图后图片:转灰度图后图片
  • 阈值化后全黑输出:阈值化后全黑输出

问题原因

核心问题出在GThresholdImageOps.threshold方法的最后一个参数invert:

  • 当invert = true时,BoofCV会反转阈值化结果:高于阈值的像素设为0(黑色),低于阈值的设为255(白色)。
  • 你的灰度图整体偏亮,通过computeEntropy计算出的熵阈值会非常低(接近0),导致几乎所有像素都高于阈值,反转后全部变为0,最终输出全黑图片。

解决方案

方案1:关闭反转功能

将阈值化方法的最后一个参数改为false,这样高于阈值的像素会保留为白色,低于的为黑色:

GrayU8 imageThreshold = GThresholdImageOps.threshold(imageGray, null, GThresholdImageOps.computeEntropy(imageGray, 0, 255), false);

方案2:手动指定阈值测试

如果熵阈值不适合当前场景,可以尝试手动设置阈值(比如针对偏亮的图,设置150-200左右的阈值),验证效果:

// 手动设置阈值为180,关闭反转
GrayU8 imageThreshold = GThresholdImageOps.threshold(imageGray, null, 180, false);

方案3:换用其他阈值计算方式

如果熵阈值法不适用当前图像的亮度分布,可以尝试Otsu阈值法,它更适合对比度明显的场景:

int otsuThreshold = GThresholdImageOps.computeOtsu(imageGray, 0, 255);
GrayU8 imageThreshold = GThresholdImageOps.threshold(imageGray, null, otsuThreshold, false);

内容的提问来源于stack exchange,提问作者Sher Mi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 07:45:49