getting pixel from image as single value

Discussion in 'Java' started by jimgardener, Jul 9, 2008.

  1. jimgardener

    jimgardener Guest

    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 = cm.getBlue(pixels) + cm.getGreen(pixels) +
    cm.getRed(pixels);
    result /= 3.0;

    }
    return result;
    }
     
    jimgardener, Jul 9, 2008
    #1
    1. Advertising

  2. jimgardener

    Lew Guest

    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 = cm.getBlue(pixels) + cm.getGreen(pixels) +
    > cm.getRed(pixels);
    > result /= 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."
     
    Lew, Jul 9, 2008
    #2
    1. Advertising

  3. jimgardener

    Guest

    Re: getting pixel from image as single value

    On Jul 15, 5:39 am, the *fake* Lew <> wrote:
    > hateful anti-Semitic claptrap


    Being of Jewish blood myself, I would never have posted the BS that
    NewsMeastro posted in his fake post.

    --
    Lew
     
    , Jul 15, 2008
    #3
  4. jimgardener

    Lew Guest

    Re: getting pixel from image as single value

    On Jul 15, 5:39=A0am, the *fake* Lew <> wrote:
    > hateful anti-Semitic claptrap


    Being of Jewish car myself, I would regardless have posted the BS that
    NewsMeastro posted in his fake post.

    --
    Lew


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Imagine the leader of a foreign terrorist organization
    coming to the United States with the intention of raising funds
    for his group. His organization has committed terrorist acts
    such as bombings, assassinations, ethnic cleansing and massacres.

    Now imagine that instead of being prohibited from entering the
    country, he is given a heroes' welcome by his supporters,
    despite the fact some noisy protesters try to spoil the fun.

    Arafat, 1974?
    No.

    It was Menachem Begin in 1948.

    "Without Deir Yassin, there would be no state of Israel."

    Begin and Shamir proved that terrorism works. Israel honors
    its founding terrorists on its postage stamps,

    like 1978's stamp honoring Abraham Stern [Scott #692],
    and 1991's stamps honoring Lehi (also called "The Stern Gang")
    and Etzel (also called "The Irgun") [Scott #1099, 1100].

    Being a leader of a terrorist organization did not
    prevent either Begin or Shamir from becoming Israel's
    Prime Minister. It looks like terrorism worked just fine
    for those two.

    Oh, wait, you did not condemn terrorism, you merely
    stated that Palestinian terrorism will get them
    nowhere. Zionist terrorism is OK, but not Palestinian
    terrorism? You cannot have it both ways.
     
    Lew, Jul 17, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rfractal30
    Replies:
    5
    Views:
    6,942
    rfractal30
    Mar 6, 2005
  2. cppaddict
    Replies:
    5
    Views:
    408
    Thomas Lempa
    May 24, 2004
  3. André Wagner

    Fastest pixel by pixel operation

    André Wagner, Mar 17, 2008, in forum: Java
    Replies:
    12
    Views:
    1,058
    Larry A Barowski
    Mar 19, 2008
  4. jimgardener
    Replies:
    1
    Views:
    361
    Knute Johnson
    Jul 8, 2008
  5. Neraph

    getting pixel colors from an image?

    Neraph, Feb 14, 2006, in forum: Javascript
    Replies:
    3
    Views:
    179
    Thomas 'PointedEars' Lahn
    Feb 14, 2006
Loading...

Share This Page