我的任务是编写一个脚本来“侵 eclipse ”二值图像,即黑白照片。
这意味着我必须区分白色和黑色的部分,在 RGB 尺度上,黑色为 0,1 为白色。
像素在水平(i
索引)和垂直(j
索引)上迭代。为了使像素被视为特定颜色,它的直接邻居必须是该颜色,即 i+1
和 i-1
以及 j+1
和 j-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/