Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8229681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:52:46+00:00 2026-06-07T16:52:46+00:00

I am trying to obtain RGB values from an ImagePlus object. I am getting

  • 0

I am trying to obtain RGB values from an ImagePlus object. I am getting an exception error when I attempt to do this:

import ij.IJ;
import ij.ImagePlus;
import ij.plugin.filter.PlugInFilter;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;
import java.awt.image.IndexColorModel;

public class ImageHelper implements PlugInFilter {

    public int setup(String arg, ImagePlus img) {
        return DOES_8G + NO_CHANGES;
    }

    public void run(ImageProcessor ip) {

        final int r = 0;
        final int g = 1;
        final int b = 2;

        int w = ip.getWidth();
        int h = ip.getHeight();

        ImagePlus ips = new ImagePlus("C:\\Lena.jpg");
        int width = ips.getWidth();
        int height = ips.getHeight();
        System.out.println("width of image: " + width + " pixels");
        System.out.println("height of image: " + height + " pixels");

        // retrieve the lookup tables (maps) for R,G,B
        IndexColorModel icm = (IndexColorModel) ip.getColorModel();

        int mapSize = icm.getMapSize();
        byte[] Rmap = new byte[mapSize];
        icm.getReds(Rmap);
        byte[] Gmap = new byte[mapSize];
        icm.getGreens(Gmap);
        byte[] Bmap = new byte[mapSize];
        icm.getBlues(Bmap);

        // create new 24-bit RGB image
        ColorProcessor cp = new ColorProcessor(w, h);
        int[] RGB = new int[3];
        for (int v = 0; v < h; v++) {
            for (int u = 0; u < w; u++) {
                int idx = ip.getPixel(u, v);
                RGB[r] = Rmap[idx];
                RGB[g] = Gmap[idx];
                RGB[b] = Bmap[idx];
                cp.putPixel(u, v, RGB);
            }
        }
        ImagePlus cwin = new ImagePlus("RGB Image", cp);
        cwin.show();
    }
}

The exception is comming from this line:

 IndexColorModel icm = (IndexColorModel) ip.getColorModel();

Exception:

Exception in thread “main” java.lang.ClassCastException:
java.awt.image.DirectColorModel cannot be cast to
java.awt.image.IndexColorModel

…Any ideas? ^_^

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:52:48+00:00Added an answer on June 7, 2026 at 4:52 pm

    The error occurs beccause ip.getColorModel() does not return a IndexColorModel object, but a ColorModel object.

    To get the IndexColorModel object, you should use the following code:

    IndexColorModel icm = ip.getDefaultColorModel();
    

    That should give you a IndexColorModel, according to the ImageJ API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to obtain a variable from a different file type and import
I'm trying to obtain the values between two relational operation from an image. A(34,67,4)
Im trying to obtain javascript .js files from a website using beautifulsoup. This is
I'm trying to obtain credentials from ning network using Coldfusion 9, so first this
I am trying to obtain the value part of this syntax: [name:value] I got
I'm trying to obtain a ROI from an image using VC++ and OpenCV. I
I am trying to obtain some information from a webpage that requires login. I
I am trying to obtain something similar to the share this on... menu here
I'm trying to obtain the n-th elements from a list of tuples. I have
Using jQuery I'm trying to obtain the rotation state of an object but in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.