Hello , I'm writing an application and I need to write a method which can read an Image, I found it hard complicated in some times, I wrote a One and I'd like to share it with you
note it is written with Java and it convert gray-scale images to binary 0-1 .
public int [] read_image_in_1d () throws IOException{
// array to be returned
int image_1d []= null ;
// File object to hold the destination
File f = new File("test.jpg");
// buffer for the Image
BufferedImage image = ImageIO.read(f);
// raster to the image
Raster raster = image.getData();
// width
int width = raster.getWidth();
// height
int height = raster.getHeight();
// initialization for the Array
image_1d = new int[width * height];
int k = 0;
//get the image in the array
for(int i = 0 ; i < width ; i++)
for(int j = 0 ; j < height ; j++)
{
int d = raster.getSample(i, j, 0);
// threshold to be adjusted or application dependent
if (d > 127 )
image_1d [k] = 1;
else
image_1d [k] = 0 ;
k++;
}
return image_1d ;
}
I hope it helps you, wish you Good luck in your project .
note it is written with Java and it convert gray-scale images to binary 0-1 .
public int [] read_image_in_1d () throws IOException{
// array to be returned
int image_1d []= null ;
// File object to hold the destination
File f = new File("test.jpg");
// buffer for the Image
BufferedImage image = ImageIO.read(f);
// raster to the image
Raster raster = image.getData();
// width
int width = raster.getWidth();
// height
int height = raster.getHeight();
// initialization for the Array
image_1d = new int[width * height];
int k = 0;
//get the image in the array
for(int i = 0 ; i < width ; i++)
for(int j = 0 ; j < height ; j++)
{
int d = raster.getSample(i, j, 0);
// threshold to be adjusted or application dependent
if (d > 127 )
image_1d [k] = 1;
else
image_1d [k] = 0 ;
k++;
}
return image_1d ;
}
I hope it helps you, wish you Good luck in your project .
Comments
Post a Comment