【问题标题】:How to get pixel values of an image and store it into an array?如何获取图像的像素值并将其存储到数组中?
【发布时间】:2014-11-12 23:54:09
【问题描述】:

我想尝试在图像中查找图像。对于我的“查找”方法,我想拍摄一张图像并使用它来扫描和比较绝对差的总和与更大的图像。

最小的 SAD 将是我用来扫描的确切图像。我的想法是将两个图像的每个像素值放入两个单独的数组中,并通过Math.abs(image1[i][j]-image2[i][j]); 进行比较。我唯一的问题是我不知道如何将每个像素值放入一个数组中。

另外,如果我只想比较图片中的绿色。我看到 Pixel 类有一个getGreen(); 方法。如果我想找到绿色的 SAD,Math.abs(image1.getGreen()-image2.getGreen()); 会起作用吗?我计划在每个图像的每一列和每一行中运行 2 个嵌套循环,然后找到绿色值的 SAD。

【问题讨论】:

    标签: java image pixel


    【解决方案1】:

    您可以像这样将图像的所有颜色放入 Color[][] 中

    BufferedImage paintImage = null;
        try {
             paintImage = ImageIO.read(new File ("C://Users/promo.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Color[][] cols = new Color[paintImage.getWidth()][paintImage.getHeight()];
        for(int z = 0;z < paintImage.getWidth();z++){
            for(int a = 0;a < paintImage.getHeight();a++){
                int color = paintImage.getRGB(z, a);
    
                int  red = (color & 0x00ff0000) >> 16;
                int  green = (color & 0x0000ff00) >> 8;
                int  blue = color & 0x000000ff;
                int alpha = (color>>24) & 0xff;
                Color col = new Color(red,green,blue,alpha);
                cols[z][a] = col;
    
            }
        }
    

    【讨论】:

    • 如果我只想将红色值输入到图片数组中该怎么办?另外,为什么是 Color[][] 而不是 int [][]?我只想要一个红色值的数组,Color[][] 会这样做吗?
    • color有getRed()方法,也有getBlue()、getGreen()、getAlpha()方法返回int,每个Color代表图像每个像素的像素数据
    • 哦,所以对于我的代码...我有图片模板=新图片(a);。我应该使用颜色模板 = new Color(a);反而?这样我就可以运行 template.getRed() 并将所有红色值存储在 int [][] 中,因为本质上,我想找到两个 int 数组的所有 SAD 以找到最低的 SAD。
    • 你可以做任何你想做的事,你知道所有的细节,是的,就像你在我发布的循环中看到的那样,它有 int 的颜色,甚至颜色的所有不同部分,使用你想要的喜欢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    相关资源
    最近更新 更多