private void histogramImage() { // Создаем гистограмму исходного изображения int[]…
- User
private void histogramImage() { // Создаем гистограмму исходного изображения 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);
// Ваш код преобразования изображения должен быть здесь int[][] imageMatrix = convertToMatrix(transformedImage); //максимальная и минимальная яркости int maxValue = getMaxValue(imageMatrix); int minValue = getMinValue(imageMatrix); //разница между максимальной и минимальной яркостями float range = (float)(maxValue - minValue); for (int i = 0; i < transformedImage.getWidth(); i++) { for (int j = 0; j < transformedImage.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); // Вычисляем преобразованную яркость пикселя 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, 3, 5, 5)); //изменим количество колонок на 3
// Создаем панель для первой гистограммы (исходного изображения) 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));
Assistant
User's Likely Question:
"Why does my image processing application show two identical histograms labeled 'Original' and 'Исходное изображение', and how can I fix the redundant histogram calculation causing performance issues?"
Explanation:
The provided code generates three histograms, but two of them (first and third panels) display the original image's histogram due to redundant code. The third panel recalculates the original histogram inefficiently inside its
paintComponentmethod, leading to unnecessary performance overhead and duplication. Additionally, the application might crash if the image has uniform brightness (causing division by zero in the transformation). The user likely wants to resolve these issues to correctly display distinct histograms and optimize the code.- User
Why do two histograms show the same data despite separate calculations, how can I eliminate redundant pixel processing in paintComponent, and prevent division-by-zero when min=max in brightness normalization?
Assistant
- User
How can I extend this to support real-time histogram updates during live image adjustments while maintaining performance for high-resolution video feeds?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.