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

标签 java pixel

我的任务是编写一个脚本来“侵 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/

相关文章:

java - 将Java程序的 "System.out.print"输出重定向到txt文件

java - 使用 SimpleDateFormat 获取 UTC 日期

java - 在 for 循环中禁用 JPanel 上的 JComponent : annoying delay

resize - VMR9无窗口的DirectShow插值(像素化)问题

c++ - PixelToaster 库上的鼠标处理

java - 以编程方式获取 TextView 的精确宽度(以像素为单位)(Android)

java - 堆参数对 GC/性能的影响?

java - Spring-Integration 聚合器无法正常工作。

2d - calayer 对象上的自定义绘制内容看起来相当像素化

Java - 阅读有关另一个程序的信息?