Hi everybody,
if I have 2D array of integers. How to convert it to binary image ?
thanx in advance for any help.
Hi everybody,
if I have 2D array of integers. How to convert it to binary image ?
thanx in advance for any help.
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??!!
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
Ok
Yes 1-bit-per-pixel image.
I want to write the image to disk immediately.
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]; } }
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
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:
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:// 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]; } }
BUT I DONT KNOW HOW TO WRITE THE ARRAY TO "nimage" ???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");
Ahh I see. So you are actually wanting to ask the question "How do I draw to a BufferedImage object?", is that correct?
EddCode:WritableRaster raster = nimage.getRaster(); raster.setPixel(x, y, someColour); // as necessary // now get the Graphics2D object from nimage as before and write to disk
Hello
Here is the solution
Thanx.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; }
There are currently 1 users browsing this thread. (0 members and 1 guests)