переделай код, чтобы при яркостном срезе изображения можно выделить области изображения с яркостью из определенного интервала. При этом остальным областям можно присвоить черный цвет и оставить неизменными . private void brightness1Image() {
if (image != null) {
int minBrightness = 50;
int maxBrightness = 50;

// Create panels for min and max brightness thresholds
JPanel minBrightnessPanel = new JPanel(new GridLayout(1, 2));
JPanel maxBrightnessPanel = new JPanel(new GridLayout(1, 2));

// Create text fields for threshold values
JTextField minBrightnessField = new JTextField(Integer.toString(minBrightness));
JTextField maxBrightnessField = new JTextField(Integer.toString(maxBrightness));
minBrightnessPanel.add(new JLabel("Min threshold: "));
maxBrightnessPanel.add(new JLabel("Max threshold: "));
minBrightnessPanel.add(minBrightnessField);
maxBrightnessPanel.add(maxBrightnessField);

// Create window for inputting thresholds
int result = JOptionPane.showConfirmDialog(null, new Object[]{minBrightnessPanel, maxBrightnessPanel},
“Enter Threshold Values”, JOptionPane.OK_CANCEL_OPTION);

if (result == JOptionPane.OK_OPTION) {
// Update threshold values
minBrightness = Integer.parseInt(minBrightnessField.getText());
maxBrightness = Integer.parseInt(maxBrightnessField.getText());

BufferedImage brightImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
int pixel = image.getRGB(i, j);
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = pixel & 0xff;
int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue);
Color c;
if (brightness >= minBrightness && brightness <= maxBrightness) {
c = new Color(red, green, blue);
} else {
c = Color.BLACK;
}
brightImage.setRGB(i, j, c.getRGB());
}
}
if (brightImage != null) {
setImageIcon(brightImage);
saveImage(brightImage);

// Create histogram for original image
int[] histogram = new int[256];
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
int pixel = image.getRGB(i, j);
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = pixel & 0xff;
int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue);
histogram[brightness]++;
}
}

// Create histogram for bright image
int[] brightHistogram = new int[256];
for (int i = 0; i < brightImage.getWidth(); i++) {
for (int j = 0; j < brightImage.getHeight(); j++) {
int pixel = brightImage.getRGB(i, j);
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = pixel & 0xff;
int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue);
brightHistogram[brightness]++;
}
}

// Create window for displaying histograms
JFrame histogramFrame = new JFrame(“Histograms”);
histogramFrame.setLayout(new GridLayout(2, 2, 10, 10));

// Panel for original image histogram
JPanel histogramPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int max = Arrays.stream(histogram).max().getAsInt();
for (int i = 0; i < 256; i++) {
int height = (int) (300.0 * histogram[i] / max);
g.fillRect(i, 300 - height, 1, height);
}
}
};
histogramPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
histogramPanel.setPreferredSize(new Dimension(256, 300));
histogramFrame.add(new JLabel(“Original Image”));
histogramFrame.add(histogramPanel);

// Panel for bright image histogram
JPanel brightHistogramPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int max = Arrays.stream(brightHistogram).max().getAsInt();
for (int i = 0; i < 256; i++) {
int height = (int) (300.0 * brightHistogram[i] / max);
g.fillRect(i, 300 - height, 1, height);
}
}
};
brightHistogramPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
brightHistogramPanel.setPreferredSize(new Dimension(256, 300));
histogramFrame.add(new JLabel(“Brightness Adjusted Image”));
histogramFrame.add(brightHistogramPanel);

histogramFrame.pack();
histogramFrame.setLocationRelativeTo(null);
histogramFrame.setVisible(true);
}
}
}
}