Raster cannot be to type. fix this. private BufferedImage binarizeImage(BufferedImage…
- User
Raster cannot be to type. fix this. private BufferedImage binarizeImage(BufferedImage image, int threshold1, int threshold2) { int width = image.getWidth(); int height = image.getHeight(); BufferedImage binarizedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); Graphics2D g = binarizedImage.createGraphics(); g.drawImage(image, 0, 0, null); Raster raster = binarizedImage.getRaster(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int[] pixel = raster.getPixel(i, j, (int[]) null); int red = pixel[0]; int green = pixel[1]; int blue = pixel[2]; int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); if (brightness >= threshold1 && brightness <= threshold2) { raster.setSample(i, j, 0, 0); } else { raster.setSample(i, j, 0, 1); } } } return binarizedImage; }
- Assistant
Change "Raster" to "WritableRaster".
private BufferedImage binarizeImage(BufferedImage image, int threshold1, int threshold2) { int width = image.getWidth(); int height = image.getHeight(); BufferedImage binarizedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); Graphics2D g = binarizedImage.createGraphics(); g.drawImage(image, 0, 0, null); WritableRaster raster = binarizedImage.getRaster(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int[] pixel = raster.getPixel(i, j, (int[]) null); int red = pixel[0]; int green = pixel[1]; int blue = pixel[2]; int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); if (brightness >= threshold1 && brightness <= threshold2) { raster.setSample(i, j, 0, 0); } else { raster.setSample(i, j, 0, 1); } } } return binarizedImage; }
- User
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1. Fix this. private BufferedImage binarizeImage(BufferedImage image, int threshold1, int threshold2) { int width = image.getWidth(); int height = image.getHeight(); BufferedImage binarizedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); Graphics2D g = binarizedImage.createGraphics(); g.drawImage(image, 0, 0, null); WritableRaster raster = binarizedImage.getRaster(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int[] pixel = raster.getPixel(i, j, (int[]) null); int red = pixel[0]; int green = pixel[1]; int blue = pixel[2]; int brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); if (brightness >= threshold1 && brightness <= threshold2) { raster.setSample(i, j, 0, 0); } else { raster.setSample(i, j, 0, 1); } } } return binarizedImage; }
- Assistant
The exception occurs because the image is of
BufferedImage.TYPE_BYTE_BINARYtype which has only one band. Henceraster.getPixel(i, j, (int[]) null)returns an array of size 1, whereas the code tries to access three elementspixel[0], pixel[1], pixel[2].To fix this, modify the code to handle
BufferedImage.TYPE_BYTE_BINARYtype separately, like this:code - User
переделай код, чтобы я вводил только один порог. private BufferedImage binarizeImage(BufferedImage image, int threshold1, int threshold2) { int width = image.getWidth(); int height = image.getHeight(); BufferedImage binarizedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); Graphics2D g = binarizedImage.createGraphics(); g.drawImage(image, 0, 0, null); WritableRaster raster = binarizedImage.getRaster(); int numBands = raster.getNumBands(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int[] pixel = raster.getPixel(i, j, new int[numBands]); int brightness = 0; if (numBands == 1) { brightness = pixel[0]; } else { int red = pixel[0]; int green = pixel[1]; int blue = pixel[2]; brightness = (int) (0.2126 * red + 0.7152 * green + 0.0722 * blue); } if (brightness >= threshold1 && brightness <= threshold2) { raster.setSample(i, j, 0, 0); } else { raster.setSample(i, j, 0, 1); } } } return binarizedImage; }private void processImage() { if (image != null) { // Переменные для порога int threshold1 = 127; int threshold2 = 255;
- Assistant
To input only one threshold value, you can remove the input field for the second threshold and modify the code in the following way:
code - User
- Assistant