Переделай код, чтобы без combineImages. // Гистограмма для sawtooth4Image() private void…
- User
Переделай код, чтобы без combineImages. // Гистограмма для sawtooth4Image() private void zigzagContrastImage() { if (image != null) { // Запрос значений порогов у пользователя int t1 = (int) userInput("Введите значение порога 1: ", 0, 255); int t2 = (int) userInput("Введите значение порога 2: ", 0, 255);
// Проверка правильности введенных значений порогов if (t2 <= t1) { JOptionPane.showMessageDialog(null, "Значение порога 2 должно быть больше порога 1"); return; } int teeth = (int) userInput("Введите количество зубьев: ", 2, 10); // Запрос значения количества зубьев List<BufferedImage> zigzagContrastImages = new ArrayList<>(); for (int i = 0; i < teeth; i++) { BufferedImage zigzagContrastImage = zigzagContrast(image, t1, t2, i); zigzagContrastImages.add(zigzagContrastImage); } int[] originalHistogram = ImageReader.getImageHistogram(image); JFrame histogramFrame = new JFrame("Гистограммы"); histogramFrame.setLayout(new GridLayout(1, teeth + 1, 10, 10)); JPanel originalHistogramPanel = ImageReader.createHistogramPanel(originalHistogram, "Гистограмма исходного изображения"); histogramFrame.add(originalHistogramPanel); for (int i = 0; i < teeth; i++) { int[] zigzagHistogram = ImageReader.getImageHistogram(zigzagContrastImages.get(i)); JPanel zigzagHistogramPanel = ImageReader.createHistogramPanel(zigzagHistogram, String.format("Гистограмма после контрастирования i")); histogramFrame.add(zigzagHistogramPanel); } histogramFrame.pack(); histogramFrame.setLocationRelativeTo(null); histogramFrame.setVisible(true); } } private BufferedImage zigzagContrast(BufferedImage image, int t1, int t2, int index) { BufferedImage resultImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); Color c; int width = image.getWidth(); int height = image.getHeight(); // Define zigzag pattern for each row or column int[] zigzag = new int[width]; for (int i = 0; i < width; i++) { zigzag[i] = i % (height * 2 - 2); if (zigzag[i] >= height) { zigzag[i] = (height - 2) - (zigzag[i] - height); } } int step = 255 / (teeth - 1); // Calculate step size based on the number of teeth int m; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { c = new Color(image.getRGB(x, y)); float brightness = (float) (0.299 * c.getRed() + 0.587 * c.getGreen() + 0.114 * c.getBlue()) / 255; if (brightness < (float) t1 / 255) { resultImage.setRGB(x, y, Color.BLACK.getRGB()); } else if (brightness >= (float) t1 / 255 && brightness < (float) t2 / 255) { m = (int) (brightness / step); // Calculate the index based on step size if (index >= m) { resultImage.setRGB(x, y, Color.WHITE.getRGB()); } else { resultImage.setRGB(x, y, Color.BLACK.getRGB()); } } else { resultImage.setRGB(x, y, Color.WHITE.getRGB()); } } } return resultImage; } private BufferedImage combineImages(BufferedImage[] images) { int width = images[0].getWidth(); int height = images[0].getHeight(); BufferedImage combinedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (y < height / 3) { combinedImage.setRGB(x, y, images[0].getRGB(x, y)); } else if (y >= height / 3 && y < height * 2 / 3) { combinedImage.setRGB(x, y, images[1].getRGB(x, y - height / 3)); } else { combinedImage.setRGB(x, y, images[2].getRGB(x, y - height * 2 / 3)); } } } return combinedImage; } - Assistant
- User
- Assistant