简体   繁体   English

为灰度图像添加颜色

[英]Adding colour to greyscale image

Im looking to add colour to a greyscale image; 我希望为灰度图像添加颜色; it doesnt have to be an accurate colour representation but adding a colour to a different shade of grey, this is just to identify different areas of interest within an image. 它不一定是准确的颜色表示,而是为不同的灰色阴影添加颜色,这只是为了识别图像中不同的感兴趣区域。 Eg areas of vegetation are likely to be of a similar shade of grey, by adding a colour to this range of values it should be clear which areas are vegetation, which are of water, etc. 例如,植被区域可能具有相似的灰色阴影,通过在该值范围内添加颜色,应该清楚哪些区域是植被,哪些是水等。

I have the code for getting colours from an image and storing them as a colour object but this doesnt seem to give a way to modify the values. 我有从图像中获取颜色并将它们存储为颜色对象的代码,但这似乎没有提供修改值的方法。 Eg if the grey vale is less than 85, colour red, if between 86 and 170 colour green and between 171 and 255 colour blue. 例如,如果灰色谷值小于85,则颜色为红色,如果介于86和170绿色之间,则介于171和255蓝色之间。 I have no idea how this will look but in theory the resulting image should allow a user to identify the different areas. 我不知道这看起来如何,但理论上,结果图像应该允许用户识别不同的区域。

The current code I have for getting pixel value is below. 我获取像素值的当前代码如下。

int total_pixels = (h * w);
Color[] colors = new Color[total_pixels];

for (int x = 0; x < w; x++)
{
  for (int y = 0; y < h; y++)
  {
    colors[i] = new Color(image.getRGB(x, y));
    i++;
  }
}
for (int i = 0; i < total_pixels; i++)
{
  Color c = colors[i];
  int r = c.getRed();
  int g = c.getGreen();
  int b = c.getBlue();
  System.out.println("Red " + r + " | Green " + g + " | Blue " + b);
}

I appreciate any help at all! 我感谢任何帮助! Thanks a lot 非常感谢

You are going to have to choose your own method of converting colours from the greyscale scheme to whatever colour you want. 您将不得不选择自己的方法将颜色从灰度方案转换为您想要的任何颜色。

In the example you've given, you could do something like this. 在你给出的例子中,你可以做这样的事情。

public Color newColorFor(int pixel) {
    Color c = colors[pixel];
    int r = c.getRed();  // Since this is grey, the G and B values should be the same
    if (r < 86) {
        return new Color(r * 3, 0, 0);  // return a red
    } else if (r < 172) {
        return new Color(0, (r - 86) * 3, 0); // return a green
    } else {
        return new Color(0, 0, (r - 172) * 3); // return a blue
    }
}

You may have to play around a bit to get the best algorithm. 您可能需要玩一下才能获得最佳算法。 I suspect that the code above will actually make your image look quite dark and dingy. 我怀疑上面的代码实际上会让你的图像看起来很黑暗。 You'd be better off with lighter colours. 使用较浅的颜色会让你感觉更好。 For example, you might change every 0 in the code above to 255, which will give you shades of yellow, magenta and cyan. 例如,您可以将上面代码中的每个0更改为255,这将为您提供黄色,品红色和青色的阴影。 This is going to be a lot of trial and error. 这将是一个很多的试验和错误。

I recommend you to take a look at Java2D. 我建议你看看Java2D。 It has many classes that can make your life much easier. 它有许多课程可以让你的生活更轻松。 You may end up reinventing the wheel if you ignore it. 如果忽略它,你最终可能会重新发明轮子。

Here is a short showcase of what you can do: 以下是您可以做的简短展示:

    int width = 100;
    int height = 100;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    image.getRGB(x, y);
    Graphics2D g2d = (Graphics2D)image.getGraphics();
    g2d.setColor(Color.GREEN);
    g2d.fillRect(x, y, width, height);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM