【发布时间】: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<128)...
标签: java if-statement for-loop colors