Переделай код, чтобы что в пороге растягивалась, что не в пороге не изменялось. private…
- User
Переделай код, чтобы что в пороге растягивалась, что не в пороге не изменялось. private void brightness1Image() { if (image != null) { int minThreshold = 50; int maxThreshold = 50;
// Create panels for min and max threshold values JPanel minThresholdPanel = new JPanel(new GridLayout(1, 2)); JPanel maxThresholdPanel = new JPanel(new GridLayout(1, 2));
// Create text fields for threshold values JTextField minThresholdField = new JTextField(Integer.toString(minThreshold)); JTextField maxThresholdField = new JTextField(Integer.toString(maxThreshold)); minThresholdPanel.add(new JLabel("Min threshold: ")); maxThresholdPanel.add(new JLabel("Max threshold: ")); minThresholdPanel.add(minThresholdField); maxThresholdPanel.add(maxThresholdField);
// Create window for inputting threshold values int result = JOptionPane.showConfirmDialog(null, new Object[]{minThresholdPanel, maxThresholdPanel}, “Enter Threshold Values”, JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) { // Update threshold values minThreshold = Integer.parseInt(minThresholdField.getText()); maxThreshold = Integer.parseInt(maxThresholdField.getText());
// Create new image for modified image BufferedImage modifiedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
// Create color for black pixels Color black = Color.BLACK;
for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { // Get pixel and extract RGB values int pixel = image.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff;
// Calculate brightness of pixel int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue);
// Check if brightness value falls within threshold range if (brightness >= minThreshold && brightness <= maxThreshold) { Color c = new Color(red, green, blue); modifiedImage.setRGB(i, j, c.getRGB()); } else { modifiedImage.setRGB(i, j, black.getRGB()); } } }
if (modifiedImage != null) { setImageIcon(modifiedImage); saveImage(modifiedImage);
// Create histogram for original image int[] originalHistogram = new int[256]; for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { // Get pixel and extract RGB values int pixel = image.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff;
// Calculate brightness of pixel int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue);
originalHistogram[brightness]++; } }
// Create histogram for modified image int[] modifiedHistogram = new int[256]; for (int i = 0; i < modifiedImage.getWidth(); i++) { for (int j = 0; j < modifiedImage.getHeight(); j++) { // Get pixel and extract RGB values int pixel = modifiedImage.getRGB(i, j); int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = pixel & 0xff;
- Assistant
- User
- Assistant