我有一个RGB图像,我想转换二进制图像0-255。
我计算RGB图像中的阈值和灰度图像中大于阈值的像素,我将红色设置为255 =绿色= 255和蓝色= 255,并低于阈值将红色设置为0 =绿色= 0和蓝色= 0
private static int colorToRGB(int alpha, int red, int green, int blue) {
int newPixel = 0;
newPixel += alpha;
newPixel = newPixel << 8;
newPixel += red; newPixel = newPixel << 8;
newPixel += green; newPixel = newPixel << 8;
newPixel += blue;
System.out.println("asd" + newPixel);
return newPixel;
}
如果像素为白色,则newPixel的值为-16777216
如果像素为黑色,则newPixel的值为-1
alpha值是常数255
我在哪里错了,因为我想将像素的值设置为0和255。
BufferedImage类型为TYPE_INT_ARGB
谢谢你的帮忙
最佳答案
老实说,你的问题对我来说没有多大意义。
所以我回答您的问题并作一些假设:
您有一个全局阈值[0-255]
该阈值应应用于灰度值,因此您需要将rgb像素转换为灰度值
该过程应尽可能快
你想用java做
此函数采用rgb颜色和阈值并返回黑色或白色。
public static int treshold(final int sourceColor, final int treshold) {
// green channel is a good approximation of rgb intensity
int green = (sourceColor >> 8) & 0xFF;
if (green < treshold) {
return 0xFF000000;
} else {
return 0xFFFFFFFF;
}
}