1級 import java.util.Scanner;車削圖像GreyScale
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);
}
}
}
}
,所以我需要turnn這個圖像灰度。我知道你需要平平紅綠藍。我嘗試過,但是當我每次運行程序時,它只是改變顏色使它們變得更暗
[Converting RGB to grayscale/intensity]可能的重複(http://stackoverflow.com/questions/687261/converting-rgb-to-grayscale-intensity) – Jeremy
我不認爲我有重複的 – jackjackk1223