use of java.awt.image.BufferedImage in project deeplearning4j by deeplearning4j.
the class ConvolutionalIterationListener method restoreRGBImage.
/**
* Returns RGB image out of 3D tensor
*
* @param tensor3D
* @return
*/
private BufferedImage restoreRGBImage(INDArray tensor3D) {
INDArray arrayR = null;
INDArray arrayG = null;
INDArray arrayB = null;
// entry for 3D input vis
if (tensor3D.shape()[0] == 3) {
arrayR = tensor3D.tensorAlongDimension(2, 2, 1);
arrayG = tensor3D.tensorAlongDimension(1, 2, 1);
arrayB = tensor3D.tensorAlongDimension(0, 2, 1);
} else {
// for all other cases input is just black & white, so we just assign the same channel data to RGB, and represent everything as RGB
arrayB = tensor3D.tensorAlongDimension(0, 2, 1);
arrayG = arrayB;
arrayR = arrayB;
}
BufferedImage imageToRender = new BufferedImage(arrayR.columns(), arrayR.rows(), BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < arrayR.columns(); x++) {
for (int y = 0; y < arrayR.rows(); y++) {
Color pix = new Color((int) (255 * arrayR.getRow(y).getDouble(x)), (int) (255 * arrayG.getRow(y).getDouble(x)), (int) (255 * arrayB.getRow(y).getDouble(x)));
int rgb = pix.getRGB();
imageToRender.setRGB(x, y, rgb);
}
}
return imageToRender;
}
use of java.awt.image.BufferedImage in project deeplearning4j by deeplearning4j.
the class ConvolutionalIterationListener method rasterizeConvoLayers.
/**
* We visualize set of tensors as vertically aligned set of patches
*
* @param tensors3D list of tensors retrieved from convolution
*/
private BufferedImage rasterizeConvoLayers(@NonNull List<INDArray> tensors3D, BufferedImage sourceImage) {
int width = 0;
int height = 0;
int border = 1;
int padding_row = 2;
int padding_col = 80;
/*
We determine height of joint output image. We assume that first position holds maximum dimensionality
*/
int[] shape = tensors3D.get(0).shape();
int numImages = shape[0];
height = (shape[2]);
width = (shape[1]);
// log.info("Output image dimensions: {height: " + height + ", width: " + width + "}");
//(height + (border * 2 ) + padding_row) * numImages;
int maxHeight = 0;
int totalWidth = 0;
int iOffset = 1;
Orientation orientation = Orientation.LANDSCAPE;
/*
for debug purposes we'll use portait only now
*/
if (tensors3D.size() > 3) {
orientation = Orientation.PORTRAIT;
}
List<BufferedImage> images = new ArrayList<>();
for (int layer = 0; layer < tensors3D.size(); layer++) {
INDArray tad = tensors3D.get(layer);
int zoomed = 0;
BufferedImage image = null;
if (orientation == Orientation.LANDSCAPE) {
maxHeight = (height + (border * 2) + padding_row) * numImages;
image = renderMultipleImagesLandscape(tad, maxHeight, width, height);
totalWidth += image.getWidth() + padding_col;
} else if (orientation == Orientation.PORTRAIT) {
totalWidth = (width + (border * 2) + padding_row) * numImages;
image = renderMultipleImagesPortrait(tad, totalWidth, width, height);
maxHeight += image.getHeight() + padding_col;
}
images.add(image);
}
if (orientation == Orientation.LANDSCAPE) {
// append some space for arrows
totalWidth += padding_col * 2;
} else if (orientation == Orientation.PORTRAIT) {
maxHeight += padding_col * 2;
maxHeight += sourceImage.getHeight() + (padding_col * 2);
}
BufferedImage output = new BufferedImage(totalWidth, maxHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = output.createGraphics();
graphics2D.setPaint(bgColor);
graphics2D.fillRect(0, 0, output.getWidth(), output.getHeight());
BufferedImage singleArrow = null;
BufferedImage multipleArrows = null;
/*
We try to add nice flow arrow here
*/
try {
if (orientation == Orientation.LANDSCAPE) {
try {
ClassPathResource resource = new ClassPathResource("arrow_sing.PNG");
ClassPathResource resource2 = new ClassPathResource("arrow_mul.PNG");
singleArrow = ImageIO.read(resource.getInputStream());
multipleArrows = ImageIO.read(resource2.getInputStream());
} catch (Exception e) {
}
graphics2D.drawImage(sourceImage, (padding_col / 2) - (sourceImage.getWidth() / 2), (maxHeight / 2) - (sourceImage.getHeight() / 2), null);
graphics2D.setPaint(borderColor);
graphics2D.drawRect((padding_col / 2) - (sourceImage.getWidth() / 2), (maxHeight / 2) - (sourceImage.getHeight() / 2), sourceImage.getWidth(), sourceImage.getHeight());
iOffset += sourceImage.getWidth();
if (singleArrow != null)
graphics2D.drawImage(singleArrow, iOffset + (padding_col / 2) - (singleArrow.getWidth() / 2), (maxHeight / 2) - (singleArrow.getHeight() / 2), null);
} else {
try {
ClassPathResource resource = new ClassPathResource("arrow_singi.PNG");
ClassPathResource resource2 = new ClassPathResource("arrow_muli.PNG");
singleArrow = ImageIO.read(resource.getInputStream());
multipleArrows = ImageIO.read(resource2.getInputStream());
} catch (Exception e) {
}
graphics2D.drawImage(sourceImage, (totalWidth / 2) - (sourceImage.getWidth() / 2), (padding_col / 2) - (sourceImage.getHeight() / 2), null);
graphics2D.setPaint(borderColor);
graphics2D.drawRect((totalWidth / 2) - (sourceImage.getWidth() / 2), (padding_col / 2) - (sourceImage.getHeight() / 2), sourceImage.getWidth(), sourceImage.getHeight());
iOffset += sourceImage.getHeight();
if (singleArrow != null)
graphics2D.drawImage(singleArrow, (totalWidth / 2) - (singleArrow.getWidth() / 2), iOffset + (padding_col / 2) - (singleArrow.getHeight() / 2), null);
}
iOffset += padding_col;
} catch (Exception e) {
// if we can't load images - ignore them
}
for (int i = 0; i < images.size(); i++) {
BufferedImage curImage = images.get(i);
if (orientation == Orientation.LANDSCAPE) {
// image grows from left to right
graphics2D.drawImage(curImage, iOffset, 1, null);
iOffset += curImage.getWidth() + padding_col;
if (singleArrow != null && multipleArrows != null) {
if (i < images.size() - 1) {
// draw multiple arrows here
if (multipleArrows != null)
graphics2D.drawImage(multipleArrows, iOffset - (padding_col / 2) - (multipleArrows.getWidth() / 2), (maxHeight / 2) - (multipleArrows.getHeight() / 2), null);
} else {
// draw single arrow
// graphics2D.drawImage(singleArrow, iOffset - (padding_col / 2) - (singleArrow.getWidth() / 2), (maxHeight / 2) - (singleArrow.getHeight() / 2), null);
}
}
} else if (orientation == Orientation.PORTRAIT) {
// image grows from top to bottom
graphics2D.drawImage(curImage, 1, iOffset, null);
iOffset += curImage.getHeight() + padding_col;
if (singleArrow != null && multipleArrows != null) {
if (i < images.size() - 1) {
// draw multiple arrows here
if (multipleArrows != null)
graphics2D.drawImage(multipleArrows, (totalWidth / 2) - (multipleArrows.getWidth() / 2), iOffset - (padding_col / 2) - (multipleArrows.getHeight() / 2), null);
} else {
// draw single arrow
// graphics2D.drawImage(singleArrow, (totalWidth / 2) - (singleArrow.getWidth() / 2), iOffset - (padding_col / 2) - (singleArrow.getHeight() / 2) , null);
}
}
}
}
return output;
}
use of java.awt.image.BufferedImage in project deeplearning4j by deeplearning4j.
the class ManualTests method writeImage.
private void writeImage(INDArray array, File file) {
// BufferedImage image = ImageLoader.toImage(array);
log.info("Array.rank(): " + array.rank());
log.info("Size(-1): " + array.size(-1));
log.info("Size(-2): " + array.size(-2));
BufferedImage imageToRender = new BufferedImage(array.columns(), array.rows(), BufferedImage.TYPE_BYTE_GRAY);
for (int x = 0; x < array.columns(); x++) {
for (int y = 0; y < array.rows(); y++) {
log.info("x: " + (x) + " y: " + y);
imageToRender.getRaster().setSample(x, y, 0, (int) (255 * array.getRow(y).getDouble(x)));
}
}
try {
ImageIO.write(imageToRender, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
}
use of java.awt.image.BufferedImage in project darkFunction-Editor by darkFunction.
the class AnimationController method buildAnimatedGif.
private void buildAnimatedGif(String filePath) {
Animation animation = this.getWorkingAnimation();
if (animation != null && animation.numCells() > 0) {
Rectangle firstCellRect = animation.getCellAtIndex(0).getSpreadRect();
Point topLeft = new Point(firstCellRect.x, firstCellRect.y);
Point bottomRight = new Point(firstCellRect.x + firstCellRect.width, firstCellRect.y + firstCellRect.height);
for (int i = 1; i < animation.numCells(); i++) {
Rectangle r = animation.getCellAtIndex(i).getSpreadRect();
if (r.x < topLeft.x)
topLeft.x = r.x;
if (r.y < topLeft.y)
topLeft.y = r.y;
if (r.x + r.width > bottomRight.x)
bottomRight.x = r.x + r.width;
if (r.y + r.height > bottomRight.y)
bottomRight.y = r.y + r.height;
}
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.setTransparent(new Color(0, 0, 0, 0));
e.start(filePath);
e.setRepeat(animation.getLoops());
e.setQuality(1);
e.setSize(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
for (int i = 0; i < animation.numCells(); i++) {
BufferedImage image = new BufferedImage(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y, BufferedImage.TYPE_INT_ARGB);
Graphics ig = image.getGraphics();
AnimationCell cell = animation.getCellAtIndex(i);
cell.rebuild();
Rectangle r = cell.getSpreadRect();
r.x -= topLeft.x;
r.y -= topLeft.y;
cell.draw(ig, r);
e.addFrame(image);
e.setDelay(cell.getDelay());
}
e.finish();
}
}
use of java.awt.image.BufferedImage in project darkFunction-Editor by darkFunction.
the class SpritesheetController method pack.
private void pack() {
CustomNode rootNode = (CustomNode) nameTree.getModel().getRoot();
if (rootNode != null) {
ArrayList<Rectangle> rectList = new ArrayList<Rectangle>();
addNodeToRectList(rootNode, rectList);
if (rectList.size() > 0) {
Rectangle[] array = new Rectangle[rectList.size()];
for (int i = 0; i < rectList.size(); ++i) {
array[i] = rectList.get(i);
}
String[] choices = { " Power of two ", " Smallest " };
int choice = JOptionPane.showOptionDialog(// Center in window.
this, // Message
"Would you like the resulting image to use power of two dimensions (eg, 256x512)?", // Title in titlebar
"Image dimensions?", // Option type
JOptionPane.YES_NO_OPTION, // messageType
JOptionPane.QUESTION_MESSAGE, // Icon (none)
null, // Button text as above.
choices, // Default button's label
" ");
PixelPacker packer = new PixelPacker();
BufferedImage newImage = packer.packPixels(viewPanel.getImage(), array, (choice == 0));
viewPanel.setImage(newImage);
bImageModified = true;
viewPanel.repaint();
setModified(true);
taskChangeListener.taskChanged(this);
}
}
}
Aggregations