【问题标题】:Java: How do I detect the format and read the RGB values of each pixel from an image?Java:如何检测格式并从图像中读取每个像素的 RGB 值?
【发布时间】:2015-10-06 01:22:22
【问题描述】:

我一直在写一个批量图像处理的程序。图像有多种格式,但都是 JPEG、TIFF 或 PNG。我编写了以下用于存储和访问像素数据的类:

package imageReader;

import misc.NumberFunctions;

/**
 * A class to store pixel data and provide access to some of the pixel's     basic
 * attributes
 * 
 * @author Thomas
 *
 */
public class Pixel {

    /**
     * The X-coordinate of the pixel The left-most column of the image     is x=0
     */
    private int x;

    /**
     * The Y-coordinate of the pixel The top row of the image is y=0
     */
    private int y;

    /**
     * The red value of the pixel (from 0 to 255)
     */
    private int r;

    /**
     * The green value of the pixel (from 0 to 255)
     */
    private int g;

    /**
     * The blue value of the pixel (from 0 to 255)
     */
    private int b;

    /**
     * The highest color value in the pixel, which determine its brightness
     * (from 0 to 255)
     */
    private int value;

    /**
     * Creates an empty pixel object, setting it to black (0,0,0)
     * 
     * @param x
     *            the x-coordinate of the pixel
     * @param y
     *            the y-coordinate of the pixel
     */
    public Pixel(int x, int y) {
        this.x = x;
        this.y = y;
        this.r = 0;
        this.g = 0;
        this.b = 0;
        this.setValue();
    }

    /**
     * Get the x-coordinate of the pixel
     * 
     * @return the x-coordinate
     */
    public int getX() {
        return this.x;
    }

    /**
     * Get the y-coordinate of the pixel
     * 
     * @return the y-coordinate
     */
    public int getY() {
        return this.y;
    }

    /**
     * Get the red value of the pixel
     * 
     * @return the red value (0 - 255)
     */
    public int getRed() {
        return this.r;
    }

    /**
     * Get the green value of the pixel
     * 
     * @return the green value (0 - 255)
     */
    public int getGreen() {
        return this.g;
    }

    /**
     * Get the blue value of the pixel
     * 
     * @return the blue value (0 - 255)
     */
    public int getBlue() {
        return this.b;
    }

    /**
     * Get the brightness of the pixel
     * 
     * @return the value (0 - 255)
     */
    public int getValue() {
        return this.value;
    }

    /**
     * Get the brightness of the pixel
     * 
     * @return the value (0 - 100)
     */
    public double getValue100() {
        double value100 = ((double) this.value) / 2.55;
        return value100;
    }

    /**
     * Check if this Pixel object is set to the same location as another
     * 
     * @param otherPixel
     *            the other pixel
     * @return whether they share the same coordinates or not
     */
    public boolean hasSameLocation(Pixel otherPixel) {
        boolean xMatches = (this.getX() == otherPixel.getX());
        boolean yMatches = (this.getY() == otherPixel.getY());
        return (xMatches && yMatches);
    }

    /**
     * Checks if this pixel has the same color as another pixel
     * 
     * @param otherPixel
     *            the other pixel
     * @return whether they share the same color or not
     */
    public boolean hasSameColor(Pixel otherPixel) {
        if (this.getValue() != otherPixel.getValue())
            return false;
        boolean redMatches = (this.getRed() == otherPixel.getRed());
        boolean greenMatches = (this.getGreen() == otherPixel.getGreen());
        boolean blueMatches = (this.getBlue() == otherPixel.getBlue());
        if (redMatches && greenMatches && blueMatches)
            return true;
        else
            return false;
    }

    /**
     * An internal function for finding the brightness value of the pixel
     */
    private void setValue() {
        this.value = NumberFunctions.getMax(r, g, b);
    }
}

我想导入图像(我已经可以获取所有图像的路径),然后将每个图像的数据记录在 Pixel 对象的二维数组 (Pixel[][]) 中。为此,我需要你们的帮助来做三件事:

  1. 确定图片类型并导入

  2. 找出图像的尺寸

  3. 读取每个像素的 RGB 值

感谢你们能给我的任何帮助。谢谢!

【问题讨论】:

  • 您知道吗,保存这样的图像需要原始图像大小的 6 倍?甚至没有计算每个像素的 Java 对象开销。
  • 您是否故意避免使用ImageIO 和BufferedImage?

标签: java image import pixel


【解决方案1】:

您可以使用此代码导入指定文件名的图像。这可能会引发 IOException,因此请注意这一点。

String path = "Image1.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);

图像类型对此并不重要,ImageIO 会自动检测它。如果你想在阅读后输入使用image.getType()

图片尺寸可取自BufferedImage

int width = image.getWidth();
int height = image.getHeight();

像素可以用image.getRGB(int x, int y) 读取,它返回一个整数颜色。你可以用构造函数Color c = new Color(int rgb)把它转换成java.awt.color然后用c.getRed()c.getGreen()c.getBlue(0)获取频道是一个简单的过程

如果我能给你一些建议,请不要复制数据。无需构建Pixels,只需编写您的方法以使用上述方法获取数据。正如其他人指出的那样,您的实现将占用更多内存。

【讨论】:

    【解决方案2】:

    使用BufferedImage:

    try {
        BufferedImage image = ImageIO.read(new File("monkey.jpg")); // TODO actual file
        int width = image.getWidth();
        int height = image.getHeight();
    
        // TODO do whatever you want with image, for example get an rgb:
        Color color = new Color(image.getRgb(0,0));
    } catch (IOException e) {
        // TODO handle
    }
    

    顺便说一句,这是丑陋的代码:

    if (redMatches && greenMatches && blueMatches)
            return true;
        else
            return false;
    

    替换为:

    return redMatches && greenMatches && blueMatches;
    

    【讨论】:

      猜你喜欢
      • 2019-03-11
      • 2015-03-07
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      相关资源
      最近更新 更多