Переделать код, чтобы пилообразное контрастирование с одной пилой в вверх. Добавить два…
- User
Переделать код, чтобы пилообразное контрастирование с одной пилой в вверх. Добавить два порога, которые я буду вводить сам. И гистограмма для исходного и преобразованного изображения. // Гистограмма для sawtoottImage() private void sawtoottImage() { if (image != null) { BufferedImage[] sawtoothContrastImages = new BufferedImage[4]; sawtoothContrastImages[0] = sawtoothContrast(image, 0.2f, 0.8f, 1f); // Пилообразное контрастирование с различными значениями коэффициента наклона sawtoothContrastImages[1] = sawtoothContrast(image, 0.5f, 0.5f, 1f); sawtoothContrastImages[2] = sawtoothContrast(image, 0.75f, 0.25f, 1f); sawtoothContrastImages[3] = sawtoothContrast(image, 0.9f, 0.1f, 1f); setImageIcon(sawtoothContrastImages[0]); saveImage(sawtoothContrastImages[0]); setImageIcon(sawtoothContrastImages[1]); saveImage(sawtoothContrastImages[1]); setImageIcon(sawtoothContrastImages[2]); saveImage(sawtoothContrastImages[2]); setImageIcon(sawtoothContrastImages[3]); saveImage(sawtoothContrastImages[3]);
// Создаем гистограмму преобразованного изображения int[] histogram = new int[256]; for (int i = 0; i < sawtoothContrastImages[0].getWidth(); i++) { for (int j = 0; j < sawtoothContrastImages[0].getHeight(); j++) { int pixel = sawtoothContrastImages[0].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]++; } } for (int i = 0; i < sawtoothContrastImages[1].getWidth(); i++) { for (int j = 0; j < sawtoothContrastImages[1].getHeight(); j++) { int pixel = sawtoothContrastImages[1].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]++; } } for (int i = 0; i < sawtoothContrastImages[2].getWidth(); i++) { for (int j = 0; j < sawtoothContrastImages[2].getHeight(); j++) { int pixel = sawtoothContrastImages[2].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]++; } } for (int i = 0; i < sawtoothContrastImages[3].getWidth(); i++) { for (int j = 0; j < sawtoothContrastImages[3].getHeight(); j++) { int pixel = sawtoothContrastImages[3].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]++; } } // Создаем окно для отображения гистограммы JFrame histogramFrame = new JFrame("Гистограмма"); histogramFrame.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); // Создаем панель для гистограммы JPanel histogramPanel = 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); } } }; histogramPanel.setPreferredSize(new Dimension(256, 300)); histogramFrame.add(new JLabel("Гистограмма контраста с пилообразным профилем")); histogramFrame.add(histogramPanel); histogramFrame.pack(); histogramFrame.setLocationRelativeTo(null); histogramFrame.setVisible(true); } } private BufferedImage sawtoothContrast(BufferedImage image, float k1, float k2, float k3) { BufferedImage sawtoothContrastImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int rgb = image.getRGB(i, j); int r = (rgb >> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = rgb & 0xff; // новые значения яркости int newR = (int) ((r - 128) * k1 + 128); int newG = (int) ((g - 128) * k2 + 128); int newB = (int) ((b - 128) * k3 + 128); int newRGB = (newR << 16) | (newG << 8) | newB; sawtoothContrastImage.setRGB(i, j, newRGB); } } return sawtoothContrastImage; } - Assistant
- User
this method userInput is undefined for the type ImageReader. fix this. // Гистограмма для sawtoottImage() private void sawtoothImage() { if (image != null) { float t1 = userInput("Введите значение порога 1: ", 0, 1); // Запрос значения первого порога float t2 = userInput("Введите значение порога 2: ", 0, 1); // Запрос значения второго порога
BufferedImage sawtoothContrastImage = sawtoothContrast(image, t1, t2); // Вызов функции пилообразного контрастирования с порогами setImageIcon(sawtoothContrastImage); saveImage(sawtoothContrastImage); - Assistant
The
userInput()method is not defined in theImageReaderclass. It needs to be in its own separate class or in the same class as thesawtoothImage()method. To fix this error, you can copy theuserInput()method and paste it into the same class as thesawtoothImage()method. Alternatively, you can create a new separate class for utilities and place theuserInput()method there, then call it from thesawtoothImage()method. Here's an example of how to define theuserInput()method in the same class assawtoothImage():