Skip to main content

HOW CONVERT a image (PlanarImage) in Matrix (byte[ ][ ][ ] )

3 replies [Last post]
nanr7dx
Offline
Joined: 2006-02-17
Points: 0

Hello. I need know how convert a PlanarImage in a matriz of data in byte [width] [height] [num_Bands] format.

Please Help me.

PS. :
I believe that I should use DataBuffer, SampleModel and WritableRaster for this operation, but I am not sure.

PS.2 :
If anyone knows a manual of JAI in Spanish, tell me where I found this.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aastha
Offline
Joined: 2004-04-19
Points: 0

Hi,

In order to get direct access to the pixel data in a PlanarImage, you can use one of the following PlanarImage methods:

1. getAsBufferedImage(). This will return a BufferedImage. You can then call getTile() or getRaster() on the BufferedImage to get the Raster. The Raster can then be queried using the getDataBuffer() which will give you a DataBuffer. Casting the DataBuffer to the appropriate sub-class (such as DataBufferByte) will enable access to the pixel arrays by using getData() or getBankData(). Note that the data layout in the Raster is specified by it's associated SampleModel (getSampleModel()). The color information is contained in the ColorModel associated with the BufferedImage (getColorModel())

Please note that since a BufferedImage has only a single tile/Raster, if the PlanarImage contains multiple tiles, all data will be first copied into a single tile in order to create the BufferedImage that will be returned from getAsBufferedImage()

2. PlanarImage methods getData(), getTile() can also be directly used to access the pixel data. Both of these will return a Raster, from which pixel data can be accessed as explained above.

3. If you are interested in only accessing certain pixels, the PixelAccessor class would be the way to go. Note that this may reformat the pixel data in some cases.

The (slightly outdated) Programmer's Guide (in English) is available here:

http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-...

Hope this helps,

Aastha

nanr7dx
Offline
Joined: 2006-02-17
Points: 0

Thank you aastha for you help.
According to that said, I have written the following thing:
[b][i]
public ConvertImageToMatrix {

public int [] ConvertImageToMatrix(PlanarImage pi) {

int width = pi.getWidth();
int height = pi.getHeight();
int MaxBand = pi.getNumBands();

WritableRaster wr = pi.getAsBufferedImage().getRaster();

int [] map = new int [width*height*MaxBand];
int c = 0;

for (int b = 0 ; b < MaxBand ; b++)
for (int w = 0 ; w < width ; w++)
for (int h = 0 ; h < height ; h++)
map [c++] = wr.getPixel(w,h, b);
}
}[/i][/b]

====================================================
But, I am not sure how use getPixel, overalls the third parameter.

PS. : is my solution acceptable?

aastha
Offline
Joined: 2004-04-19
Points: 0

There are many variants of getPixel() in the Raster class. The parameters that each accepts is outlined in the Javadoc for the Raster class, so please take a look at the Javadoc for further details. There is no version of getPixel that takes 3 ints as arguments.

Note that getPixel will return the samples/bands for only one pixel. If you need to access multiple pixels, then you might as well use the Raster.getPixels() method, which will be more efficient.

Since these questions are related to the Java2D API, you might find it useful to read the Programmer's Guide to the JavaTM 2D API at

http://java.sun.com/j2se/1.4.2/docs/guide/2d/spec/j2d-bookTOC.html

Aastha