用JavaFX写一个处理高光谱图片的软件,并附上详细代码
时间: 2024-02-25 10:53:08 浏览: 105
好的,以下是一个简单的JavaFX程序,它可以打开和显示高光谱图像,并且可以应用一个简单的滤波器来增强图像。
```java
import java.io.File;
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
public class HyperspectralImageProcessor extends Application {
private Image originalImage;
private Image filteredImage;
@Override
public void start(Stage primaryStage) {
// Create the UI components
ImageView imageView = new ImageView();
Button openButton = new Button("Open");
Button filterButton = new Button("Filter");
Slider filterSlider = new Slider(0, 10, 5);
// Set up the filter slider
filterSlider.setMajorTickUnit(1);
filterSlider.setMinorTickCount(0);
filterSlider.setSnapToTicks(true);
filterSlider.setShowTickMarks(true);
filterSlider.setShowTickLabels(true);
filterSlider.setPadding(new Insets(10));
filterSlider.setDisable(true);
// Set up the open button
openButton.setOnAction(e -> {
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
File selectedFile = fileChooser.showOpenDialog(primaryStage);
if (selectedFile != null) {
try {
originalImage = new Image(selectedFile.toURI().toURL().toString());
imageView.setImage(originalImage);
filterSlider.setDisable(false);
} catch (Exception ex) {
System.out.println("Error opening file: " + ex.getMessage());
}
}
});
// Set up the filter button
filterButton.setOnAction(e -> {
if (originalImage != null) {
int w = (int) originalImage.getWidth();
int h = (int) originalImage.getHeight();
int[] pixels = new int[w * h];
originalImage.getPixelReader().getPixels(0, 0, w, h, javafx.scene.image.PixelFormat.getIntArgbInstance(), pixels, 0, w);
int filterSize = (int) Math.round(filterSlider.getValue());
int[] filteredPixels = filterImage(pixels, w, h, filterSize);
filteredImage = createImageFromArray(filteredPixels, w, h);
imageView.setImage(filteredImage);
}
});
// Set up the layout
BorderPane root = new BorderPane();
HBox toolbar = new HBox();
toolbar.setSpacing(10);
toolbar.setPadding(new Insets(10));
toolbar.getChildren().addAll(openButton, filterButton, filterSlider);
root.setTop(toolbar);
root.setCenter(imageView);
// Set up the stage
Scene scene = new Scene(root, 800, 600);
primaryStage.setTitle("Hyperspectral Image Processor");
primaryStage.setScene(scene);
primaryStage.show();
}
private int[] filterImage(int[] pixels, int w, int h, int filterSize) {
int[] filteredPixels = new int[w * h];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int r = 0, g = 0, b = 0, count = 0;
for (int dy = -filterSize; dy <= filterSize; dy++) {
int yy = y + dy;
if (yy >= 0 && yy < h) {
for (int dx = -filterSize; dx <= filterSize; dx++) {
int xx = x + dx;
if (xx >= 0 && xx < w) {
int pixel = pixels[yy * w + xx];
r += (pixel >> 16) & 0xff;
g += (pixel >> 8) & 0xff;
b += pixel & 0xff;
count++;
}
}
}
}
r /= count;
g /= count;
b /= count;
filteredPixels[y * w + x] = (0xff << 24) | (r << 16) | (g << 8) | b;
}
}
return filteredPixels;
}
private Image createImageFromArray(int[] pixels, int w, int h) {
javafx.scene.image.PixelFormat format = javafx.scene.image.PixelFormat.getIntArgbInstance();
javafx.scene.image.WritablePixelFormat<java.nio.ByteBuffer> byteFormat = format.getByteBgraInstance();
java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(w * h * 4);
byte[] bytePixels = new byte[pixels.length * 4];
for (int i = 0; i < pixels.length; i++) {
int pixel = pixels[i];
bytePixels[i * 4] = (byte) ((pixel >> 16) & 0xff);
bytePixels[i * 4 + 1] = (byte) ((pixel >> 8) & 0xff);
bytePixels[i * 4 + 2] = (byte) (pixel & 0xff);
bytePixels[i * 4 + 3] = (byte) ((pixel >> 24) & 0xff);
}
buffer.put(bytePixels);
return new Image(buffer, w, h);
}
public static void main(String[] args) {
launch(args);
}
}
```
在这个程序中,我们使用JavaFX的ImageView组件来显示图像,并使用FileChooser组件来打开图像文件。我们还使用Slider组件来控制图像滤波器的大小,并使用Button组件来应用图像滤波器。
在filterImage函数中,我们使用一个简单的均值滤波器来增强图像。该滤波器将每个像素的颜色值替换为其周围像素的平均值。滤波器大小由filterSize参数控制。
在createImageFromArray函数中,我们将一个int数组转换为JavaFX的Image对象。我们首先将int数组转换为字节数组,然后使用JavaFX的PixelFormat和WritablePixelFormat类将字节数组转换为JavaFX的ByteBuffer对象。最后,我们使用JavaFX的Image构造函数创建一个新的Image对象。
请注意,这只是一个简单的示例程序,实际的高光谱图像处理程序可能需要更复杂的算法和更复杂的用户界面。
阅读全文