【问题标题】:Grayscale on Picture using Java使用Java对图片进行灰度化
【发布时间】:2016-07-29 15:48:03
【问题描述】:

我对我正在处理的这段代码迷失了方向。我必须仅使用 java 使图片具有灰度。我有基本代码,但我不知道该放什么不会使整个屏幕变灰。我正在努力工作,但我迷路了,不知道下一步该做什么。目前的方式是,它只需要一个图像经过一个过程,然后制作一个完全相同的新版本,但我需要将新版本设为灰色版本。 访问:https://ask.extension.org/uploads/question/images/attachments/000/037/087/image_300x300%2523.jpg?1406470060我正在使用的树图。

import java.awt.*;                                       //import the awt graphics package
class TrueColors                                                //start of the class
{
 TrueColors()                              //start of the main method
 {       
    Picture pictureObj = new Picture("trunk.jpg");     
    pictureObj.explore();                                           
    int redValue = 0; int greenValue = 0; int blueValue = 0;        

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

    for(int y=0; y < pictureObj.getHeight(); y++)                   
    {
        for(int x = 0; x < pictureObj.getWidth(); x++)              
        {
            targetPixel = pictureObj.getPixel(x,y);                 
            pixelColor = targetPixel.getColor();                    

            redValue = pixelColor.getRed();                         
            greenValue = pixelColor.getGreen();                     
            blueValue = pixelColor.getBlue();                       
            pixelColor = new Color(redValue, greenValue, blueValue);
            targetPixel.setColor(pixelColor);                       
        }//end of the inner for loop
    }//end of the outer for loop

    pictureObj.explore();                                           
    pictureObj.write("NewTrunk.jpg");                  
    pictureObj.show();                                              
 }//end of main method
}//end of class

【问题讨论】:

    标签: java image colors jpeg


    【解决方案1】:

    您需要将 RGB 值更改为全部相等以使其成为灰度。有很多可能的算法。这是一个:

    int gray = (0.2989 * red) + (0.5870 * green) + (0.1140 * blue);
    pixelColor = new Color(gray, gray, gray); 
    

    【讨论】:

    • 当我尝试使用您提供的代码时,它不会编译,因为它说 int 灰度值不兼容。它说不能从 double 转换为 int
    • 另外,当我在将它从 double 变为 int 后尝试运行它时,它说新颜色应该是你放置灰色的红色、绿色和蓝色。
    【解决方案2】:

    我建议你看看这个问题:convert a RGB image to grayscale Image reducing the memory in java

    虽然您的目的不是减少内存,但它应该达到同样的目的。

    --编辑-- 一个简单的灰度算法就是取红色、绿色和蓝色的平均值。与您可以做的其他帖子类似:

    redValue = pixelColor.getRed();                         
    greenValue = pixelColor.getGreen();                     
    blueValue = pixelColor.getBlue();
    greyValue = (int)((redValue + greenValue + blueValue)/3)                  
    pixelColor = new Color(greyValue, greeyValue, greyValue);
    targetPixel.setColor(pixelColor);
    

    【讨论】:

    • 问题在于,我还没有学会如何使用 BufferedImages。我正在学习一门课程,这是我坚持的任务之一。
    • 非常感谢,这解决了我的问题。
    • (R + G + B) / 3 是计算灰度图像颜色的一种非常糟糕的方法。
    • @eldo 你会推荐什么?
    • @Ben Rocco 保亮度转换,使用颜色加权平均 0.2126 r 0.7152 g 0.0722 b 查看灰度转换不同方法的解释和结果:johndcook.com/blog/2009/08/24/…
    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 2012-12-12
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多