parent
d92c5cceb8
commit
214bc943ee
@ -1,104 +0,0 @@ |
|||||||
/** |
|
||||||
* |
|
||||||
*/ |
|
||||||
package neural; |
|
||||||
|
|
||||||
import java.awt.Color; |
|
||||||
import java.awt.Graphics2D; |
|
||||||
import java.awt.image.BufferedImage; |
|
||||||
import java.io.File; |
|
||||||
import java.io.IOException; |
|
||||||
import java.util.Random; |
|
||||||
|
|
||||||
import javax.imageio.ImageIO; |
|
||||||
|
|
||||||
public class ColorMap { |
|
||||||
|
|
||||||
/** |
|
||||||
* @param data |
|
||||||
* @param pixelsWidth |
|
||||||
* @param pixelsHeight |
|
||||||
* @return |
|
||||||
* @throws Exception |
|
||||||
* @see https://josetips.wordpress.com/category/java/
|
|
||||||
*/ |
|
||||||
public static BufferedImage generateImage(double[][] data, int pixelsWidth, int pixelsHeight) throws Exception { |
|
||||||
|
|
||||||
double x = 0, y = 0; |
|
||||||
BufferedImage bi = new BufferedImage(pixelsWidth, pixelsHeight, BufferedImage.TYPE_3BYTE_BGR); |
|
||||||
Graphics2D g = (Graphics2D) bi.getGraphics(); |
|
||||||
|
|
||||||
int w = data[0].length; |
|
||||||
int l = data.length; |
|
||||||
double Dy = (double) pixelsHeight / (double) l; |
|
||||||
double Dx = (double) pixelsWidth / (double) w; |
|
||||||
int height = (int) Math.ceil(Dy); |
|
||||||
int width = (int) Math.ceil(Dx); |
|
||||||
int gap = (int) Math.ceil(width / 4); |
|
||||||
|
|
||||||
double max = -Double.MAX_VALUE; |
|
||||||
double min = Double.MAX_VALUE; |
|
||||||
for (int i = 0; i < l; i++) { |
|
||||||
for (int j = 0; j < w; j++) { |
|
||||||
max = data[i][j]; |
|
||||||
if (data[i][j] < min) { |
|
||||||
min = data[i][j]; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
float fcolor; |
|
||||||
float saturation = (float) 0.9; |
|
||||||
float bright = (float) 0.7; |
|
||||||
|
|
||||||
g.setColor(Color.WHITE); |
|
||||||
g.fillRect(0, 0, pixelsWidth, pixelsHeight); |
|
||||||
for (int i = 0; i < l; i++) { |
|
||||||
for (int j = 0; j < w; j++) { |
|
||||||
fcolor = (float) ((data[i][j] - min) / (max - min)); |
|
||||||
float hue = fcolor; |
|
||||||
g.setColor(Color.getHSBColor((float) (hue * 0.7), saturation, bright)); |
|
||||||
g.fillRect((int) x, (int) y, width - gap, height); |
|
||||||
x += Dx; |
|
||||||
} |
|
||||||
y += Dy; |
|
||||||
x = 0; |
|
||||||
} |
|
||||||
|
|
||||||
File f = new File("color.png"); |
|
||||||
try { |
|
||||||
ImageIO.write(bi, "png", f); |
|
||||||
} |
|
||||||
catch (IOException e) { |
|
||||||
e.printStackTrace(); |
|
||||||
} |
|
||||||
g.dispose(); |
|
||||||
|
|
||||||
return bi; |
|
||||||
} |
|
||||||
|
|
||||||
public static void main(String[] args) { |
|
||||||
double[][] data; |
|
||||||
int pixelsWidth = 300; |
|
||||||
int pixelsHeight = 400; |
|
||||||
Random r = new Random(); |
|
||||||
|
|
||||||
data = new double[pixelsWidth][]; |
|
||||||
for (int ndx = 0; ndx < pixelsWidth; ndx++) { |
|
||||||
data[ndx] = new double[pixelsHeight]; |
|
||||||
for (int ndx2 = 0; ndx2 < pixelsHeight; ndx2++) { |
|
||||||
// data[ndx][ndx2] = r.nextDouble();
|
|
||||||
data[ndx][ndx2] = ndx2; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
try { |
|
||||||
generateImage(data, pixelsWidth, pixelsHeight); |
|
||||||
} |
|
||||||
catch (Exception e) { |
|
||||||
e.printStackTrace(); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue