【发布时间】: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[][]) 中。为此,我需要你们的帮助来做三件事:
确定图片类型并导入
找出图像的尺寸
读取每个像素的 RGB 值
感谢你们能给我的任何帮助。谢谢!
【问题讨论】:
-
您知道吗,保存这样的图像需要原始图像大小的 6 倍?甚至没有计算每个像素的 Java 对象开销。
-
您是否故意避免使用ImageIO 和BufferedImage?