我的SWT与数字图像处理总结(3)—SWT如何得到图像某个位置的像素值和相应的RGB的值
SWT中如何得到图像某个位置的像素值和相应的RGB的值
方法一:
通过PaletteData中的三个mask来得到RGB,这里是逐行得到的像素值
这种方式是最严谨的方式,对于非索引图像来说
ImageData ideaImageData = img.getImageData();
// The pixel data of the image.
byte[] pixelData = ideaImageData.data;
int redMask = ideaImageData.palette.redMask;
int blueMask = ideaImageData.palette.blueMask;
int greenMask = ideaImageData.palette.greenMask;
int h = 0;
int pixelValue =0;
int[] lineData = new int[ideaImageData.width];
for (int y = 0; y < ideaImageData.height; y++) {
// 首先是得到每行的像素值,保存到int数组lineData中
ideaImageData.getPixels(0, y, ideaImageData.width, lineData, 0);
for (int x = 0; x < lineData.length; x++) {
//得到像素值
pixelValue = lineData[x];
//根据像素值得到RGB的值
int r = pixelValue & redMask;
int g = (pixelValue & greenMask) >> 8;
int b = (pixelValue & blueMask) >> 16;
//根据RGB的值得到亮度(或者intensity)
double temp = (double) (0.3 * r + 0.59 * g + 0.11 * b);
h = (int) (temp) + ((temp - (int) (temp)) > 0.5 ? 1 : 0);
h = h < 0 ? 0 : h;
h = h > 255 ? 255 : h;
histArray[h]++;
}
}
// The pixel data of the image.
byte[] pixelData = ideaImageData.data;
int redMask = ideaImageData.palette.redMask;
int blueMask = ideaImageData.palette.blueMask;
int greenMask = ideaImageData.palette.greenMask;
int h = 0;
int pixelValue =0;
int[] lineData = new int[ideaImageData.width];
for (int y = 0; y < ideaImageData.height; y++) {
// 首先是得到每行的像素值,保存到int数组lineData中
ideaImageData.getPixels(0, y, ideaImageData.width, lineData, 0);
for (int x = 0; x < lineData.length; x++) {
//得到像素值
pixelValue = lineData[x];
//根据像素值得到RGB的值
int r = pixelValue & redMask;
int g = (pixelValue & greenMask) >> 8;
int b = (pixelValue & blueMask) >> 16;
//根据RGB的值得到亮度(或者intensity)
double temp = (double) (0.3 * r + 0.59 * g + 0.11 * b);
h = (int) (temp) + ((temp - (int) (temp)) > 0.5 ? 1 : 0);
h = h < 0 ? 0 : h;
h = h > 255 ? 255 : h;
histArray[h]++;
}
}
方法二:
这里是直接得到的,通过偏移量offset从ImageData的data中得到像素值,bingdedaoRGB分量以及Brightness
ImageData ida = img.getImageData();
byte[] data2 = new byte[ida.data.length];
int offset = 0;
int k = 0;
int r, g, b, gray;
for (int i = 0; i < ida.height; i++) {
for (int j = 0; j < ida.width; j++) {
k = offset + j * 3;
b = ida.data[k] & 0xff;
g = ida.data[k + 1] & 0xff;
r = ida.data[k + 2] & 0xff;
gray = (int) (0.11 * b + 0.59 * g + 0.3 * r);
data2[k] = data2[k + 1] = data2[k + 2] = (byte) gray;
}
offset += ida.bytesPerLine;
}
ImageData ida2 = new ImageData(ida.width, ida.height, ida.depth, ida.palette, ida.scanlinePad, data2);
imgdst = new Image(img.getDevice(), ida2);
byte[] data2 = new byte[ida.data.length];
int offset = 0;
int k = 0;
int r, g, b, gray;
for (int i = 0; i < ida.height; i++) {
for (int j = 0; j < ida.width; j++) {
k = offset + j * 3;
b = ida.data[k] & 0xff;
g = ida.data[k + 1] & 0xff;
r = ida.data[k + 2] & 0xff;
gray = (int) (0.11 * b + 0.59 * g + 0.3 * r);
data2[k] = data2[k + 1] = data2[k + 2] = (byte) gray;
}
offset += ida.bytesPerLine;
}
ImageData ida2 = new ImageData(ida.width, ida.height, ida.depth, ida.palette, ida.scanlinePad, data2);
imgdst = new Image(img.getDevice(), ida2);
方式三:
根据颜色值得到亮度值,也包含了通过像素值得到RGB分量,并得到Brightness
/**
* 根据颜色值(实际上是像素值)得到亮度
*/
public static int getBrightness(int color) {
int r = (color & 0x00ff0000) >> 16;
int g = (color & 0x0000ff00) >> 8;
int b = (color & 0x000000ff);
int y = Math.round(0.3f * r + 0.59f * g + 0.11f * b);
y = y < 0 ? 0 : y;
y = y > 255 ? 255 : y;
return y;
}
* 根据颜色值(实际上是像素值)得到亮度
*/
public static int getBrightness(int color) {
int r = (color & 0x00ff0000) >> 16;
int g = (color & 0x0000ff00) >> 8;
int b = (color & 0x000000ff);
int y = Math.round(0.3f * r + 0.59f * g + 0.11f * b);
y = y < 0 ? 0 : y;
y = y > 255 ? 255 : y;
return y;
}