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 7933223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:15:50+00:00 2026-06-03T21:15:50+00:00

Can we find which color is dominant in an image using Java, ImageMagick ,

  • 0

Can we find which color is dominant in an image using Java, ImageMagick, or JMagick?

  • 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-03T21:15:51+00:00Added an answer on June 3, 2026 at 9:15 pm

    in java iterate on each pixel and determine color

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import javax.imageio.ImageIO;
    import javax.imageio.ImageReader;
    import javax.imageio.stream.ImageInputStream;
    
    
    public class ImageTester {
    
    
        public static void main(String args[]) throws Exception {
            File file = new File("C:\\Users\\Andrew\\Desktop\\myImage.gif");
            ImageInputStream is = ImageIO.createImageInputStream(file);
            Iterator iter = ImageIO.getImageReaders(is);
    
            if (!iter.hasNext())
            {
                System.out.println("Cannot load the specified file "+ file);
                System.exit(1);
            }
            ImageReader imageReader = (ImageReader)iter.next();
            imageReader.setInput(is);
    
            BufferedImage image = imageReader.read(0);
    
            int height = image.getHeight();
            int width = image.getWidth();
    
            Map m = new HashMap();
            for(int i=0; i < width ; i++)
            {
                for(int j=0; j < height ; j++)
                {
                    int rgb = image.getRGB(i, j);
                    int[] rgbArr = getRGBArr(rgb);                
                    // Filter out grays....                
                    if (!isGray(rgbArr)) {                
                            Integer counter = (Integer) m.get(rgb);   
                            if (counter == null)
                                counter = 0;
                            counter++;                                
                            m.put(rgb, counter);                
                    }                
                }
            }        
            String colourHex = getMostCommonColour(m);
            System.out.println(colourHex);
        }
    
    
        public static String getMostCommonColour(Map map) {
            List list = new LinkedList(map.entrySet());
            Collections.sort(list, new Comparator() {
                  public int compare(Object o1, Object o2) {
                    return ((Comparable) ((Map.Entry) (o1)).getValue())
                      .compareTo(((Map.Entry) (o2)).getValue());
                  }
            });    
            Map.Entry me = (Map.Entry )list.get(list.size()-1);
            int[] rgb= getRGBArr((Integer)me.getKey());
            return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]);        
        }    
    
        public static int[] getRGBArr(int pixel) {
            int alpha = (pixel >> 24) & 0xff;
            int red = (pixel >> 16) & 0xff;
            int green = (pixel >> 8) & 0xff;
            int blue = (pixel) & 0xff;
            return new int[]{red,green,blue};
    
      }
        public static boolean isGray(int[] rgbArr) {
            int rgDiff = rgbArr[0] - rgbArr[1];
            int rbDiff = rgbArr[0] - rgbArr[2];
            // Filter out black, white and grays...... (tolerance within 10 pixels)
            int tolerance = 10;
            if (rgDiff > tolerance || rgDiff < -tolerance) 
                if (rbDiff > tolerance || rbDiff < -tolerance) { 
                    return false;
                }                 
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know if I can find an emacs color syntax configuration which resembles
I have a legacy project using Rails 2.3.5 but I can't find with which
I am using the backbone-boilerplate, which you can find here . I was wondering
Can I find out from which class a given method's implementation is inherited? e.g.
I can't find the header which has the DXTrace function in it. Which should
I want to find which tables are related to a specific table. I can
How can I query git to find out which branches contain a given commit?
How can I find all column values in a column which have trailing spaces?
1)how can i find out the Windows Installation drive in which the user is
In C# ,How can i find the number of days in a month which

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.