gpt4 book ai didi

java通过索引+1和-1迭代图像错误模型

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:10 26 4
gpt4 key购买 nike

我的任务是编写一个脚本来“侵 eclipse ”二值图像,即黑白照片。

这意味着我必须区分白色和黑色的部分,在 RGB 尺度上,黑色为 0,1 为白色。

像素在水平(i 索引)和垂直(j 索引)上迭代。为了使像素被视为特定颜色,它的直接邻居必须是该颜色,即 i+1i-1 以及 j+1j-1

我尝试编码如下:

public static BufferedImage getErodedImage(BufferedImage image) {
BufferedImage target = copyImage(image);
for (int i = 0; i < image.getRaster().getWidth(); i++) {
for (int j = 0; j < image.getRaster().getHeight(); j++) {
if (i + 1)||(i - 1) == 0 {
i = 0
}
else{
i = 1
}

}
if (j + 1)||(j - 1) == 0 {
j = 0
}
else{
j = 1
}
}
}

我的尝试非常Pythonic,并返回很多错误,我猜测我可能需要另一个for循环来迭代-1、0和+1值。然后设置第i个像素的值。

最佳答案

你的代码中有很多语法错误,尝试使用这段代码:

public static BufferedImage getErodedImage(BufferedImage image) {
BufferedImage target = copyImage(image);
for (int i = 0; i < image.getRaster().getWidth(); i++) {
for (int j = 0; j < image.getRaster().getHeight(); j++) {
if (i + 1 == 0 || i - 1 == 0) { // not if (i + 1)||(i - 1) == 0
i = 0; // ;
}
else{
i = 1; // ;
}
if (j + 1 == 0 ||j - 1 == 0) { // not if (j + 1)||(j - 1) == 0 and this "if" should be in "for (int j..."
j = 0; // ;
}
else{
j = 1; // ;
}
}
}
}

关于java通过索引+1和-1迭代图像错误模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34318328/

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