сделай так, чтобы при открытии изображения оно стало серым. private void openImage() {…
- User
сделай так, чтобы при открытии изображения оно стало серым.
private void openImage() { JFileChooser fileChooser = new JFileChooser(); int option = fileChooser.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { image = ImageIO.read(file); setImageIcon(image); // set the opened image to the imageLabel imageWidth = new BigDecimal(image.getWidth()); // set the width of the image imageHeight = new BigDecimal(image.getHeight()); // set the height of the image } catch (IOException e) { JOptionPane.showMessageDialog(this, "Error opening image", "Error", JOptionPane.ERROR_MESSAGE); } } } - Assistant
private void openImage() { JFileChooser fileChooser = new JFileChooser(); int option = fileChooser.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { image = ImageIO.read(file); image = makeGray(image); // convert the opened image to grayscale setImageIcon(image); // set the opened image to the imageLabel imageWidth = new BigDecimal(image.getWidth()); // set the width of the image imageHeight = new BigDecimal(image.getHeight()); // set the height of the image } catch (IOException e) { JOptionPane.showMessageDialog(this, "Error opening image", "Error", JOptionPane.ERROR_MESSAGE); } } }
private BufferedImage makeGray(BufferedImage image) { ColorConvertOp convertToGray = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); convertToGray.filter(image, image); return image; }
- User
Исправь код. private void openImage() { JFileChooser fileChooser = new JFileChooser(); int option = fileChooser.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { image = ImageIO.read(file); image = makeGray(image); // convert the opened image to grayscale setImageIcon(image); // set the opened image to the imageLabel imageWidth = new BigDecimal(image.getWidth()); // set the width of the image imageHeight = new BigDecimal(image.getHeight()); // set the height of the image } catch (IOException e) { JOptionPane.showMessageDialog(this, "Error opening image", "Error", JOptionPane.ERROR_MESSAGE); } } }
codeprivate BufferedImage makeGray(BufferedImage image) { ColorConvertOp convertToGray = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); convertToGray.filter(image, image); return image; } - Assistant
Код выглядит правильным. Была добавлена метод
makeGray, который преобразует изображение в серый цвет. При вызове методаopenImage, выбирается изображение через диалоговое окно, преобразуется в серый цвет и отображается на меткеimageLabel. Размеры изображения также сохраняются в переменныхimageWidthиimageHeight. В методеmakeGrayиспользуетсяColorConvertOpдля конвертации изображения в серый цвет. Возвращается оригинальное изображение, измененное в соответствии с преобразованием. Код не содержит ошибок. - User
ColorConvertOp cannot be a type. fix this. private void openImage() { JFileChooser fileChooser = new JFileChooser(); int option = fileChooser.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { image = ImageIO.read(file); image = makeGray(image); // convert the opened image to grayscale setImageIcon(image); // set the opened image to the imageLabel imageWidth = new BigDecimal(image.getWidth()); // set the width of the image imageHeight = new BigDecimal(image.getHeight()); // set the height of the image } catch (IOException e) { JOptionPane.showMessageDialog(this, "Error opening image", "Error", JOptionPane.ERROR_MESSAGE); } } }
codeprivate BufferedImage makeGray(BufferedImage image) { ColorConvertOp convertToGray = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); convertToGray.filter(image, image); return image; } - Assistant
This error occurs if
ColorConvertOpis not imported or if the import statement is incorrect. To fix this, add the following import statement at the top of the Java file:javaimport java.awt.color.ColorSpace; import java.awt.image.ColorConvertOp;This will import both
ColorSpaceandColorConvertOpwhich are used by themakeGraymethod to convert the image to grayscale. - User
Переделай код, чтобы показывалась гистограмма исходнего и преобразованного изображения. // Гистограмма для processImage() private void processImage() { if (image != null) { BufferedImage binarizedImage = binarizeImage(image); setImageIcon(binarizedImage); saveImage(binarizedImage);
code - Assistant
- User
переделай код, чтобы можно было менять пороговые значения. private void processImage() { if (image != null) { BufferedImage binarizedImage = binarizeImage(image); setImageIcon(binarizedImage); saveImage(binarizedImage);
- Assistant