Переделать код, чтобы все, что выше уровня яркости который ввели с клавиатуры,…
- User
Переделать код, чтобы все, что выше уровня яркости который ввели с клавиатуры, превращалось в 255. } 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); if (brightness < threshold) { // Преобразуем яркость пикселя в 0, если она не входит в порог brightness = 0; } else { // Растягиваем яркость пикселя до максимального значения brightness = (int) (255.0 * (brightness - threshold) / (255 - threshold)); } 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); } } } private BufferedImage linearContrastStretch(BufferedImage image) { // Проходим по всем пикселям изображения и находим минимальное и максимальное значение яркости int minBrightness = 255; int maxBrightness = 0; 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); if (brightness < minBrightness) { minBrightness = brightness; } if (brightness > maxBrightness) { maxBrightness = brightness; } } } // Вычисляем коэффициенты a и b для линейного растяжения контраста double a = 255.0 / (maxBrightness - minBrightness); double b = -minBrightness * a;
- Assistant