Write code in java to find the highest color component of a pixel

QUESTION

Can you give me an example of a code that finds the color component of a pixel which has highest value, in java.(highest valued pixel)

 

SUMMARY

An image is read (each pixel composition) and the pixel with the highest value is found and printed as output. 

 

EXPLANATION

An image is taken and is read by a file which in turn is read by an ImageIO read function. The height and width of the image are found by predefined methods of ImageIO class. Every pixel of the image is traversed and the color composition of each pixel is found by getRGB() method of color class and the highest valued pixel found and its composition is printed as output.

 

CODE

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
class Main
{
   int wi,he;
   int large=0;
   BufferedImage image;
   public Main()
   {
      try
      {
         File in=new File("black.jpg");
         image= ImageIO.read(in);
         wi= image.getWidth();
         he= image.getHeight();
         Color x=new Color(image.getRGB(0,0));
         int count = 0;
         for(int i=0; i<he; i++)
         {
            for(int j=0; j<wi; j++)
            {
               count++;
               Color c = new Color(image.getRGB(j, i));
               int temp=c.getRed()+c.getGreen()+c.getBlue();
               if(large<temp)
               {
                   large=temp;
                   x=new Color(image.getRGB(j,i));
               }
            }
         }
         System.out.println("Color composition of highest valued pixel: ("+
         x.getRed()+", "+x.getGreen()+", "+x.getBlue()+")");
      } 
      catch (Exception e) {}
   }
   static public void main(String args[]) throws Exception
   {
      Main m= new Main();
   }
}

 

OUTPUT

 

 

Also Read: What is the problem with the following call to getHashTable?

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *