Results 1 to 5 of 5
  1. #1
    meth is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default how to count frequency of the color?

    hi, i want to convert a color image to a greyscale image by counting the frequency of the color (0-255) and put the frequency in an array but im stuck with putting it into the array. can anyone pls help me what do i have to do next? thx in advance.


    Java Code:
    public class Freq extends javax.swing.JFrame {
        Image inputimage, output_rgb;
        BufferedImage buff_in, buff_grey;
    
        int counter;
        int width = 256; //initial image width pixel
        int height = 256; //initial image height pixel
    
        File file_input, file_output;
    
        JFileChooser fileopen = new JFileChooser();
        JFileChooser filesave= new JFileChooser();
        FileFilter filter = new FileNameExtensionFilter("image files","jpg");                       
    
        private void openbuttonActionPerformed(<em>java</em>.awt.event.ActionEvent evt) {                                           
            fileopen.addChoosableFileFilter(filter);
    
            int return_value = fileopen.showOpenDialog(this);
    
            if (return_value == JFileChooser.APPROVE_OPTION)
            {
                file_input = fileopen.getSelectedFile();
                System.out.println(file_input);
            }
            try
            {
                inputimage = ImageIO.read(file_input);
                inputimage = inputimage.getScaledInstance(256,256,Image.SCALE_SMOOTH);
                repaint();
            } 
            catch(IOException e)
            {
                System.out.println("ERROR");
            }
    }                                          
    
        public void paint(Graphics g)
        {
            g.drawImage(inputimage,100,100,this);
        }                                    
    
        private void processActionPerformed(<em>java</em>.awt.event.ActionEvent evt) {                                        
            width = inputimage.getWidth(null);
            height = inputimage.getHeight(null);
            buff_grey = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
            Graphics gr = buff_grey.getGraphics();
            gr.drawImage(inputimage,0,0,null);
            gr.dispose();
    
            process_input = new int[width * height];
            process_grey = new int[width * height];
            counter = 0;
            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    int clr = buff_grey.getRGB(j,i);
                    int red = (clr & 0x00ff0000) >> 16;
                    int green = (clr & 0x0000ff00) >> 8;
                    int blue = clr & 0x000000ff;
                    int grey = (red + green + blue)/3;
                    process_input[counter] = clr;
                    process_grey[counter] = grey;
                    counter++;
                }
            }
            repaint();
    
    //is this where i have to write the array for the frequency?
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default

    I don't see the problem: if you have a gray value in the range [0, 255] an array of 255 elements can store the frequencies:

    Java Code:
    int[] freqs[]= new int[256];
    ...
    int gray= ...; // any value in the range [0, 255]
    ...
    freqs[gray]++; // update the frequency for gray value 'gray'.
    Update the array for all gray values in your image and voila.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    meth is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    sir, i have the code as follow:

    Java Code:
    public static BufferedImage equalizeGrayScale(BufferedImage imageIn)
    	{
    		BufferedImage imageOut = new BufferedImage(256,256, BufferedImage.TYPE_BYTE_GRAY);
    		
    		int[] frequency = new int[256];
    		int maxIntensity = 255;
    		
    		for(int x = 0; x < imageIn.getWidth(); x++)
    		{
    			for(int y = 0; y <imageIn.getHeight(); y++)
    			{
    				double[] pixel = new double[1];
    				pixel = imageIn.getRaster().getPixel(x, y, pixel);
    				frequency[(int)pixel[0]]++;
    			}
    		}
    		
    		int sum = 0;
    		for (int x = 0; x < frequency.length; x++)
    		{
    			sum += frequency[x];
    			frequency[x] = sum * maxIntensity /(256 * 256);
    		}
    		
    		for (int x = 0; x <imageIn.getWidth(); x++)
    		{
    			for (int y = 0; y < imageIn.getHeight(); y++)
    			{
    				double[] pixel = new double[1];
    				pixel = imageIn.getRaster().getPixel(x,y,pixel);
    				pixel[0] = frequency[(int)pixel[0]];
    				imageOut.getRaster().setPixel(x, y, pixel);
    			}
    		} 
    	
    		return imageOut;
    	}
    is it the one that i need?

  4. #4
    meth is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    ok i got it now :)

  5. #5
    kzzaman is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default

    I can't understand the above code.plz define how to count the frequency in a image...

Similar Threads

  1. Replies: 3
    Last Post: 11-09-2010, 01:10 AM
  2. Working With Frequency Arrays...
    By Hexbomber in forum New To Java
    Replies: 3
    Last Post: 12-19-2009, 08:18 AM
  3. Sort Frequency
    By ScaryJello in forum New To Java
    Replies: 7
    Last Post: 03-26-2009, 01:47 AM
  4. Word Frequency
    By capu in forum Advanced Java
    Replies: 2
    Last Post: 10-09-2008, 02:03 PM
  5. Frequency Counter
    By justlearning in forum New To Java
    Replies: 0
    Last Post: 05-07-2008, 10:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •