【问题标题】:converting image from grayscale to color pallete将图像从灰度转换为调色板
【发布时间】:2015-08-31 17:55:52
【问题描述】:

我正在制作一个程序,我必须将灰度图像转换为具有不同颜色的调色板图像。

例如,灰度图片看起来像黑白图像,而最终输出应该是这样的:

我发现我应该使用带有条件语句(&& 或 ||)的 if 语句。但是我很难弄清楚在 if 语句中添加什么来产生这种效果。你可以在我的程序中看到我试图启动它,但是我打印出来的 if 语句没有做任何事情。如果有人可以帮助我,那就太好了!

谢谢

我的代码:

import java.awt.*;
public class ConvertColor
{
    public static void main(String [] args) 
    {
        Picture pictureObj = new Picture("WashingtonMonument.png");     //creates a new Picture object representing the file in the parameter list                 
        pictureObj.explore();                                           //explore the Picture object which is currently the unaltered original image
        int redValue = 0; int greenValue = 0; int blueValue = 0;  int luminance = 0;      //declare and initialize the variables that hold the red, green, and blue values (0-255)

        Pixel pixelSource1 = new Pixel(pictureObj, 0,0);
        Color pixelColor = null;

        for(int y=0; y< pictureObj.getHeight(); y++)
        {
            for(int x=0; x< pictureObj.getWidth(); x++)
            {
                pixelSource1 = pictureObj.getPixel(x,y);
                pixelColor = pixelSource1.getColor();
                redValue = pixelColor.getRed();
                blueValue = pixelColor.getBlue();
                greenValue = pixelColor.getGreen();
                luminance = (int)((redValue * .5));

                pixelSource1.setColor(new Color(luminance, luminance, luminance));
            }
        }

        if(redValue > 50 && blueValue < 190)
            greenValue = (int) .5 * (pixelColor.getGreen());


        pictureObj.write("grayWashingtonMonument.png");
        pictureObj.show();
    }
}

【问题讨论】:

  • 你可以尝试一下吗?您的问题看起来可以为我做这个吗?
  • 还有原始作业文本?我不明白为什么要明确坚持使用“if”和“&& or ||”应该有任何意义......
  • 你想在你的 if 语句中做什么?
  • if 语句将从灰度图像@bhspencer 中生成颜色
  • 如何获得新调色板的颜色?这些只是随机的吗? pixelSource1.setColor(new Color(luminance, luminance, luminance)); 行将像素设置为灰度值。也许您可以根据您的情况在此之前添加一行。 if (redValue&lt;128) ...

标签: java if-statement for-loop colors


【解决方案1】:

要使用从更亮到更暗的值将灰色转换为颜色,这是使用 switch/case 语句的解决方案:

     for(int x=0; x< pictureObj.getWidth(); x++)
        {
            pixelSource1 = pictureObj.getPixel(x,y);
            pixelColor = pixelSource1.getColor();
            redValue = pixelColor.getRed();
            blueValue = pixelColor.getBlue();
            greenValue = pixelColor.getGreen();
            int nr = 0;
            int ng = 0;
            int nb = 0;
            int tempResult = redValue / 21;

            switch (tempResult){
            case 1 : nr = 255; nb = 127;break;
            case 2 : nr = 255; nb = 255;break;
            case 3 : nr = 127; nb = 255;break;
            case 4 : nb = 255; break;
            case 5 : ng = 128; nb = 255;break;
            case 6 : ng = 255; nb = 255;break;
            case 7 : ng = 255; nb = 128;break;
            case 8 : ng = 255; break;
            case 9 : nr = 128; ng = 255;break;
            case 10 : nr = 255; ng = 255;break;
            case 11 : nr = 255; ng = 128;break;
            case 12 : nr = 255;break;
            default : nr = 255; ng = 255; nb = 255; break;}

            pixelSource1.setColor(new Color(nr, ng, nb));
        }

对于 13 种灰度,我们有相应的颜色。

回答告诉我它是否运作良好,因为我无法自己测试它。但逻辑是正确的。

【讨论】:

    猜你喜欢
    • 2016-01-11
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 2015-10-07
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多