java中像素表示方式 标准表示转化为非标准表示
在编写java时,获得了一张图片的各个像素标准表示方式,即每个像素用三个0.0-1.0之间的小数表示,现在想根据各个像素的这三个标准的表示方式数组来得到其对应的RGB表示。
即输入是一张图片的标准表示方式,输出时一张图片的RGB表示方式,(我想获得这张图片像素值中的R部分),大致知道使用java中的colormodel.java来实现,但是具体方法不知道。
求教!
输入:float[w*h*3]
输出:int[w*h]
代码如下:我分别获得了一张图片的R色域(0-255,可以用来检验),和标准表示方式的数组,想知道如何用标准表示的数组推出R色域。
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.Raster;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import
java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
@SuppressWarnings("restriction")
public class Problem {
private float[] pelsIS;//存放用InputStream,变成float的像素,这个是我要用的,和结果无关
private float[] pelsISD;//存放用InputStream,直接调用getElem的函数获得的像素
private float[] pelsBI;//存放用BufferedImage,直接调用红色色域的函数获得的像素
public void decodeImageIS(InputStream is) throws
IOException {
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(is);
Raster raster = decoder.decodeAsRaster();
DataBuffer dataBuffer = raster.getDataBuffer();
int w = raster.getWidth();
int h = raster.getHeight();
pelsIS = new float[w * h *3];
pelsISD = new float[w * h *3];
if (raster.getNumBands() == 4) {
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
int Y = dataBuffer.getElem(i * w * 4 + j * 4);
int Cr = dataBuffer.getElem(i * w * 4 + j * 4 + 1);
int Cb = dataBuffer.getElem(i * w * 4 + j * 4 + 2);
int c = (int)Math.min(Math.max(Y + 1.772 * (Cb - 128), 0), 255);
int m = (int)Math.min(Math.max(Y - 0.34414 * (Cb - 128) - 0.71414 * (Cr - 128), 0), 255);
int y = (int)Math.min(Math.max(Y + 1.402 * (Cr - 128), 0), 255);
int k = dataBuffer.getElem(i * w * 4 + j * 4 + 3);
pelsIS[i* w * 3 + j * 3] = (float) ((k - (c * k >> 8)) / 255.0);
pelsIS[i* w * 3 + j * 3 + 1] = (float) ((k - (m * k >> 8)) / 255.0);
pelsIS[i* w * 3 + j * 3 + 2] = (float) ((k - (y * k >> 8)) / 255.0);
}
} else if (raster.getNumBands() == 3) {
System.out.println("用jpg格式的图片,会进入这里,NumBands=3");
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
int Y = dataBuffer.getElem(i * w * 3 + j * 3);
int Cr = dataBuffer.getElem(i * w * 3 + j * 3 + 1);
int Cb = dataBuffer.getElem(i * w * 3 + j * 3 + 2);
pelsISD[i* w * 3 + j * 3] = Y;
pelsISD[i* w * 3 + j * 3 + 1] = Cr;
pelsISD[i* w * 3 + j * 3 + 2] = Cb;
pelsIS[i* w * 3 + j * 3] = (float) ((Math.min(Math.max(Y + 1.772f * (Cb - 128), 0), 255)) / 255.0);
pelsIS[i* w * 3 + j * 3 + 1] = (float) ((Math.min(Math.max(Y - 0.34414f * (Cb - 128) - 0.71414f * (Cr - 128), 0), 255)) / 255.0);
pelsIS[i* w * 3 + j * 3 + 2] = (float) ((Math.min(Math.max(Y + 1.402f * (Cr - 128), 0), 255)) / 255.0);
}
} else if (raster.getNumBands() == 1) {
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) {
int Y = dataBuffer.getElem(i * w + j);
pelsIS[i* w * 3 + j * 3] = (float) (Y / 255.0);
pelsIS[i* w * 3 + j * 3 + 1] = (float) (Y / 255.0);
pelsIS[i* w * 3 + j * 3 + 2] = (float) (Y / 255.0);
}
}
this.writeArrayToFile(pelsIS, "c:\\pelsIS.txt");
this.writeArrayToFile(pelsISD, "c:\\pelsISD.txt");
}
public void decodeImageBI(String path){