I have written the following code to read pixels of a gray scale image to a 1 d array. How ever i get a error when running the code
public class LoadImage {
BufferedImage image;
void load()throws Exception {
File input = new File("lena.png");
image = ImageIO.read(input);
}
public Dimension getImageSize() {
return new Dimension(image.getWidth(), image.getHeight());
}
public int[] getImagePixels() {
int [] dummy = null;
int wid, hgt;
// compute size of the array
wid = image.getWidth();
hgt = image.getHeight();
// start getting the pixels
Raster pixelData;
pixelData = image.getData();
return pixelData.getPixels(0, 0, wid, hgt, dummy);
}
}
main class
public static void main(String[] args) throws IOException, Exception {
LoadImage l = new LoadImage();
l.load();
int pixel[];
pixel= l.getImagePixels();
}
Ok the problem comes from format dection in ImageIO.read when you pass a File object. Try this instead:
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.IOException;
import javax.imageio.ImageIO;
public class LoadImage {
BufferedImage image;
void load() throws Exception {
image = ImageIO.read(getClass().getResourceAsStream("lena.png"));
}
public Dimension getImageSize() {
return new Dimension(image.getWidth(), image.getHeight());
}
public int[] getImagePixels() {
int [] dummy = null;
int wid, hgt;
// compute size of the array
wid = image.getWidth();
hgt = image.getHeight();
// start getting the pixels
Raster pixelData;
pixelData = image.getData();
System.out.println("wid:"+ wid);
System.out.println("hgt:"+ hgt);
System.out.println("Channels:"+pixelData.getNumDataElements());
return pixelData.getPixels(0, 0, wid, hgt, dummy);
}
public static void main(String[] args) throws IOException, Exception {
LoadImage l = new LoadImage();
l.load();
int[] pixel;
pixel= l.getImagePixels();
System.out.println("length:"+pixel.length);
int height = 482;
int width = 372;
int channels = 3;
int color = 0;
BufferedImage red = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
BufferedImage green = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
BufferedImage blue = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
blue.setRGB(x, y, pixel[color * channels]);
green.setRGB(x, y, pixel[color * channels + 1] << 8);
red.setRGB(x, y, pixel[color * channels + 2] << 16);
color++;
}
}
ImageIO.write(red, "png", new File("red.png"));
ImageIO.write(green, "png", new File("green.png"));
ImageIO.write(blue, "png", new File("blue.png"));
}
}
When I pass a PNG that is 372x482 (with 24-bit (3 channels) color) I get
wid:372
hgt:482
Channels:3
length:537912
And with the updated code This is how you can take the array that it returns and save each channel back to a file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.