将short []转换为灰度图像 [英] Convert short[] into a grayscale image

查看:130
本文介绍了将short []转换为灰度图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用aparapi写一个Buddhabrot分形发生器。我让它的OpenCL部分工作,产生一个代表每个像素的单维数组。我将最终图像的尺寸作为最终的int,并编写了代码来获取该数组中任意点的索引。我想将此保存为图像,我正在尝试使用带有TYPE_USHORT_GRAY的BufferedImage。这是我到目前为止所拥有的:

I am writing a Buddhabrot fractal generator using aparapi. I got the OpenCL part of it to work, resulting in a single-dimension array that represents each pixel. I have the dimensions of the final image as final ints, and have written code to get the index of arbitrary points in that array. I want to save this as an image and I'm trying to use BufferedImage with TYPE_USHORT_GRAY. Here's what I have so far:

    BufferedImage image=new BufferedImage(VERTICAL_PIXELS, HORIZONTAL_PIXELS, BufferedImage.TYPE_USHORT_GRAY);
    for(int i=0; i<VERTICAL_PIXELS; i++)
        for(int k=0; k<HORIZONTAL_PIXELS; k++)
            image.setRGB(k, i, normalized[getArrayIndex(k,i,HORIZONTAL_PIXELS)]);

问题是,我不知道将RGB设置为什么。我需要做什么?

The problem is, I don't know what to set the RGB as. What do I need to do?

推荐答案

这里的问题是 setRGB()想要一个0xRRGGBB颜色值。无论数据存储的是什么,BufferedImage都喜欢假装图像是RGB。你实际上可以得到内部 DataBufferShort (带有 getTile(0,0).getDataBuffer()),但它可能很难弄清楚它是如何布局的。

The problem here is that setRGB() wants an 0xRRGGBB color value. BufferedImage likes to pretend that the image is RGB, no matter what the data is stored as. You can actually get at the internal DataBufferShort (with getTile(0, 0).getDataBuffer()), but it can be tricky to figure out how it is laid out.

如果你的像素已经在短[] ,一个更简单的解决方案可能是将它们复制到 int [] 而不是将它堵塞到 MemoryImageSource

If you already have your pixels in a short[], a simpler solution might be to copy them into an int[] instead an jam it into a MemoryImageSource:

int[] buffer = /* pixels */;

ColorModel model = new ComponentColorModel(
   ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] { 16 }, 
   false, true, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);

Image image = Toolkit.getDefaultToolkit().createImage(
   new MemoryImageSource(VERTICAL_PIXELS, HORIZONTAL_PIXELS, 
                         model, buffer, 0, VERTICAL_PIXELS));

这种方法的优点是可以控制基础像素阵列。您可以对该数组进行更改,并在 MemoryImageSource 上调用 newPixels(),它会更新为实时更新。除了灰度外,它还为您提供了定义自己的调色板的全部功能:

The advantage of this approach is that you control the underlying pixel array. You could make changes to that array and call newPixels() on your MemoryImageSource, and it would update live. It also gives you complete power to define your own palette other than grayscale:

int[] cmap = new int[65536];
for(int i = 0; i < 65536; ++i) {

    cmap[i] = (((i % 10000) * 256 / 10000) << 16) 
            | (((i % 20000) * 256 / 20000) << 8)
            | (((i % 40000) * 256 / 40000) << 0);
}
ColorModel model = new IndexColorModel(16, 65536, cmap, 0, false, -1, DataBuffer.TYPE_USHORT);

如果您只是想在屏幕上显示图像,这种方法可以正常工作:

This approach works fine if you just want to display the image on the screen:

JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

但是,如果你想把它写出来一个文件并保留一个短的像素格式(比如加载到Matlab中)然后你运气不好。您可以做的最好的事情是将其绘制成 BufferedImage 并使用 ImageIO 保存,这将保存为RGB。

However, if you wanted to write it out to a file and preserve the one-short-per-pixel format (say, to load into Matlab) then you're out of luck. The best you can do is to paint it into a BufferedImage and save that with ImageIO, which will save as RGB.

如果你最后肯定需要 BufferedImage ,另一种方法是自己应用调色板,计算RGB值,然后将它们复制到图像中:

If you definitely need a BufferedImage at the end, another approach is to apply the color palette yourself, calculate the RGB values, and then copy them into the image:

short[] data = /* your data */;
int[] cmap = /* as above */;
int[] rgb = new int[data.length];

for(int i = i; i < rgb.length; ++i) {
   rgb[i] = cmap[data[i]];
}

BufferedImage image = new BufferedImage(
   VERTICAL_PIXELS, HORIZONTAL_PIXELS, 
   BufferedImage.TYPE_INT_RGB);

image.setRGB(0, 0, VERTICAL_PIXELS, HORIZONTAL_PIXELS,
   pixels, 0, VERTICAL_PIXELS);

这篇关于将short []转换为灰度图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