ocal variable maxBrightness defined in an enclosing scope must be final. fix this.…
- User
ocal variable maxBrightness defined in an enclosing scope must be final. fix this. private void contrast2Image() { if (image != null) { BufferedImage contrastStretchedImage = linearContrastStretch_H2(image); setImageIcon(contrastStretchedImage); saveImage(contrastStretchedImage);
// Создаем гистограмму исходного изображения 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]; // Переменные для минимального и максимального значений яркости final int[] minBrightness = {255}; int maxBrightness = 0; 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); // Находим минимальное и максимальное значение яркости if (brightness < minBrightness[0]) { minBrightness[0] = brightness; } if (brightness > maxBrightness) { maxBrightness = brightness; } 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 newBrightness = (int) (255.0 * (i - minBrightness[0]) / (maxBrightness - minBrightness[0])); int height = (int) (300.0 * contrastStretchedHistogram[newBrightness] / 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
- User
- Assistant
The issue seems to be caused by trying to access an index of the
contrastStretchedHistogramarray which is greater than 255. You can fix this by adding a check to make sure that you're not accessing an index greater than or equal to 256.Here's the updated code:
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 newBrightness = (int) (255.0 * (i - minBrightness[0]) / (maxBrightness[0] - minBrightness[0])); if (newBrightness < 0) { newBrightness = 0; } else if (newBrightness >= 256) { newBrightness = 255; } int height = (int) (300.0 * contrastStretchedHistogram[newBrightness] / max); g.fillRect(i, 300 - height, 1, height); } } };This code adds a check for the newBrightness value, making sure it's always in the range of 0 to 255.