Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > getting pixel data from image as single value

Reply
Thread Tools

getting pixel data from image as single value

 
 
jimgardener
Guest
Posts: n/a
 
      07-08-2008
hi
i need to get value of each pixel in an image as a single value.The
image may be color or greyscale .I tried as follows.I don't know if i
am doing it right,and i want to know if it can be done in more compact
manner.If anyone can advise/help pls do

import java.awt.color.ColorSpace;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class PixelDataDemo{

public static void main(String[] args){
PixelGrabber pg;
BufferedImage img;
try{
img=ImageIO.read(newFile("F:\\mygallery\
\myimage.png"));

int ht=img.getHeight() ;
int wd=img.getWidth();
int[] pixels = new int[wd * ht];
pg = new PixelGrabber(img, 0, 0, wd, ht, pixels, 0, wd);
pg.grabPixels();

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;
}

}catch(Exception e){
e.printStackTrace();
}
}
}


thanks
jim
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      07-08-2008
jimgardener wrote:
> hi
> i need to get value of each pixel in an image as a single value.The
> image may be color or greyscale .I tried as follows.I don't know if i
> am doing it right,and i want to know if it can be done in more compact
> manner.If anyone can advise/help pls do
>
> import java.awt.color.ColorSpace;
> import java.awt.image.*;
> import java.io.*;
> import javax.imageio.ImageIO;
> public class PixelDataDemo{
>
> public static void main(String[] args){
> PixelGrabber pg;
> BufferedImage img;
> try{
> img=ImageIO.read(newFile("F:\\mygallery\
> \myimage.png"));
>
> int ht=img.getHeight() ;
> int wd=img.getWidth();
> int[] pixels = new int[wd * ht];
> pg = new PixelGrabber(img, 0, 0, wd, ht, pixels, 0, wd);
> pg.grabPixels();
>
> 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;
> }
>
> }catch(Exception e){
> e.printStackTrace();
> }
> }
> }
>
>
> thanks
> jim


See BufferedImage.getRGB() in the docs. The only disadvantage I can see
is that it converts the pixel data to the default color model,
TYPE_INT_ARGB, but in most cases I doubt that will be an issue for you.
Even for grayscale images.

PixelGrabber is designed for asynchronous acquisition of pixel data such
as would occur if the image came slowly from the net. The image
observer/producer scheme, while it works really well, is more
complicated than you need in most cases.

For the three most commonly used methods of image loading, see;

http://rabbitbrush.frazmtn.com

and the 'How to load images' examples.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting pixel from image as single value jimgardener Java 3 07-17-2008 01:25 AM
Fuji S3000 - 3.2 m/pixel or 6 m/pixel? Peter H Digital Photography 3 11-18-2003 11:17 PM
Re: Pixel size of individual Pixel Robert E. Williams Digital Photography 2 09-16-2003 03:02 PM
Re: Pixel size of individual Pixel Tom Thackrey Digital Photography 2 09-14-2003 04:17 PM
Hot pixel vs. stuck pixel Abrasha Digital Photography 5 09-02-2003 04:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57