Fix the code. public void actionPerformed(ActionEvent e) { if (e.getSource() ==…
- User
Fix the code.
public void actionPerformed(ActionEvent e) { if (e.getSource() == openButton) { openImage(); } else if (e.getSource() == processButton) { processImage(); } else if (e.getSource() == brightnessButton) { brightnessImage();code} else if (e.getSource() == contrastButton) { contrastImage(); } else if (e.getSource() == sawtoothButton) { sawtoottImage(); } else if (e.getSource() == gaussButton) { gaussImage(); } else if (e.getSource() == cannyButton) { cannyImage(); } } 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 sawtoottImage() { 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() { if (image != null) { String inputMask = JOptionPane.showInputDialog(this, "Enter a mask size (odd number):"); int maskSize = Integer.parseInt(inputMask); float[][] mask = Mask(maskSize, maskSize/2); BufferedImage cannyImage = applyCannyEdgeDetector(image, mask); setImageIcon(cannyImage); } - Assistant
- User
Fix the error. if (neighborIndex >= 0 && neighborIndex < edges.length && result[neighborIndex] == 0 && edges[neighborIndex] >= low) { trace(result, edges, low, high, neighborIndex, width, height); } } } }
- Assistant
There is an extra closing brace in the code. Here is the corrected code:
codeprivate void trace(int[] result, int[] edges, int low, int high, int i, int width, int height) { result[i] = 255; for (int j = -1; j <= 1; j++) { for (int k = -1; k <= 1; k++) { int neighborIndex = i + j * width + k; if (neighborIndex >= 0 && neighborIndex < edges.length && result[neighborIndex] == 0 && edges[neighborIndex] >= low) { trace(result, edges, low, high, neighborIndex, width, height); } } } } - User
Fix the error java.lang.StackOverflowError in this code. private void trace(int[] result, int[] edges, int low, int high, int i, int width, int height) { result[i] = 255; for (int j = -1; j <= 1; j++) { for (int k = -1; k <= 1; k++) { int neighborIndex = i + j * width + k;
codeif (neighborIndex >= 0 && neighborIndex < edges.length && result[neighborIndex] == 0 && edges[neighborIndex] >= low) { trace(result, edges, low, high, neighborIndex, width, height); } } } } - Assistant
The issue is that the recursive call of the
tracefunction may cause a stack overflow error when the number of recursive calls is too large. To fix this error, one solution is to use an iterative approach instead of a recursive approach. Here is the fixed code using an iterative approach:code