我有一个 8 位灰度位图,我需要对其进行一些模式识别。我用 java 创建了第一个测试框架,它工作得很好。
之后,我将所有内容移植到 C++ 中,发现我的模式不再被发现。
经过一番调查,我意识到在 java 代码中存在从 TYPE_BYTE_GRAY 到 TYPE_3BYTE_BGR 的“隐藏”格式更改。
我可以将其分解为以下测试函数:
public static void ConvertFiles(File dir, String format)
{
File[] images = getOrderedFiles(dir, format);
for (int i = 0; i < images.length; i++)
{
try
{
BufferedImage img = ImageIO.read(images[i]);
BufferedImage dst = new BufferedImage(img.getWidth() , img.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
for (int xTarget = 0; xTarget <img.getWidth(); xTarget++)
{
for (int yTarget = 0; yTarget <img.getHeight(); yTarget++)
{
int val = img.getRGB(xTarget, yTarget);
dst.setRGB(xTarget,yTarget, val);
}
}
ImageIO.write(dst, "bmp", new File(correctSlash(images[i].getParent()) + "Convert\\" + images[i].getName()));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
生成的文件似乎“更亮”(抱歉,无法发布图像。我可以根据要求发送它们)。当我在创建新图像时使用 TYPE_BYTE_GRAY 时,没有出现任何变化,因此很明显如何避免 Java 中的效果。
现在的问题是,我更喜欢“明亮”的图像,并且想知道这里发生了什么,以便我可以将其重现为某种图像增强。
提前致谢。
最佳答案
通过与同事深入研究 java 代码 (getRGB()) 发现了它(谢谢 Holger)。
通过查找表对原始灰度值进行转换,生成如下:
l8Tos8 = new byte[256];
float input, output;
// algorithm for linear RGB to nonlinear sRGB conversion
// is from the IEC 61966-2-1 International Standard,
// Colour Management - Default RGB colour space - sRGB,
// First Edition, 1999-10,
// avaiable for order at http://www.iec.ch
for (int i = 0; i <= 255; i++) {
input = ((float) i) / 255.0f;
if (input <= 0.0031308f) {
output = input * 12.92f;
} else {
output = 1.055f * ((float) Math.pow(input, (1.0 / 2.4)))
- 0.055f;
}
l8Tos8[i] = (byte) Math.round(output * 255.0f);
}
所以至少在部分 Gamma 校正中。
我可以在 C++ 端应用它并获得相同的结果。
关于Java BufferedImage 灰度到RGB转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29675337/