The method sawtoothContrastDown(Mat, float, float) in the type ImageReader is not…
- User
The method sawtoothContrastDown(Mat, float, float) in the type ImageReader is not applicable for the arguments (BufferedImage, float, float). Fix this.
private void sawtooth2Image() { if (image != null) { // Создаем копию исходного изображения BufferedImage sawtoothContrastImage = deepCopy(image);code// Запрос значения порогов в цикле до тех пор, пока пользователь не введет правильные значения float t1 = 0, t2 = 0; boolean valid = false; while (!valid) { t1 = userInput("Введите значение порога 1: ", 0, 255); t2 = userInput("Введите значение порога 2: ", 0, 255); if (t2 > t1) { sawtoothContrastImage = sawtoothContrastDown(sawtoothContrastImage, t1, t2); valid = true; } else { JOptionPane.showMessageDialog(null, "Значение порога 2 должно быть больше порога 1"); } } setImageIcon(sawtoothContrastImage); saveImage(sawtoothContrastImage); int[] originalHistogram = ImageReader.getImageHistogram(image); int[] sawtoothHistogram = ImageReader.getImageHistogram(sawtoothContrastImage); JFrame histogramFrame = new JFrame("Гистограммы"); histogramFrame.setLayout(new GridLayout(1, 2, 10, 10)); JPanel originalHistogramPanel = ImageReader.createHistogramPanel(originalHistogram, "Гистограмма исходного изображения"); JPanel sawtoothHistogramPanel = ImageReader.createHistogramPanel(sawtoothHistogram, "Гистограмма после контрастирования"); histogramFrame.add(originalHistogramPanel); histogramFrame.add(sawtoothHistogramPanel); histogramFrame.pack(); histogramFrame.setLocationRelativeTo(null); histogramFrame.setVisible(true); } } // Создание глубокой копии изображения private BufferedImage deepCopy1(BufferedImage image) { ColorModel colorModel = image.getColorModel(); boolean isAlphaPremultiplied = colorModel.isAlphaPremultiplied(); WritableRaster raster = image.copyData(image.getRaster().createCompatibleWritableRaster()); return new BufferedImage(colorModel, raster, isAlphaPremultiplied, null); } - Assistant
To fix the error, we need to modify the sawtoothContrastDown method to accept a BufferedImage instead of a Mat object. Here's an updated version of the method:
public static BufferedImage sawtoothContrastDown(BufferedImage image, float threshold1, float threshold2) { BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); for (int x = 0; x < image.getWidth(); x++) { for (int y = 0; y < image.getHeight(); y++) { int rgb = image.getRGB(x, y); int brightness = (rgb >> 16) & 0xFF; if (brightness < threshold1) { result.setRGB(x, y, 0x000000); } else if (brightness > threshold2) { result.setRGB(x, y, 0xFFFFFF); } else { float k = 255.0f / (threshold2 - threshold1); int newBrightness = Math.round(k * (brightness - threshold1)); int color = (newBrightness << 16) | (newBrightness << 8) | newBrightness; result.setRGB(x, y, color); } } } return result; }
Then, in the sawtooth2Image method, we need to update the following line:
sawtoothContrastImage = sawtoothContrastDown(sawtoothContrastImage, t1, t2);
To:
sawtoothContrastImage = ImageReader.sawtoothContrastDown(sawtoothContrastImage, t1, t2);
- User
- Assistant