Results 1 to 14 of 14
  1. #1
    dvreed77 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Does a BufferedImage help you out?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dvreed77 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

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

  6. #6
    dvreed77 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    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. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by dvreed77 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    dvreed77 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    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. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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. #11
    dvreed77 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    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. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

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

  13. #13
    dvreed77 is offline Member
    Join Date
    Feb 2011
    Posts
    27
    Rep Power
    0

    Default

    Ok, so using 151 colors may I ask how you would select the colors?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    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.

Similar Threads

  1. Matrix Data Assignment
    By sehudson in forum New To Java
    Replies: 5
    Last Post: 03-01-2011, 07:45 AM
  2. Matrix Data Type
    By fraB3422 in forum New To Java
    Replies: 7
    Last Post: 02-27-2011, 02:08 AM
  3. Arbitrary number of objects, each with a unique name.
    By providence in forum New To Java
    Replies: 8
    Last Post: 02-23-2011, 01:12 AM
  4. gradients between two arbitrary colors
    By falkon114 in forum New To Java
    Replies: 4
    Last Post: 02-09-2011, 05:08 PM
  5. Replies: 2
    Last Post: 12-12-2007, 05:01 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
  •