# Thread: Create Image from Arbitrary 2D Matrix of Data

1. Member
Join Date
Feb 2011
Posts
27
Rep Power
0

## Create Image from Arbitrary 2D Matrix of Data

Hi,

I have a 2D matrix of floats with each value corresponding to certain data value. The matrix is very large ~4000x4000 and I would like to display this matrix as an image, similar to MATLABs imagesc. I think I can do this by first finding the max and min of the image and scaling each element between 0 and 1, but I want to keep the original data values, and I feel there should be a way to keep the original data matrix and display it. Any help here would be greatly appreciated.

Thanks,
Dave

kind regards,

Jos

3. Member
Join Date
Feb 2011
Posts
27
Rep Power
0
I should have mentioned that I have been trying that. I think a problem is that I don't think I know how to use BufferedImage correctly. For example, one data set I have has a range of floats from 0-150. I create a bufferedimage like so:
Java Code:
`BufferedImage image = new BufferedImage(4000, 4000, BufferedImage.TYPE_INT_RGB`
I then pull the data from a .dat file and load the image like so:
Java Code:
`image.setRGB(ii,jj,(int) bf.order(ByteOrder.LITTLE_ENDIAN).getFloat())`
Which gives me an image but only in the blue range. I want the data to be imaged across red, green, and blue, while still preserving the original data value.

Hope this provides a little motivation. Thanks

4. A BufferedImage internally uses ints for its pixel colors; you are just casting a float value (in the range [0,150]) to an int and use that for a color value; an int is of the form ARGB (four bytes), representing the alpha value, red, green and blue (as its least significant byte); that's why you only get blue-ish values in your image. Only if you're able to find a bijective function f(float) -> int there is no need to separately keep those floating point values because you can always get them back again from the BufferedImage; otherwise you're out of luck. b.t.w. do those floating point values represent color values or just scales of grey? If the latter, a bit of scaling can serve as your bijective function.

kind regards,

Jos

5. Create an array of 151 RGB/Color elements and index it with the numeric values

6. Member
Join Date
Feb 2011
Posts
27
Rep Power
0
Norm, the problem with that is that the range [0,150] are not integers, they are rational floats so I can't simply make a mapping like that.

Jos, So are you saying map my range [0-150] linearly to [0-16777215]? This might be suitable for my problem, but I just feel like there is a more elegant and efficient solution, as I feel like this is not a unique problem to have.

I was looking into the WritableRaster class that accepts setPixels of float[]. Do you know more about this?

7. Are there more than 151 different colors you want to display? If so, how many?
Map your numbers to the range of the total different colors you want to show.

8. Originally Posted by dvreed77
Jos, So are you saying map my range [0-150] linearly to [0-16777215]? This might be suitable for my problem, but I just feel like there is a more elegant and efficient solution, as I feel like this is not a unique problem to have.

I was looking into the WritableRaster class that accepts setPixels of float[]. Do you know more about this?
I guess it should be possible; I never worked with (Writable)Rasters but according to the API documentation it should be possible; you have to experiment a bit with those DataBuffers, WritableRasters and BufferedImages ... best of luck.

kind regards,

Jos

9. Member
Join Date
Feb 2011
Posts
27
Rep Power
0
Norm,

I guess I may need to be a bit more flexible and I can do something like 150 colors. My next problem is the actual mapping. I have a 150 x 3 matrix of color mappings with each row representing the RGB component, [0-1] for each entry. I think I need to somehow use a ColorModel to efficiently do this mapping. Can you provide any advice?

10. I thought the problem was how to show a 4000X4000 grid with each square having one of 151 different colors.
So given an array of 151 different colors, use the contents of each x,y element to chose a color for that x,y square.

How do the values of the elements relate to the color you want to use to show it in a square?

11. Member
Join Date
Feb 2011
Posts
27
Rep Power
0
Im pulling this colormap from MATLAB. I cant tell for sure but it seems to be the same idea as color model in Java. For the basic color model the lowest color is blue and the highest color is red. The colormap transitions linearly from blue through the rainbow to red. There are also color maps that may transition non-linearly. I suppose I could loop over each pixel everytime I want to change the colormodel, but it seems I shouldn't have to do this.

12. Sorry, I'm not following your discussion about colormaps. I was simply using 151 different colors.

13. Member
Join Date
Feb 2011
Posts
27
Rep Power
0
Ok, so using 151 colors may I ask how you would select the colors?

14. Yes, that is the problem. How to select the colors. If you could provide an image of the band of colors from min to max, I have a program that will extract the RGB at mouse positions on it. With a bit of a wiggle, there should be a way to get 150 colors.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•