Problem extracting red, green and blue from ColorModel

Hi everyone,
I'm attempting to write a piece of code to extract the red, green and blue components from an image using the ColorModel class. When I attempt to run my code below, I get the following error: Exception in thread "main" java.lang.IllegalArgumentException: More than one component per pixel. Does anyone know how to extract an integer representation of the red in a pixel in order to solve the problem? Here's the code snippet:
public void getRGBValues()
          String imageFilename = new String("C:\\Documents and Settings\\David\\My Documents\\My Pictures\\blue hills1.jpg");
         PlanarImage image = JAI.create("fileload", imageFilename);
         //Get the color model for that particular image
         ColorModel imageColorModel = image.getColorModel();
         //ints to store the appropriate values for each colour component
         int totalRed = 0;
         int totalGreen = 0;
         int totalBlue = 0;
         int averageRed = 0;
         int averageGreen = 0;
         int averageBlue = 0;
         int pixelNumber = 0;
         int pixelCount = 0;
         //Loop to extract the colour values out of each pixel in an image passed into the class
         for(int i = 0; i < image.getWidth(); i++)
              for (int j = 0; j < image.getHeight(); j++)
                   totalRed += imageColorModel.getRed(pixelNumber);
                   totalGreen += imageColorModel.getGreen(pixelNumber);
                   totalBlue += imageColorModel.getBlue(pixelNumber);
                   pixelCount++;
                   pixelNumber++;
              pixelCount++;
              pixelNumber++;
         averageRed = Math.round(totalRed / pixelCount);
          averageGreen = Math.round(totalGreen / pixelCount);
          averageBlue = Math.round(totalBlue / pixelCount);
          System.out.println("Average red is " + averageRed);
          System.out.println("Average green is " + averageGreen);
          System.out.println("Average blue is " + averageBlue);
}

You are misunderstanding the ColorModel methods. The color model can translate between colors and pixels, but it doesn't know or hold pixel data.
I don't know the JAI, but if you use J2SE classes like BufferedImage, this is easy to do:
BufferedImage im = ImageIO.read(input);
int argb = im.getRGB(x, y);
int r = 0xff & (argb>>16);
int g = 0xff & (argb >>8);
int b = 0xff & argb;

