+ Reply to Thread
Results 1 to 10 of 10

Thread: array 2 image

  1. #1
    StarIsFar is offline A Toddler - Don't be Fooled! StarIsFar is on a distinguished road
    Join Date
    Dec 2005
    Posts
    45
    Rep Power
    0

    array 2 image

    Hi everybody,

    if I have 2D array of integers. How to convert it to binary image ?

    thanx in advance for any help.

  2. #2
    cpf's Avatar
    cpf
    cpf is offline Hiiii cpf is on a distinguished road
    Join Date
    Jun 2005
    Location
    Box 12 Site 5 RR1
    Posts
    2,829
    Rep Power
    11

    Re: array 2 image

    such as 1=white, 0=black? Could you clarify your problem?

    Quote Calamitie originally posted:
    Calamitie says:
    Noodles is a lossy teleportation framework for Noodle objects

  3. #3
    StarIsFar is offline A Toddler - Don't be Fooled! StarIsFar is on a distinguished road
    Join Date
    Dec 2005
    Posts
    45
    Rep Power
    0

    Re: array 2 image

    Hi

    I grapped an image using "grapping pixel" into an array, then i convert this array into 2D array to work with it and modified it, now I want to convert back the 2d array into an image. I dont know how to do that??!!

  4. #4
    cpf's Avatar
    cpf
    cpf is offline Hiiii cpf is on a distinguished road
    Join Date
    Jun 2005
    Location
    Box 12 Site 5 RR1
    Posts
    2,829
    Rep Power
    11

    Re: array 2 image

    Just guessing, (doesn't know java), but is there a SetPixel method? If so, just use a loop to set the pixels from the values in your 2d array.

    Quote Calamitie originally posted:
    Calamitie says:
    Noodles is a lossy teleportation framework for Noodle objects

  5. #5
    hot_cakes's Avatar
    hot_cakes is offline Moderat0r!!1 hot_cakes will become famous soon enough
    Join Date
    Aug 2005
    Location
    Bristol, UK
    Age
    31
    Posts
    2,913
    Rep Power
    10

    Re: array 2 image

    Please be even more specific. The more detail you provide, the easier it is for people to help!

    cpf asked a specific question - what format is the raster? You say "binary image" but clarification is needed as to what you mean by this. Again, do you mean a 1-bit-per-pixel image?

    Also, do you need to write the image to disk immediately, or do you want to store it inside some image-like object provided by the Java standard API? The process of "converting an array to an image" could mean either of these things.

    Also, could you please define "grapping pixel". Was the array formed by reading a file with a custom written parser, or did you use a pre-canned Java function to load the image from some source? If the array was created by polling the interface of an image-like object, does the same class not provide facilities for doing the reverse?

    As you can see, I'm merely speculating as there's so little to go on.

    Edd

  6. #6
    StarIsFar is offline A Toddler - Don't be Fooled! StarIsFar is on a distinguished road
    Join Date
    Dec 2005
    Posts
    45
    Rep Power
    0

    Re: array 2 image

    Quote hot_cakes originally posted: View Post
    Please be even more specific. The more detail you provide, the easier it is for people to help!

    Ok
    Quote hot_cakes originally posted: View Post
    cpf asked a specific question - what format is the raster? You say "binary image" but clarification is needed as to what you mean by this. Again, do you mean a 1-bit-per-pixel image?
    Yes 1-bit-per-pixel image.

    Quote hot_cakes originally posted: View Post
    Also, do you need to write the image to disk immediately, or do you want to store it inside some image-like object provided by the Java standard API? The process of "converting an array to an image" could mean either of these things.
    I want to write the image to disk immediately.

    Quote hot_cakes originally posted: View Post
    Also, could you please define "grapping pixel". Was the array formed by reading a file with a custom written parser, or did you use a pre-canned Java function to load the image from some source? If the array was created by polling the interface of an image-like object, does the same class not provide facilities for doing the reverse?

    Edd
    I didn't get that really but here is the code where I read an image then convert it to an array.
    Code:
       
                       // Read histogram image in image of type  Image 
                       Image Histimage =  Toolkit.getDefaultToolkit().createImage(bi[0].getSource());
                      
                       
                       // then get the array in histogram [] []
                       // First get the Width and Hieght of origenal histogram
                       int ImgW = Histimage.getWidth(this);
                       int ImgH = Histimage.getHeight(this);
                       histogram  = new int[ImgW*ImgH];
                        
                       // Store pixels in histogram array  
                       PixelGrabber pg = new PixelGrabber(Histimage, 0, 0, ImgW, ImgH,histogram, 0, ImgW);
    		   try {
    			pg.grabPixels();
    		      }
    		   catch(InterruptedException e) {
    			System.err.println("Grabbing Interrupted");
    			e.printStackTrace();
    		  }  
                       
                      // Now Convert the one-Dim histogram array
                     // into two-Dim  array to process first column
                      
                      twoDim = new int[ImgH][ImgW];
                      System.out.println(ImgH);
                      System.out.println(ImgW);
                      
                      for (int saNum = 0; saNum < ImgH; ++saNum)
                      {
                        for (int elNum = 0; elNum < ImgW; ++elNum)
                            
                        {
                           twoDim[saNum][elNum] = histogram[ImgW * saNum +elNum];
                        }
                       }

  7. #7
    hot_cakes's Avatar
    hot_cakes is offline Moderat0r!!1 hot_cakes will become famous soon enough
    Join Date
    Aug 2005
    Location
    Bristol, UK
    Age
    31
    Posts
    2,913
    Rep Power
    10

    Re: array 2 image

    In which file format are you wanting to save the image?

    First you need to search for a class or set of classes that allow you to save the kind of image file you require. Then you need to find out how to "fill in" the object of the appropriate type, ready to save it.

    For example, if you want to save the image as a BMP file (which as far as I recall supports 1 bit-per-pixel images), Google for "java save BMP". You get a load of example code to look at.

    However, it might be easier to convert the image to grayscale (with only pure black and white pixels). The image file will be a lot bigger, but writing the image will be easier and you'll have a larger number of formats to choose from.

    Edd

  8. #8
    StarIsFar is offline A Toddler - Don't be Fooled! StarIsFar is on a distinguished road
    Join Date
    Dec 2005
    Posts
    45
    Rep Power
    0

    Re: array 2 image

    Hi edd,

    I think u still not get my point. I have no problem in writing the image to a file. My problem is how to write the modified array to the image. If u see the code here:
    Code:
    // Now Convert the one-Dim histogram array
    // into two-Dim  array to process first column
                      
                      twoDim = new int[ImgH][ImgW];
                      System.out.println(ImgH);
                      System.out.println(ImgW);
                      
                      for (int saNum = 0; saNum < ImgH; ++saNum)
                      {
                        for (int elNum = 0; elNum < ImgW; ++elNum)
                            
                        {
                           twoDim[saNum][elNum] = histogram[ImgW * saNum +elNum];
                        }
                       }
    I stored the image pixels in an array so I can easily modify the pixels. Now after modifing the array I want to convert it back to an image. Actullay I did the following:
    Code:
    // Convert two dim array to image
     BufferedImage nimage = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
     Graphics2D big = nimage.createGraphics();
     big.drawImage(nimage,0,0,this);
     saveImageToFile( nimage,"newimage.jpg");
    BUT I DONT KNOW HOW TO WRITE THE ARRAY TO "nimage" ???

  9. #9
    hot_cakes's Avatar
    hot_cakes is offline Moderat0r!!1 hot_cakes will become famous soon enough
    Join Date
    Aug 2005
    Location
    Bristol, UK
    Age
    31
    Posts
    2,913
    Rep Power
    10

    Re: array 2 image

    Ahh I see. So you are actually wanting to ask the question "How do I draw to a BufferedImage object?", is that correct?

    Code:
    WritableRaster raster = nimage.getRaster();
    raster.setPixel(x, y, someColour); // as necessary
    
    // now get the Graphics2D object from nimage as before and write to disk
    Edd

  10. #10
    StarIsFar is offline A Toddler - Don't be Fooled! StarIsFar is on a distinguished road
    Join Date
    Dec 2005
    Posts
    45
    Rep Power
    0

    Re: array 2 image

    Hello

    Here is the solution
    Code:
    /**
         * Convert a two dimensional array of ints to a BufferedImage.
         * Each int represents a pixel of a certain colour.
         * The ints are expected to be calculated using
         * int color = (255 << 24 ) | (red << 16 ) | (green <<  8) | blue;
         * where red,green and blue are values in [0-255]
         * 
         * @param rgbValue The two dimensional int array representing the pixels
         * @return A BufferedImage with all the pixels drawn
         */
        public static BufferedImage convertRGBImage(int[][] rgbValue){
            int height = rgbValue.length;
            int width = rgbValue[0].length;
            
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            //we either have to loop through all values, or convert to 1-d array
            for(int y=0; y< height; y++){
                for(int x=0; x< width; x++){
                    bufferedImage.setRGB(x,y,rgbValue[y][x]);  
                }
            }
            return bufferedImage;  
        }
    Thanx.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Image into subimages
    By StarIsFar in forum Java Programming
    Replies: 5
    Last Post: 04-09-2006, 04:05 PM
  2. "Sliding" an array
    By NedreN in forum PHP Scripting
    Replies: 8
    Last Post: 10-22-2005, 08:02 PM
  3. [Solved] Sorting multidimensional array
    By [Michael] in forum PHP Scripting
    Replies: 2
    Last Post: 08-20-2005, 06:07 AM
  4. Checking source codes of image, audio and video files
    By onauc in forum C and C++ Programming
    Replies: 7
    Last Post: 06-22-2005, 12:18 AM
  5. How to Link a Image
    By Helloadam in forum HTML & CSS Articles
    Replies: 2
    Last Post: 09-08-2004, 03:44 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