简体   繁体   中英

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. 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]); . 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(); method. If I want to find the SAD of the green, would 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.

you can get all the colors of an image into Color[][] like this

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;

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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