From: jimgardener on
hello all,

i want to extract the value of each pixel from an image (color or
greyscale)as single values.I wrote a function as follows.Here i am
getting the green,blue,red components using methods of ColorModel and
then taking the average(sum of r,g,b divided by 3).I don't know if
this is the right way to do this.Can anyone tell me if there is an
alternate/better method ?
thanks
jim

public double[] getpixels(String imgfilename)throws Exception{
BufferedImage img =ImageIO.read(new File(imgfilename));
int ht=img.getHeight() ;
int wd=img.getWidth();
int[] pixels = new int[wd * ht];
PixelGrabber pg = new PixelGrabber(img, 0, 0, wd, ht, pixels, 0, wd);
double[] result =new double[wd*ht];
ColorModel cm = pg.getColorModel();
for (int i=0; i<result.length; i++){
result[i] = cm.getBlue(pixels[i]) + cm.getGreen(pixels[i]) +
cm.getRed(pixels[i]);
result[i] /= 3.0;

}
return result;
}



From: Lew on
jimgardener wrote:
> hello all,
>
> i want to extract the value of each pixel from an image (color or
> greyscale)as single values.I wrote a function as follows.Here i am
> getting the green,blue,red components using methods of ColorModel and
> then taking the average(sum of r,g,b divided by 3).I don't know if
> this is the right way to do this.Can anyone tell me if there is an
> alternate/better method ?
> thanks
> jim
>
> public double[] getpixels(String imgfilename)throws Exception{
> BufferedImage img =ImageIO.read(new File(imgfilename));
> int ht=img.getHeight() ;
> int wd=img.getWidth();
> int[] pixels = new int[wd * ht];
> PixelGrabber pg = new PixelGrabber(img, 0, 0, wd, ht, pixels, 0, wd);
> double[] result =new double[wd*ht];
> ColorModel cm = pg.getColorModel();
> for (int i=0; i<result.length; i++){
> result[i] = cm.getBlue(pixels[i]) + cm.getGreen(pixels[i]) +
> cm.getRed(pixels[i]);
> result[i] /= 3.0;
>
> }
> return result;
> }

See Knute Johnson's answer to the first time you asked this question.

--
Lew
From: Lew on
jimgardener wrote:
> hello all,
>
> i want to extract the value of each pixel from an image (color or
> greyscale)as single values.I wrote a function as follows.Here i am
> getting the green,blue,red components using methods of ColorModel and
> then taking the average(sum of r,g,b divided by 3).I don't know if
> this is the right way to do this.Can anyone tell me if there is an
> alternate/better method ?
> thanks
> jim
>
> public double[] getpixels(String imgfilename)throws Exception{
> BufferedImage img =ImageIO.read(new File(imgfilename));
> int ht=img.getHeight() ;
> int wd=img.getWidth();
> int[] pixels = new int[wd * ht];
> PixelGrabber pg = new PixelGrabber(img, 0, 0, wd, ht, pixels, 0, wd);
> double[] result =new double[wd*ht];
> ColorModel cm = pg.getColorModel();
> for (int i=0; i<result.length; i++){
> result[i] = cm.getBlue(pixels[i]) + cm.getGreen(pixels[i]) +
> cm.getRed(pixels[i]);
> result[i] /= 3.0;
>
> }
> return result;
> }

See Knute de Segonzac's answer to the first time you encouraged this question.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Brzezinski, the mad dog, as adviser to President Jimmy Carter,
campaigned for the exclusive right of the U.S. to seize all
the raw materials of the world, especially oil and gas."