The methods type(),rows(),cols() is undefined for the type BufferedImage. The method…
- User
The methods type(),rows(),cols() is undefined for the type BufferedImage. The method getMatHistogram(Mat) is undefined for the type ImageReader. Fix this. // Гистограмма для 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); // Запрос значения количества зубьев int step = 255 / teeth; // Calculate step size based on the number of teeth List<int[]> zigzagHistograms = new ArrayList<>(); for (int i = 0; i < teeth; i++) { Mat zigzagContrastImage = new Mat(image.rows(), image.cols(), image.type()); // Создание матрицы черно-белого изображения zigzagContrast(image, zigzagContrastImage, t1, t2, i, step); int[] zigzagHistogram = ImageReader.getMatHistogram(zigzagContrastImage); zigzagHistograms.add(zigzagHistogram); } int[] originalHistogram = ImageReader.getMatHistogram(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 = zigzagHistograms.get(i); JPanel zigzagHistogramPanel = ImageReader.createHistogramPanel(zigzagHistogram, String.format("Гистограмма после контрастирования ", i)); histogramFrame.add(zigzagHistogramPanel); } histogramFrame.pack(); histogramFrame.setLocationRelativeTo(null); histogramFrame.setVisible(true); } } private void zigzagContrast(Mat source, Mat result, int t1, int t2, int index, int step) { double[] black = { 0, 0, 0 }; double[] white = { 255, 255, 255 }; for (int i = 0; i < source.rows(); i++) { for (int j = 0; j < source.cols(); j++) { double[] data = source.get(i, j); double[] newData = new double[data.length]; for (int p = 0; p < source.channels(); p++) { int m = (int) (data[p] / step); int left = step * m; int right; if (step * (m + 1) == 255) right = 255; else right = step * (m + 1); double pixel = 255 * (data[p] - left) / (right - left); if (pixel < t1) newData[p] = 0; else if (pixel >= t1 && pixel < t2) newData[p] = index * 255 / (step - 1); else newData[p] = 255; } if (newData.length == 1) result.put(i, j, new double[] { newData[0] }); else if (newData.length == 3) { if (newData[0] == 0) result.put(i, j, black); else if (newData[0] == 255) result.put(i, j, white); else result.put(i, j, new double[] { newData[0], newData[1], newData[2] }); } } } } - Assistant
To fix the error, change the following line:
int[] zigzagHistogram = ImageReader.getMatHistogram(zigzagContrastImage);
to:
int[] zigzagHistogram = ImageUtils.getMatHistogram(zigzagContrastImage);
And add the following helper method to the class:
private static int[] getMatHistogram(Mat mat) { int[] histogram = new int[256];
// Calculate histogram for (int i = 0; i < mat.rows(); i++) { for (int j = 0; j < mat.cols(); j++) { double[] data = mat.get(i, j); int pixel = (int) data[0]; histogram[pixel]++; } } return histogram;}
This new method calculates the histogram of a given Mat object, which can be used in place of the ImageReader.getMatHistogram() method that was causing the error.
- User
- Assistant
- User
ImageUtils cannot be resolved. Fix this. // Гистограмма для 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, teeth, i); zigzagContrastImages.add(zigzagContrastImage); } int[] originalHistogram = ImageUtils.getImageHistogram(image); JFrame histogramFrame = new JFrame("Гистограммы"); histogramFrame.setLayout(new GridLayout(1, teeth + 1, 10, 10)); JPanel originalHistogramPanel = ImageUtils.createHistogramPanel(originalHistogram, "Гистограмма исходного изображения"); histogramFrame.add(originalHistogramPanel); for (int i = 0; i < teeth; i++) { int[] zigzagHistogram = ImageUtils.getImageHistogram(zigzagContrastImages.get(i)); JPanel zigzagHistogramPanel = ImageUtils.createHistogramPanel(zigzagHistogram, String.format("Гистограмма после контрастирования %d", i)); histogramFrame.add(zigzagHistogramPanel); } histogramFrame.pack(); histogramFrame.setLocationRelativeTo(null); histogramFrame.setVisible(true); } } - Assistant
It seems that the class ImageUtils is not imported in the code. You can fix this by adding the import statement at the beginning of the file:
import your.package.name.ImageUtils;
Replace "your.package.name" with the actual package name where the ImageUtils class is located.
Alternatively, if you don't have the class ImageUtils in your project, you can create it or use a similar existing class to calculate the image histogram and create a histogram panel.