简体   繁体   English

如何获取图像的像素值并将其存储到数组中?

[英]How to get pixel values of an image and store it into an array?

I want to try finding an image within an image. 我想尝试在图像中查找图像。 For my "find" method, I would like to take an image and use it to scan and compare the sums of absolute differences with the bigger image. 对于我的“查找”方法,我想拍摄一张图像,然后使用它来扫描并比较绝对差异之和与较大图像。

The smallest SAD would be the exact image that I am using to scan. 最小的SAD就是我要扫描的确切图像。 What I am thinking is to put each pixel value of both images into two separate arrays and compare them via Math.abs(image1[i][j]-image2[i][j]); 我在想将两个图像的每个像素值放入两个单独的数组中,并通过Math.abs(image1[i][j]-image2[i][j]); . My only problem is that I do not know how to put each pixel value into an array. 我唯一的问题是我不知道如何将每个像素值放入数组中。

Also, If I only want to compare just the green in the picture. 另外,如果我只想比较图片中的绿色。 I saw that the Pixel class has a getGreen(); 我看到Pixel类有一个getGreen(); method. 方法。 If I want to find the SAD of the green, would Math.abs(image1.getGreen()-image2.getGreen()); 如果我想找到绿色的SAD,可以选择Math.abs(image1.getGreen()-image2.getGreen()); work? 工作? I was planning to have 2 nested loops running through each column and row for each image and just find the SAD of the green value. 我计划在每个图像的每一列和每一行中运行2个嵌套循环,然后找到绿色值的SAD。

you can get all the colors of an image into Color[][] like this 您可以像这样将图像的所有颜色转换为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;

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM