【问题标题】:How to read and extract image pixels in java and compare如何在java中读取和提取图像像素并进行比较
【发布时间】:2013-03-14 19:29:16
【问题描述】:

我想读取 RGB 图像并想提取图像像素。然后我想比较每个像素以检查图像其他部分中的任何匹配像素。如果该像素与原始像素匹配,则应在 java 中将匹配的像素替换为红色和黄色。

我从 javaforums 和图像处理网站搜索了很多。我仍然没有完美的解决方案。

给出一些像素提取器和像素匹配器示例以进一步进行。

【问题讨论】:

标签: java image-processing image-segmentation


【解决方案1】:

这个 Marvin algorithm 完全符合您的要求。

【讨论】:

    【解决方案2】:

    下面的 getRGBA 方法会在图像 img 的 (x, y) 位置提取 RGBA 数组:

    private final int ALPHA = 24;
    private final int RED = 16;
    private final int GREEN = 8;
    private final int BLUE = 0;
    
    public int[] getRGBA(BufferedImage img, int x, int y)
    {   
        int[] color = new int[4];
        color[0]=getColor(img, x,y,RED);
        color[1]=getColor(img, x,y,GREEN);
        color[2]=getColor(img, x,y,BLUE);
        color[3]=getColor(img, x,y,ALPHA);
        return color;
    }
    
    public int getColor(int x, int y, int color)
    {       
        int value=img.getRGBA(x, y) >> color & 0xff;
        return value;
    }
    

    像素匹配器?也许您只是想运行一个循环..考虑到您将 (0,0) 像素作为原始像素,您可以执行以下操作:

        int[] originalPixel =  getRGBA(img,0,0);
        for (int i=0;i<img.getWidth();i++)
        {
            for (int j=0;j<img.getHeight();j++)
            {
               int[] color1 =  getRGBA(img,i,j);
               if (originalPixel[0] == color1[0] && originalPixel[1] == color1[1] && originalPixel[2] == color1[2] && originalPixel[3] == color1[3]) {
                   img.setRGB(i, j,Color.red.getRGB());
               } 
               else {
                   img.setRGB(i, j,Color.yellow.getRGB());
               }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多