Results 1 to 5 of 5
- 01-06-2011, 12:22 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
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? }
- 01-06-2011, 12:53 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
Update the array for all gray values in your image and voila.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'.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-06-2011, 01:07 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
sir, i have the code as follow:
is it the one that i need?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; }
- 01-06-2011, 01:39 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
ok i got it now :)
- 02-25-2011, 01:32 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Count the frequency of the word in a text file instead of a sentence.
By bMorgan in forum New To JavaReplies: 3Last Post: 11-09-2010, 12:10 AM -
Working With Frequency Arrays...
By Hexbomber in forum New To JavaReplies: 3Last Post: 12-19-2009, 07:18 AM -
Sort Frequency
By ScaryJello in forum New To JavaReplies: 7Last Post: 03-26-2009, 12:47 AM -
Word Frequency
By capu in forum Advanced JavaReplies: 2Last Post: 10-09-2008, 02:03 PM -
Frequency Counter
By justlearning in forum New To JavaReplies: 0Last Post: 05-07-2008, 10:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks