gpt4 book ai didi

java - Java简单的边缘检测方法

转载 作者:行者123 更新时间:2023-12-02 15:38:26 24 4
gpt4 key购买 nike

我正在研究一种 Java 方法来进行一些简单的边缘检测。我想获取两个颜色强度的差异,一个在一个像素处,另一个在它正下方的像素处。无论我为该方法设置什么阈值,我正在使用的图片都会被着色为黑色。我不确定我当前的方法是否只是没有计算出我需要的内容,但我不知道应该跟踪什么来找到问题。

这是迄今为止我的方法:

public void edgeDetection(double threshold)
{

Color white = new Color(1,1,1);
Color black = new Color(0,0,0);

Pixel topPixel = null;
Pixel lowerPixel = null;

double topIntensity;
double lowerIntensity;

for(int y = 0; y < this.getHeight()-1; y++){
for(int x = 0; x < this.getWidth(); x++){

topPixel = this.getPixel(x,y);
lowerPixel = this.getPixel(x,y+1);

topIntensity = (topPixel.getRed() + topPixel.getGreen() + topPixel.getBlue()) / 3;
lowerIntensity = (lowerPixel.getRed() + lowerPixel.getGreen() + lowerPixel.getBlue()) / 3;

if(Math.abs(topIntensity - lowerIntensity) < threshold)
topPixel.setColor(white);
else
topPixel.setColor(black);
}
}
}

最佳答案

new Color(1,1,1) 调用 ColorColor(int,int,int) 构造函数,该构造函数的值介于0 和 255。所以你的白色仍然基本上是黑色(嗯,非常深的灰色,但不足以引起注意)。

如果您想使用 Color(float,float,float) 构造函数,则需要浮点文字:

Color white = new Color(1.0f,1.0f,1.0f);

关于java - Java简单的边缘检测方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14765479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com