Переделать код, чтобы все, что выше уровня яркости который ввели с клавиатуры,…
- 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); } } - Assistant
private void contrastImage() { if (image != null) { BufferedImage contrastStretchedImage = linearContrastStretch(image);
setImageIcon(contrastStretchedImage); saveImage(contrastStretchedImage); // Получение порогового значения и превращение в 255 все что выше String thresholdString = JOptionPane.showInputDialog(this, "Введите пороговое значение: "); int threshold = Integer.parseInt(thresholdString); if (threshold > 255) { threshold = 255; } // Создаем гистограмму исходного изображения 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); }}
- 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;
BufferedImage contrastStretchedImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); // Проходим по всем пикселям изображения и применяем линейное растяжение контраста 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); // Вычисляем новое значение яркости с помощью линейного растяжения контраста int newBrightness = (int) (a * brightness + b); // Ограничиваем диапазон значений яркости от 0 до 255 newBrightness = Math.max(0, Math.min(255, newBrightness)); // Создаем новый пиксель с измененным значением яркости int newPixel = (255 << 24) | (newBrightness << 16) | (newBrightness << 8) | newBrightness; contrastStretchedImage.setRGB(i, j, newPixel); } } // Возвращаем изображение с примененным линейным растяжением контраста return contrastStretchedImage; } - Assistant
private void contrastImage() { if (image != null) { BufferedImage contrastStretchedImage = linearContrastStretch(image);
setImageIcon(contrastStretchedImage); saveImage(contrastStretchedImage); // Получение порогового значения и превращение в 255 все что выше String thresholdString = JOptionPane.showInputDialog(this, "Введите пороговое значение: "); int threshold = Integer.parseInt(thresholdString); if (threshold > 255) { threshold = 255; } // Меняем яркость пикселей, если она выше порога, на максимальное значение 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) { red = green = blue = 255; } pixel = (255 << 24) | (red << 16) | (green << 8) | blue; contrastStretchedImage.setRGB(i, j, pixel); } } // Создаем окно для отображения гистограмм JFrame histogramFrame = new JFrame("Гистограммы"); histogramFrame.setLayout(new GridLayout(2, 2, 10, 10)); // Создаем гистограмму исходного изображения int[] histogram = new int[256]; for (int i = 0