简体   繁体   English

如何在java中将图像转换为棕褐色

[英]How to convert image to sepia in java

Im trying to take an image and make it sepia.我正在尝试拍摄图像并使其变成棕褐色。 For some reason the code isn't working even though I got it from the book I bought to teach myself java.出于某种原因,即使我从我买来教自己 java 的书中得到了它,代码也不起作用。 Is there something Im missing or something thats in an incorrect spot?是否有我遗漏的东西或在不正确的地方的东西? Every question Ive seen like this has some crazy formulas and symbols that I dont even know yet.我见过的每个问题都有一些我什至不知道的疯狂公式和符号。 Can someone help me?有人能帮我吗?

import images.APImage;
import images.Pixel;

public class Sepia{

public static void main(String[] args){

    APImage image = new APImage("DogStockPhoto.jpg");
    //image.draw();
    //converts to grayscale
    for(Pixel p: image){
        int red = p.getRed();
        int green = p.getGreen();
        int blue = p.getBlue();
        int average = (red + green + blue)/3;
        p.setRed(average);
        p.setGreen(average);
        p.setBlue(average);

        //converts to sepia
        if(red < 63){
            red = (int)(red * 1.1);
            blue = (int)(blue * 0.9);
        }else if(red < 192){
            red = (int)(red * 1.15);
            blue = (int)(blue * 0.85);
        }else{
            red = Math.min((int)(red * 1.08), 255);
            blue = (int)(blue * 0.93);
        }
    }

    image.draw();       

}
}

Also, after I try to draw the Image, its still in grayscale.另外,在我尝试绘制图像后,它仍然处于灰度状态。

The comment is correct.评论是正确的。 You average out the pixel values making them gray.您将像素值平均化,使它们变灰。 Then you calculate the sepia value for the pixel, but never assign it back to the pixel.然后您计算像素的棕褐色值,但永远不会将其分配回像素。 Therefore your image remains in grayscale.因此,您的图像保持灰度。

After the if...else block:在 if...else 块之后:

p.setRed(red);
p.setBlue(blue);

OK!好的! So thank you two for the feedback.所以谢谢两位的反馈。 I figured out a couple things.我想出了几件事。 I should have been telling it to replace the gray with the new "red" and "blue".我应该告诉它用新的“红色”和“蓝色”替换灰色。 However, that doesn't work all in the same place, meaning you need two for statements, one for grayscale and the other for sepia.但是,这并不能在同一个地方工作,这意味着您需要两个用于语句,一个用于灰度,另一个用于棕褐色。 After figuring that out it all went well!搞清楚之后,一切顺利! Here's my finished code:这是我完成的代码:

import images.APImage;
import images.Pixel;

public class Sepia{

public static void main(String[] args){

    APImage image = new APImage("DogStockPhoto.jpg");
    //image.draw();

    //converts to grayscale
    for(Pixel p: image){
        int red = p.getRed();
        int green = p.getGreen();
        int blue = p.getBlue();
        int average = (red + green + blue)/3;
        p.setRed(average);
        p.setGreen(average);
        p.setBlue(average);
    }

    //converts to sepia
    for(Pixel p: image){
        int red = p.getRed();
        int blue = p.getBlue();
        if(red < 63){
            red = (int)(red * 1.1);
            blue = (int)(blue * 0.9);
        }else if(red < 192){
            red = (int)(red * 1.15);
            blue = (int)(blue * 0.85);
        }else{
            red = Math.min((int)(red * 1.08), 255);
            blue = (int)(blue * 0.93);
        }
        p.setRed(red);
        p.setBlue(blue);
    }

    image.draw();       
}
}

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

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