gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
Переделай код, чтобы области, которые нет входят в порог оставались неизменными. При…
- User
Переделай код, чтобы области, которые нет входят в порог оставались неизменными. При вводе порога, менялась гистограмма. private void contrastImage() { if (image != null) { BufferedImage contrastStretchedImage = linearContrastStretch(image);
setImageIcon(contrastStretchedImage); saveImage(contrastStretchedImage); // Получение порогового значения String thresholdString = JOptionPane.showInputDialog(this, "Введите пороговое значение: "); int threshold = Integer.parseInt(thresholdString); // Создаем гистограмму исходного изображения 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]++; } } // Создаем гистограмму преобразованного изображения int[] contrastStretchedHistogram = new int[256]; for (int i = 0; i < contrastStretchedImage.getWidth(); i++) { for (int j = 0; j < contrastStretchedImage.getHeight(); j++) { int pixel = contrastStretchedImage.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); contrastStretchedHistogram[brightness]++; } } // Создаем окно для отображения гистограмм JFrame histogramFrame = new JFrame("Гистограммы"); histogramFrame.setLayout(new GridLayout(2, 2, 10, 10)); // Создаем панель для гистограммы исходного изображения JPanel histogramPanel1 = 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); } } }; histogramPanel1.setPreferredSize(new Dimension(256, 300)); // Создаем панель для гистограммы преобразованного изображения JPanel histogramPanel2 = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int max = Arrays.stream(contrastStretchedHistogram).max().getAsInt(); for (int i = 0; i < 256; i++) { int height = (int) (300.0 * contrastStretchedHistogram[i] / max); g.fillRect(i, 300 - height, 1, height); } } }; histogramPanel2.setPreferredSize(new Dimension(256, 300)); histogramFrame.add(new JLabel("Гистограмма исходного изображения")); histogramFrame.add(new JLabel("Гистограмма преобразованного изображения")); histogramFrame.add(histogramPanel1); histogramFrame.add(histogramPanel2); histogramFrame.pack(); histogramFrame.setLocationRelativeTo(null); histogramFrame.setVisible(true); } } - Assistant