Results 1 to 10 of 10
- 12-13-2009, 04:15 AM #1
Error in converting colour image to gray scale
I wrote bellow code is used to convert a color image to gray scale. There is an error in it.Can somebody help me with the code to fix it. Please note that i do not need to use any JAVA API to convert the color image to grey scale.
Bellow method is used to save the image to save the converted image to desktop. Please help me with the code? Thank you in advanedJava Code:public BufferedImage grayScale(BufferedImage img){ Color clr; int r, g, b; float grey; for (int ix = 0; ix<img.getWidth();ix++){ for(int iy = 0;iy<img.getHeight();iy++){ clr = Color.decode(Integer.toString(img.getRGB(ix, iy))); r = clr.getRed(); b = clr.getBlue(); g = clr.getGreen(); grey = (float) (0.299 * r + 0.587 * g + 0.144 * b + 0.5); clr = new Color(grey, grey, grey); img.setRGB(ix, iy,clr.getRGB()); // 0.299*r + 0.587*g + 0.144*b + 0.5 } } grayScale = img; return img; }
Java Code:ProcessingImages fr = new ProcessingImages(); try{ javax.imageio.ImageIO.write(fr.grayScale(ProcessingImages.tmpSideA), "png", new File("C:\\Users\\Chathura Ekanayake\\Desktop\\GrayScale.PNG")); } catch(Exception e){ e.printStackTrace(); }
- 12-14-2009, 10:21 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
"There is an error in it."
?
Is this a game of "guess the error"?
- 12-14-2009, 01:49 PM #3
Yaa may b.. it dsnt work with me either.. i wanna fix it can u help me....
- 12-14-2009, 02:00 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
This is a bit like going to a mechanic with your car and saying "it doesn't work"...I am not spending my time looking at the engine only for you to tell me you meant the car stereo.
So, in case I was being too subtle, what does "(t)here is an error in it" mean?
- 12-14-2009, 02:08 PM #5
ok.... i got what u said.... hmmmm well juz forget about what i wrote here.... Mt task is to convert color image to gray scale image..... I know bellow code will do what i required.. Bt i cant use APIs to do this....
So i want to do it using other way round....Java Code:public static void grayScale(BufferedImage colorImage){ BufferedImage image = new BufferedImage(colorImage.getWidth(), colorImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY); Graphics g = image.getGraphics(); g.drawImage(colorImage, 0, 0, null); g.dispose(); grayScale = image; }
- 12-14-2009, 02:21 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
OK. Well, hopefully someone'll come along here who knows the nuts and bolts of images.
Me? I use the API, because that's what it's there for.
Is this an exercise then?
- 12-14-2009, 02:24 PM #7
This is for final year project.... They give high marks as far as we stick away from APIs..
- 12-14-2009, 03:00 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Thought it had to be something like that.
I don't know the mechanics of converting from a colour to grey.
I would have said what you had above, essentially converting each pixel, was the answer...but from what you've said, that doesn't work for some reason?
- 12-14-2009, 03:03 PM #9
hmmmm....... :( :( :( bt i m nt sure someone will help me herE......
- 12-14-2009, 04:30 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Converting Image to byte array[] ?
By afflictedd2 in forum CLDC and MIDPReplies: 0Last Post: 04-11-2009, 11:33 PM -
How to Scale an Image with the help of Affine Transformation?
By sayan751 in forum AWT / SwingReplies: 3Last Post: 03-21-2009, 09:34 PM -
error reported when binarization of gray image
By Mazharul in forum Java 2DReplies: 1Last Post: 09-18-2008, 09:45 AM -
Binarizarion of gray image/ArrayIndexOutOfBound error reported
By Mazharul in forum Java 2DReplies: 5Last Post: 08-29-2008, 04:59 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks