Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

how to find the number of unique colors in an image using java

loka_karthigaloka_karthiga Posts: 2Member
Explain me how to find get an image and store it in an picture box and too explain me how to create an picture box using java... Please send this to my mail id [color=Green][size=4]lokakartiga@gmail.com[/size][/color]. since i dont know how to find the explain answer in this id......

Comments

  • Josh CodeJosh Code Posts: 675Member
    Following here is some code for counting the number of unique colours in an image. It is an application that loads an image from a file and prints out how many unique colours it has.

    Displaying it in a "picture box" is something not covered by the code. If you want to display it in a graphical user interface, you'll have to look into the paint method and the Graphics class.

    The countUniqueColours method creates a BufferedImage with a specific pixel format, TYPE_INT_ARGB. This format involves each pixel being represented by a separate int value.

    A HashSet is used to collect all the unique colours. A HashSet is appropriate and efficient for the job because it averages O(1) access time for the contains method and for adding. Using an array or linked list wouldn't have nearly the speed for checking if another pixel has a unique colour.


    [code]
    import java.awt.*; // Image
    import java.awt.image.*; // BufferedImage

    import java.io.*; // File and IOException
    import javax.imageio.*; // ImageIO

    import java.util.*;

    public class ImageLoader
    {

    public static BufferedImage loadImageFrom(File f) throws IOException
    {
    return ImageIO.read(f);
    }

    public static int countUniqueColours(BufferedImage img)
    {
    BufferedImage buf = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_INT_ARGB);
    int pixels[] = ((DataBufferInt)(buf).getRaster().getDataBuffer()).getData();
    Set coloursFound = new HashSet();
    int width = buf.getWidth();

    buf.getGraphics().drawImage(img,0,0,null);

    for (int y=0;y<buf.getHeight();y++)
    for (int x=0;x<width;x++)
    {
    int index = y*width+x;
    int c = pixels[index];

    if (!coloursFound.contains(new Integer(c)))
    {
    coloursFound.add(new Integer(c));
    }
    }

    return coloursFound.size();
    }

    public static void main(String a[])
    {
    if (a.length<1)
    {
    System.out.println("Give the name of an image to load.");
    return;
    }

    try
    {

    BufferedImage img = loadImageFrom(new File(a[0]));

    // do anything you want with the image.

    System.out.println("The image contains: "+countUniqueColours(img)+" unique pixel colours.");

    }
    catch (IOException ioe)
    {
    ioe.printStackTrace();
    System.out.println("Check that '"+a[0]
    +"' exists and is a valid image file.");
    }
    }
    }
    [/code]
Sign In or Register to comment.