转动图像GreyScale

时间:2017-03-05 02:47:01

标签: java

第1类     import java.util.Scanner;

public class Image {

public static void main(String[] args) {
    String filename = "test.ppm";
    TestClass img = new TestClass(filename);

    StdDraw.setCanvasSize(img.getWidth(), img.getHeight());
    StdDraw.setXscale(0, img.getWidth());
    StdDraw.setYscale(0, img.getHeight());

    Scanner keyboard = new Scanner(System.in);

    img.display();

    System.out.println("Type anything then enter");
    keyboard.next();

    TestClass modified = new TestClass(filename);
    modified.removeReds();
    modified.display();
}
}

第2类

import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestClass {
private int width;
private int height;
private Color[][] data;

public TestClass(String filename) {
    Scanner fileIn = null;
    try {
        fileIn = new Scanner(new FileInputStream(filename));
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
        System.out.println("Looked in " +
                System.getProperty("user.dir"));
        System.exit(1);
    }

    // Header information
    String format = fileIn.nextLine();
    width = fileIn.nextInt();
    height = fileIn.nextInt();
    int colorDepth = fileIn.nextInt();

    System.out.println("File: " + filename + " is " + width
            + " x " + height + " pixels");

    // Verify file format
    if (!format.equals("P3") || colorDepth != 255) {
        System.out.println("Unknown format for file");
        System.exit(1);
    }

    // Read in data pixel by pixel
    data = new Color[height][width];
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int red = fileIn.nextInt();
            int green = fileIn.nextInt();
            int blue = fileIn.nextInt();

            data[i][j] = new Color(red, green, blue);
        }
    }
}

public void display() {
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            StdDraw.setPenColor(data[i][j]);
            StdDraw.point(j, height - i - 1);
        }
    }
}

public int getHeight() {
    return height;
}
public int getWidth() {
    return width;
}

public void removeReds() {
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int red = data[i][j].getRed();
            int green = data[i][j].getGreen();
            int blue = data[i][j].getBlue();

            int sum = red+green+blue;
            Color newColor = new Color(sum / 3);



        }
    }
}
}

所以我需要将此图像转换为灰度。我知道你需要平行红绿蓝。我尝试过,但是每当我运行程序时,它只会改变颜色,使它们变暗一点

1 个答案:

答案 0 :(得分:0)

据我所知,您的代码中没有任何地方可以将像素的颜色实际修改为灰度值。您可以使用data[i][j] = newColor方法中的for循环中的行removeRed()执行此操作(应该称为greyScale()方法)。就目前而言,您只需找到灰度颜色,然后将其丢弃,您也可能计算错误。