Skip to content
Advertisement

2d double array to image

I’m currently working on a simulation with continuous agents, which leave a pheromone trail on a 2d double array. The pheromone trails need to be on a 2d array because of a diffusion with a mean filter that needs to be performed. Ultimately, I need to visualise the agents and the pheromone trails, by transforming the double array directly into an awt.Image.

Advertisement

Answer

Basically create a BufferedImage, as suggested by Gilbert Le Blanc and use its setRGB method to set the pixels (or get its Graphics to draw on it).

Example, assuming values are between 0.0 and 1.0, converting to gray:

JavaScript

The doubleToRGB can be changed to use more complicated mapping from value to color.

Example red for lower values, blue for higher:

JavaScript

Note: posted code is just to show the idea – can/must be optimized – missing error checking

Note 2: posted mapping to gray is not necessarily the best calculation regarding our perception

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement