Results 1 to 14 of 14
 06122011, 03:43 PM #1Member
 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
 06122011, 04:01 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Does a BufferedImage help you out?
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 06122011, 04:24 PM #3Member
 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 0150. I create a bufferedimage like so:
Java Code:BufferedImage image = new BufferedImage(4000, 4000, BufferedImage.TYPE_INT_RGB
Java Code:image.setRGB(ii,jj,(int) bf.order(ByteOrder.LITTLE_ENDIAN).getFloat())
Hope this provides a little motivation. Thanks
 06122011, 04:37 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
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 blueish 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,
JosBuild a wall around Donald Trump; I'll pay for it.
 06122011, 04:42 PM #5
Create an array of 151 RGB/Color elements and index it with the numeric values
 06122011, 05:58 PM #6Member
 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 [0150] linearly to [016777215]? 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?
 06122011, 06:05 PM #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.
 06122011, 08:07 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Build a wall around Donald Trump; I'll pay for it.
 06122011, 08:09 PM #9Member
 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, [01] for each entry. I think I need to somehow use a ColorModel to efficiently do this mapping. Can you provide any advice?
 06122011, 08:13 PM #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?
 06122011, 08:35 PM #11Member
 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 nonlinearly. 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.
 06122011, 08:43 PM #12
Sorry, I'm not following your discussion about colormaps. I was simply using 151 different colors.
 06122011, 09:37 PM #13Member
 Join Date
 Feb 2011
 Posts
 27
 Rep Power
 0
Ok, so using 151 colors may I ask how you would select the colors?
 06122011, 09:42 PM #14
Similar Threads

Matrix Data Assignment
By sehudson in forum New To JavaReplies: 5Last Post: 03012011, 08:45 AM 
Matrix Data Type
By fraB3422 in forum New To JavaReplies: 7Last Post: 02272011, 03:08 AM 
Arbitrary number of objects, each with a unique name.
By providence in forum New To JavaReplies: 8Last Post: 02232011, 02:12 AM 
gradients between two arbitrary colors
By falkon114 in forum New To JavaReplies: 4Last Post: 02092011, 06:08 PM 
Create a VeryLong class that will store an integer of arbitrary length.
By hey in forum New To JavaReplies: 2Last Post: 12122007, 06:01 PM
Bookmarks