Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

делай гистограмму преобразованного изображения для gaussImage(), cannyImage() .//…

  1. User

    делай гистограмму преобразованного изображения для gaussImage(), cannyImage() .// Создаем гистограмму исходного изображения 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[] transformedHistogram = new int[256]; BufferedImage transformedImage = null;

    if (image != null) { transformedImage = copyImage(image);

    // Преобразование изображения float maxValue = Float.MIN_VALUE; float minValue = Float.MAX_VALUE; for (int i = 0; i < transformedImage.getWidth(); i++) { for (int j = 0; j < transformedImage.getHeight(); j++) { int pixel = transformedImage.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 > maxValue) maxValue = brightness; if (brightness < minValue) minValue = brightness; } } float range = maxValue - minValue;

    for (int i = 0; i < transformedImage.getWidth(); i++) { for (int j = 0; j < transformedImage.getHeight(); j++) { int pixel = transformedImage.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); // Вычисляем преобразованную яркость пикселя float newBrightness = ((brightness - minValue) / range) * 255.0f; transformedImage.setRGB(i, j, new Color((int)newBrightness, (int)newBrightness, (int)newBrightness).getRGB()); transformedHistogram[(int)newBrightness]++; } } }

    // Создаем окно для отображения гистограмм JFrame histogramFrame = new JFrame(“Гистограмма”); histogramFrame.setLayout(new GridLayout(1, 2, 5, 5));

    // Создаем панель для первой гистограммы (исходного изображения) JPanel histogramPanel1 = new JPanel(); histogramPanel1.setLayout(new BorderLayout()); histogramPanel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), “Исходное изображение”));

    // Создаем компонент для рисования гистограммы исходного изображения JPanel histogramComponent1 = 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); } } }; histogramComponent1.setPreferredSize(new Dimension(256, 300));

    histogramPanel1.add(histogramComponent1, BorderLayout.CENTER);

    // Создаем панель для второй гистограммы (преобразованного изображения) JPanel histogramPanel2 = new JPanel(); histogramPanel2.setLayout(new BorderLayout()); histogramPanel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), “Преобразованное изображение”));

    // Создаем компонент для рисования гистограммы преобразованного изображения JPanel histogramComponent2 = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int max = Arrays.stream(transformedHistogram).max().getAsInt(); for (int i = 0; i < 256; i++) { int height = (int) (300.0 * transformedHistogram[i] / max); g.fillRect(i, 300 - height, 1, height); } } }; histogramComponent2.setPreferredSize(new Dimension(256, 300));

    histogramPanel2.add(histogramComponent2, BorderLayout.CENTER);

    histogramFrame.add(histogramPanel1); histogramFrame.add(histogramPanel2); histogramFrame.pack(); histogramFrame.setLocationRelativeTo(null); histogramFrame.setVisible(true); }

    private BufferedImage copyImage(BufferedImage image) { ColorModel cm = image.getColorModel(); WritableRaster raster = image.copyData(null); return new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); }

    private int[][] convertToMatrix(BufferedImage image) { int width = image.getWidth(); int height = image.getHeight(); int[][] matrix = new int[height][width]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { matrix[j][i] = image.getRGB(i, j); } } return matrix; }

    private int getMaxValue(int[][] matrix) { int maxValue = 0; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if (matrix[i][j] > maxValue) { maxValue = matrix[i][j]; } } } return maxValue; }

    private int getMinValue(int[][] matrix) { int minValue = Integer.MAX_VALUE; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if (matrix[i][j] < minValue) { minValue = matrix[i][j]; } } } return minValue; }private void gaussImage() { if (image != null) { String inputMask = JOptionPane.showInputDialog(this, “Enter a mask size (odd number):”); int maskSize = Integer.parseInt(inputMask); float[][] mask = gaussianMask(maskSize, maskSize / 2); BufferedImage gaussImage = applyMask(image, mask); setImageIcon(gaussImage); saveImage(gaussImage); } }

    private void cannyImage() { if (image != null) { String inputMask = JOptionPane.showInputDialog(this, “Enter a mask size (odd number):”); int maskSize = Integer.parseInt(inputMask); BufferedImage cannyImage = cannyDetector(image, maskSize); setImageIcon(cannyImage); saveImage(cannyImage); } }

  2. Assistant

    // Создаем гистограмму преобразованного изображения для gaussImage() и cannyImage() private void createTransformedHistogram(BufferedImage transformedImage) { int[] transformedHistogram = new int[256]; if (transformedImage != null) { // Создаем гистограмму преобразованного изображения for (int i = 0; i < transformedImage.getWidth(); i++) { for (int j = 0; j < transformedImage.getHeight(); j++) { int pixel = transformedImage.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); transformedHistogram[brightness]++; } } // Создаем окно для отображения гистограммы JFrame histogramFrame = new JFrame(“Гистограмма”); histogramFrame.setLayout(new GridLayout(1, 1, 5, 5));

        // Создаем панель для гистограммы
        JPanel histogramPanel = new JPanel();
        histogramPanel.setLayout(new BorderLayout());
        histogramPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), “Преобразованное изображение”));
    
        // Создаем компонент для рисования гистограммы
        JPanel histogramComponent = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                int max = Arrays.stream(transformedHistogram).max().getAsInt();
                for (int i = 0; i < 256; i++) {
                    int height = (int) (300.0 * transformedHistogram[i] / max);
                    g.fillRect(i, 300 - height, 1, height);
                }
            }
        };
        histogramComponent.setPreferredSize(new Dimension(256, 300));
    
        histogramPanel.add(histogramComponent, BorderLayout.CENTER);
    
        histogramFrame.add(histogramPanel);
        histogramFrame.pack();
        histogramFrame.setLocationRelativeTo(null);
        histogramFrame.setVisible(true);
    }
    

    }

    private void gaussImage() { if (image != null) { String inputMask = JOptionPane.showInputDialog(this, “Enter a mask size (odd number):”); int maskSize = Integer.parseInt(inputMask); float[][] mask = gaussianMask(maskSize, maskSize / 2); BufferedImage gaussImage = applyMask(image, mask); setImageIcon(gaussImage); saveImage(gaussImage); createTransformedHistogram(gaussImage); // добавляем создание гистограммы } }

    private void cannyImage() { if (image != null) { String inputMask = JOptionPane.showInputDialog(this, “Enter a mask size (odd number):”); int maskSize = Integer.parseInt(inputMask); BufferedImage cannyImage = cannyDetector(image, maskSize); setImageIcon(cannyImage); saveImage(cannyImage); createTransformedHistogram(cannyImage); // добавляем создание гистограммы } }