Results 1 to 1 of 1
Thread: Convert a color to gray
- 04-03-2012, 12:27 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 45
- Rep Power
- 0
Convert a color to gray
I was trying to predict what a pixel's color would be after drawing the image in a gray colorspace, but it's nowhere near my prediction. The formula I was taught was 0.299R + 0.587G + 0.114B. I grabbed a pixel from a BufferedImage that was obtained from a screen capture. Its RGB values were (125, 155, 227). Using the preceding formula, I anticipated that the pixel in grayspace would be 154. But when I examined the pixel after drawing to BufferedImage.TYPE_BYTE_GRAY was 204. What am I doing wrong?
Java Code:import java.awt.*; import java.awt.image.*; public class ScreenCapture { private Robot r; private Rectangle screenRect; public ScreenCapture() throws AWTException { this.r = new Robot(); Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize(); this.screenRect = new Rectangle(0,0,(int)sSize.getWidth(),(int)sSize.getHeight()); } public void snap(int x, int y) { BufferedImage bi = this.r.createScreenCapture(this.screenRect); BufferedImage gbi = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_BYTE_GRAY); Graphics2D g2d = gbi.createGraphics(); g2d.drawImage(bi, 0, 0, bi.getWidth(), bi.getHeight(), null); printColor(bi.getRGB(x, y)); printColor(gbi.getRGB(x, y)); } private void printColor(int pixColor) { System.out.print(pixColor + ": "); System.out.print(((pixColor >> 16) & 0xFF) + " "); System.out.print(((pixColor >> 8) & 0xFF) + " "); System.out.println((pixColor & 0xFF) + ""); } public static void main(String[] args) { ScreenCapture sc = null; if (args.length == 2) { try { sc = new ScreenCapture(); } catch (AWTException awte) { System.err.println("Could not create robot"); } } if (sc != null) { int x = -1; int y = -1; try { x = Integer.parseInt(args[0]); y = Integer.parseInt(args[1]); } catch (ParseException pe) { System.err.println(args[0] + " or " + args[1] + " not integers"); } if (x >= 0 && y >= 0) { sc.snap(x, y); } } } }Last edited by nwboy74; 04-03-2012 at 12:32 AM.
Similar Threads
-
Convert image to RGB based on a color ramp
By forumuser in forum Java 2DReplies: 0Last Post: 12-14-2011, 06:57 PM -
Java applet on web showing gray box
By TBS in forum Java AppletsReplies: 0Last Post: 12-02-2011, 07:16 PM -
Convert String To Advanced Color
By anthropamorphic in forum New To JavaReplies: 6Last Post: 09-21-2011, 04:10 AM -
what does the gray (x) means?
By pandaman0212 in forum EclipseReplies: 3Last Post: 06-28-2010, 03:24 PM -
error reported when binarization of gray image
By Mazharul in forum Java 2DReplies: 1Last Post: 09-18-2008, 09:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks