Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2009
    Posts
    34
    Rep Power
    0

    Default 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.

    Java 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;
             
             
             
        }
    Bellow method is used to save the image to save the converted image to desktop. Please help me with the code? Thank you in advaned

    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();
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    "There is an error in it."
    ?

    Is this a game of "guess the error"?

  3. #3
    Join Date
    Mar 2009
    Posts
    34
    Rep Power
    0

    Default

    Yaa may b.. it dsnt work with me either.. i wanna fix it can u help me....

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    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?

  5. #5
    Join Date
    Mar 2009
    Posts
    34
    Rep Power
    0

    Default

    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....
    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;
     }
    So i want to do it using other way round....

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    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?

  7. #7
    Join Date
    Mar 2009
    Posts
    34
    Rep Power
    0

    Default

    This is for final year project.... They give high marks as far as we stick away from APIs..

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    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?

  9. #9
    Join Date
    Mar 2009
    Posts
    34
    Rep Power
    0

    Default

    hmmmm....... :( :( :( bt i m nt sure someone will help me herE......

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

Similar Threads

  1. Converting Image to byte array[] ?
    By afflictedd2 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-11-2009, 11:33 PM
  2. Replies: 3
    Last Post: 03-21-2009, 09:34 PM
  3. error reported when binarization of gray image
    By Mazharul in forum Java 2D
    Replies: 1
    Last Post: 09-18-2008, 09:45 AM
  4. Replies: 5
    Last Post: 08-29-2008, 04:59 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 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
  •