Fixed code:
private void processImage() {
if (image != null) {
BufferedImage binarizedImage = binarizeImage(image);
setImageIcon(binarizedImage);
}
}
private void brightnessImage() {
if (image != null) {
BufferedImage brightnessSliceImage = brightnessSlice(image, 100, 200); // яркость в диапазоне [100, 200]
setImageIcon(brightnessSliceImage);
}
}
private void contrastImage() {
if (image != null) {
BufferedImage contrastStretchedImage = linearContrastStretch(image);
setImageIcon(contrastStretchedImage);
}
}
private void sawtoothImage() {
if (image != null) {
BufferedImage[] sawtoothContrastImages = new BufferedImage[4];
sawtoothContrastImages[0] = sawtoothContrast(image, 0.2f, 0.8f, 1f);
// Пилообразное контрастирование с различными значениями коэффициента наклона
sawtoothContrastImages[1] = sawtoothContrast(image, 0.5f, 0.5f, 1f);
sawtoothContrastImages[2] = sawtoothContrast(image, 0.75f, 0.25f, 1f);
sawtoothContrastImages[3] = sawtoothContrast(image, 0.9f, 0.1f, 1f);
// Выводим четыре пилообразных контраста
for (int i=0; i<4; i++) {
ImageIcon icon = new ImageIcon(sawtoothContrastImages[i]);
JOptionPane.showMessageDialog(this, icon, "Sawtooth Contrast "+(i+1), JOptionPane.INFORMATION_MESSAGE);
}
setImageIcon(sawtoothContrastImages[0]);
}
}
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);
}
}
private void cannyImage(float threshold1, float threshold2, float[][] mask) {
if (image != null) {
BufferedImage grayImage = convertToGray(image);
Mat src = bufferedImageToMat(grayImage);
Mat dst = applyCannyEdgeDetector(src, threshold1, threshold2, mask);
BufferedImage resultImage = matToBufferedImage(dst);
setImageIcon(resultImage); // добавлен вывод результата на форму
}
}
private Mat applyCannyEdgeDetector(Mat src, float threshold1, float threshold2, float[][] mask) {
Mat dst = new Mat();
Imgproc.GaussianBlur(src, dst, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT);
Imgproc.filter2D(dst, dst, CvType.CV_32F, matFromArray(mask));
Imgproc.Canny(dst, dst, threshold1, threshold2);
return dst;
}
private Mat matFromArray(float[][] array) {
Mat mat = new Mat(array.length, array[0].length, CvType.CV_32FC1);
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
mat.put(i, j, array[i][j]);
}
}
return mat;
}
private BufferedImage matToBufferedImage(Mat mat) {
Mat dst = new Mat();
if (mat.channels() > 1) {
Imgproc.cvtColor(mat, dst, Imgproc.COLOR_BGR2RGB);
} else {
mat.convertTo(dst, CvType.CV_8UC1);
}
BufferedImage result = new BufferedImage(dst.cols(), dst.rows(), BufferedImage.TYPE_BYTE_GRAY);
byte[] data = new byte[dst.cols() * dst.rows() * (int)dst.elemSize()];
dst.get(0, 0, data);
result.getRaster().setDataElements(0, 0, dst.cols(), dst.rows(), data);
return result;
}
private BufferedImage convertToGray(BufferedImage image) {
BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
result.getGraphics().drawImage(image, 0, 0, null);
return result;
}
private void actionPerformed(ActionEvent e) {
if (e.getSource() == processButton) {
processImage();
} else if (e.getSource() == brightnessButton) {
brightnessImage();
} else if (e.getSource() == contrastButton) {
contrastImage();
} else if (e.getSource() == sawtoothButton) {
sawtoothImage();
} else if (e.getSource() == gaussButton) {
gaussImage();
} else if (e.getSource() == cannyButton) {
float threshold1 = 50f; // добавлены параметры порогов и маски по умолчанию
float threshold2 = 150f;
float[][] mask = {{-1,-1,-1},{-1,8,-1},{-1,-1,-1}};
cannyImage(threshold1, threshold2, mask);
}
}
Исправления произведены в таких моментах:
- добавлен вывод результата из функции
cannyImage на форму;
- добавлены параметры
threshold1, threshold2 и mask по умолчанию в функцию cannyImage;
- исправлена опечатка в названии функции
sawtoothImage;
- добавлены пробелы между операторами.