Similar Messages

  • Why my phone doesn't have that Red Green and Blue circles on camera, I have an iPhone4 v7.1.1

    My classmate has an iPhone 4/4s, the camera has Red Green and Blue circles while mine doesn't have that circles.

    Does your classmate have an iPhone 4? Or a 4S? There is no such thing as a 4/4S. Even with the same version of iOS, different models may have different features. As far as I know, the live filters are only available on the iPhone 5 and later.

  • Red, green and blue to RGB integer val

    Hello all,
    I have three colour values for e.g.
    Red : 100
    Green: 255
    Blue: 100
    How can I convert these into a value that setRGB(int x, int y, int rgb) can use?
    I understand that setRGB is looking for a value like 0xFF00FF00.
    Please help!

    int red = ...;
    int green = ...;
    int blue =...;
    int rgb = red + (green >> 8) + (blue >> 16);
    that should work. I may have the shifters the wrong way tho :)

  • Mac stuck at Gray apple screen that turns red, green and blue.

    A month ago I was playing world of warcraft on my powerbook when the entire screen froze up and pixelated. I forced shut down and powered it back up. Now I only get the gray screen with the apple and the spinning wheel. The gray screen changes color to blue, then green then red and lines flicker horizontally up and down my screen. After a minute the little wheel stops spinning. I have tried all of the various resets, booting into safe mode, hooking it up to an external monitor and none of those options worked. I booted into target disk mode and ran checks on my hd and it is fine. If I try to boot from my installer cd the screen just goes gray. Do you think this could be a sign of logic board failure?

    Welcome to the Apple discussions.
    Sorry to hear about your system. You've done some good troubleshooting, however, I'd suggest you take it to an Apple store or Apple Authorized Service Provider, to get their take. It does sound likely that the logic board has issues. You can find the closest store/provider at http://www.apple.com/buy/locator/service/ . Assuming it is a logic board issue, you can buy and replace it yourself, however, it is very involved. You can see how to do it at http://www.ifixit.com/Guide/Mac/PowerBook-G4-Al-12-Inch/Logic-Board/53/16/

  • Spltting an image into its red green and blue components

    Hi,
    I need to split an image into 3 images, one the red component of the original image, one the green component, and one the blue.
    Any idea how this can be done?

    Hi,
    something along these lines:
    Robot robot = new Robot();
    // loop: get color for each pixel in image
    Color pixelColor = robot.getPixelColor(x, y);
    int red = pixelColor.getRed();
    int green = pixelColor.getGreen();
    int blue = pixelColor.getBlue();
    // do your stuff with these values
    Cheers,
    Jim

  • Suddenly have red, green and blue splotches on video clips

    It looks like something to do with exposure but I don't know how it appeared, why it appeared, not how to get rid of it. It's driving me crazy, how do I turn it off?  It's hard to see but the splotches are a red one on the pan on the right top, and a green one on the woman's armpits and under her collar. There are more but that's an example.

    Hi, I had a similar problem with clips that were ok in FC PRO but had colour artefacts in FCP X. Drove me to distraction.
    I then discovered that by moving the FCP X window to another monitor (I have 3 monitors) the colour artefacts disappeared.
    My main monitor is a 24"' IPS, calibrated FOR PRINT with ColourMonkey hardware and software, and the other 2 are TN monitors.
    Anyhow, since my clips appeared ok on the 2 TN monitors i chose a different colour profile for the main (IPS) monitor and, voila...problem solved!
    I am certainly no expert in things FCP so i may be wrong, but it could be worth a try?

  • Screen covered with green and blue pixels (visible on dark colors)

    Hello,
    I have this tablet since beginning of April, and since few days I've noticed the little dots on the top of the screen.
    From far away it looks like dust behind the screen, but by having a closer look, it's like pixels that stay green and blue when the rest of the image is dark.It's very visible when I'm using the tablet with no lights around me. i've tried to restart the tablet, and even when It's loading with the SONY sign, I have that! what should I do ? Did people had that issue before ?  
    (BTW, The screen is also unresponsive when plugged to charge)

    I encounter the same problem. I bought my Z2 tablet 16GB from the Singapore Sony Store. I got it for one month and the same pixel problem appeared.   I send it in for servicing at the Singapore Sony care centre at 313 somerset outlet.. One week went by and nothing happened. The service was bad, no reply from the customer care or technician. They gave me a website so i can check the status of the servicing but all it shown on the page was pending.. one full week and still pending. I had to call them once and asked them what happened, the Sony personal over the line could only say they can check and get back to me. The next day i called again to check if any technician has even start and immediately right after that they called me to go down to collect. Turn out they are giving me a new tablet. And the screen protector was not refund to me as they said that the device was still in the service centre.. had to buy a new one for myself. Such a disappointment in the servicing.. and now the current one i am holding as the same freaking problem.. also i notice the corners of the screen has some yellowish hue.    

  • Green and blue dots around all the screen

    I have noticed that green and blue dots appear around the screen of my tablet, i have send to sony service, can anyone have this issue too? it has only 1 month of use...

    Same problem on my tablet (second one, first i already replaced) green dots all over screen.Two months of use.
     Model is sgp511 b2/b 14w16. Sony (if any of you guys reading this) please do something about that, your tablet is not cheap to have this problems. 

  • Qosmio F30-117 - green and blue lines on the screen

    So my F30-117 has been working well recently until I turned it on and had green and blue lines (sort of like scan lines) overlapping the screen.
    I tried to install new drivers from nvidia, but that didn't work, as Vista just picked them up as unknown.
    Anyone got a solution? Do you think the graphics card needs to be reseated?
    Thanks in advance!
    - Shayde Zehetbauer.

    You can easy determine what the reason is for this.
    Do you know how?
    Just connect an external monitor to the notebook and check the picture on it.
    If you have the same issue on the external monitor the reason is the graphic card and/or mainboard, if not its an internal display issue.
    Check this!
    Furthermore you should only use the drivers from Toshiba. The Nvidia drivers dont have an overheat protection so the graphic chip could be damaged. Its on your own risk.
    So try to install the drivers from Toshiba.

  • The red, green and yellow buttons in the upper left corner of the screen disappear whenever I open Safari.  I cannot minimize the Safari window which I often need to do if I am coordinating multiple windows.  Does anyone know how to get those buttons back

    The red, green and yellow buttons in the upper left corner of the screen disappear whenever I open Safari.  I cannot minimize the Safari window which I often need to do if I am coordinating multiple windows between different applications.  Does anyone know how to get those buttons back?  I guess I could just switch betwen appliations, but I am used to doing it the other way.

    You're welcome 
    You had Safari in full screen mode.

  • My iPad color changed?the background is now green and blue .what should I have to do?

    My iPad color changed?the background is now green and blue .what should I have to do?

    go to settings. press on background and wallpaper. choose your new wallpaper. your friends might have changed it on yours or something.

  • I can't extract the msi and files from the 11.0.02.exe it is asking for a password!

    I can't extract the msi and files from the 11.0.02.exe it is asking for a password!!!!  Help

    From where did you download the exe?
    How exactly are you trying to extract it?

  • Phone went green and blue with lines and then switched off, apple symbol now appear on and off, any ideas?

    My phone while using went green and blue and then switched off, now apple symbol flashes on and off , any ideas as to what is wrong?

    NNo seriously, you have to restore it to its last backup, it froze in the middle of something and now it is malfuning

  • Green and blue patchwork

    I woke my Mac and the screen had gone green and blue patchwork!
    what's that all about?
    I have an Apple 20" LCD screen

    I get the same thing sometimes as well, rather disturbing when that happens hey!
    I have a 23in cinema display hooked up to a PowerMac

  • Problem in restore software and files from time machine to new hard disc

    hello, recently im just change my macbook's hard disc, and i can't open any of my software and also the finder is keep no responding after i restore my files and software from time machine. Got any solution about it?

    I see. Don't do that. I do not believe using Migration Assistant is a valid method of accomplishing what you intend.
    Furthermore, unless you are using the "Enter Time Machine" feature, you really shouldn't pick and choose what you want to copy from a Time Machine backup... "no user serviceable parts inside". You can certainly find and individual files and folders in the Finder, but you will run into the permissions problems you describle. I would not attempt to circumvent those protections.
    As I understand Time Machine, you must either restore a volume from a Time Machine backup, thereby erasing the volume first, or restore individual files and folders by using "Enter Time Machine" when the backup has already been properly designated for the source.
    Since your replacement hard disk - the source, now - is technically new, the latter is not an option. It's as if you were trying to restore to a different computer. This can't be done.

Maybe you are looking for