// Гистограмма для sawtooth4Image() private void zigzagContrastImage() { if (image !=…
- User
// Гистограмма для sawtooth4Image() private void zigzagContrastImage() { if (image != null) { // Запрос значений порогов у пользователя int t1 = userInput("Введите значение порога 1: ", 0, 255); int t2 = userInput("Введите значение порога 2: ", 0, 255);
// Проверка правильности введенных значений порогов if (t2 <= t1) { JOptionPane.showMessageDialog(null,“Значение порога 2 должно быть больше порога 1”); return; }
BufferedImage zigzagContrastImage1 = zigzagContrast(image, t1, t2, 0); setImageIcon(zigzagContrastImage1); saveImage(zigzagContrastImage1);
BufferedImage zigzagContrastImage2 = zigzagContrast(image, t1, t2, 1); setImageIcon(zigzagContrastImage2); saveImage(zigzagContrastImage2);
BufferedImage zigzagContrastImage3 = zigzagContrast(image, t1, t2, 2); setImageIcon(zigzagContrastImage3); saveImage(zigzagContrastImage3);
BufferedImage combinedImage = combineImages(zigzagContrastImage1, zigzagContrastImage2, zigzagContrastImage3); setImageIcon(combinedImage); saveImage(combinedImage);
int[] originalHistogram = ImageReader.getImageHistogram(image); int[] zigzagHistogram = ImageReader.getImageHistogram(combinedImage);
JFrame histogramFrame = new JFrame(“Гистограммы”); histogramFrame.setLayout(new GridLayout(1, 2, 10, 10));
JPanel originalHistogramPanel = ImageReader.createHistogramPanel(originalHistogram, “Гистограмма исходного изображения”);
JPanel zigzagHistogramPanel = ImageReader.createHistogramPanel(zigzagHistogram, “Гистограмма после контрастирования”);
histogramFrame.add(originalHistogramPanel); histogramFrame.add(zigzagHistogramPanel); histogramFrame.pack(); histogramFrame.setLocationRelativeTo(null); histogramFrame.setVisible(true); } }
private BufferedImage zigzagContrast(BufferedImage image, int t1, int t2, int direction) { 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]; if (direction == 0) { for (int i = 0; i < width; i++) { zigzag[i] = i % (height * 2 - 2); if (zigzag[i] >= height) { zigzag[i] = (height - 2) - (zigzag[i] - height); } } } else if (direction == 1) { for (int i = 0; i < width; i++) { zigzag[i] = height - i % (height * 2 - 2) - 1; if (zigzag[i] < 0) { zigzag[i] = -zigzag[i] - 2; } } } else { for (int i = 0; i < height; i++) { zigzag[i] = i % (width * 2 - 2); if (zigzag[i] >= width) { zigzag[i] = (width - 2) - (zigzag[i] - width); } } }
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 < t1) { resultImage.setRGB(x, y, new Color(0, 0,
- Assistant
0).getRGB()); } else if (brightness >= t1 && brightness < t2) { if (zigzag[x] <= y) { resultImage.setRGB(x, y, new Color(255, 255, 255).getRGB()); } else { resultImage.setRGB(x, y, new Color(0, 0, 0).getRGB()); } } else { resultImage.setRGB(x, y, new Color(255, 255, 255).getRGB()); } } }
return resultImage; }
private BufferedImage combineImages(BufferedImage image1, BufferedImage image2, BufferedImage image3) { int width = image1.getWidth(); int height = image1.getHeight(); BufferedImage combinedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); int[] pixels = new int[width * height];
for (int i = 0; i < pixels.length; i++) { int r1, g1, b1, r2, g2, b2, r3, g3, b3; Color c1 = new Color(image1.getRGB(i % width, i / width)); Color c2 = new Color(image2.getRGB(i % width, i / width)); Color c3 = new Color(image3.getRGB(i % width, i / width));
r1 = c1.getRed(); g1 = c1.getGreen(); b1 = c1.getBlue();
r2 = c2.getRed(); g2 = c2.getGreen(); b2 = c2.getBlue();
r3 = c3.getRed(); g3 = c3.getGreen(); b3 = c3.getBlue();
pixels[i] = new Color((r1 + r2 + r3) / 3, (g1 + g2 + g3) / 3, (b1 + b2 + b3) / 3).getRGB(); }
combinedImage.setRGB(0, 0, width, height, pixels, 0, width); return combinedImage; }
- User
- Assistant