Skip to content

Instantly share code, notes, and snippets.

@muff1nman
Created June 3, 2013 15:16
Show Gist options
  • Save muff1nman/5698916 to your computer and use it in GitHub Desktop.
Save muff1nman/5698916 to your computer and use it in GitHub Desktop.
Open NI Depth Viewer
/****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* OpenNI is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
package OpenNI.Samples.SimpleViewer;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ShortBuffer;
import org.OpenNI.Context;
import org.OpenNI.DepthGenerator;
import org.OpenNI.DepthMetaData;
import org.OpenNI.GeneralException;
import org.OpenNI.OutArg;
import org.OpenNI.ScriptNode;
class SimpleViewer extends Component {
/**
*
*/
private static final long serialVersionUID = 1L;
private OutArg<ScriptNode> scriptNode;
private Context context;
private DepthGenerator depthGen;
private byte[] imgbytes;
private float histogram[];
private int frames = 0;
private File log = new File("/home/andrew/openni.log");
BufferedWriter bw;
private BufferedImage bimg;
int width, height;
private final String SAMPLE_XML_FILE = "SamplesConfig.xml";
private boolean visual = true;
public SimpleViewer() throws IOException {
// if file doesnt exists, then create it
if (!log.exists()) {
log.createNewFile();
}
FileWriter fw = new FileWriter(log.getAbsoluteFile());
bw = new BufferedWriter(fw);
try {
scriptNode = new OutArg<ScriptNode>();
context = Context.createFromXmlFile(SAMPLE_XML_FILE, scriptNode);
depthGen = DepthGenerator.create(context);
DepthMetaData depthMD = depthGen.getMetaData();
histogram = new float[10000];
width = depthMD.getFullXRes();
height = depthMD.getFullYRes();
imgbytes = new byte[width*height];
DataBufferByte dataBuffer = new DataBufferByte(imgbytes, width*height);
Raster raster = Raster.createPackedRaster(dataBuffer, width, height, 8, null);
bimg = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
bimg.setData(raster);
} catch (GeneralException e) {
e.printStackTrace();
System.exit(1);
}
}
private void visualizeHistogram( float[] histogram ) throws IOException {
for( float each : histogram ) {
long x = (long) Math.round(each / 10000.0);
for( long i = 0; i < x; ++i ) {
bw.write("|");
}
bw.write("\n");
}
}
private float[] compact( float[] histogram, int consecutive ) {
int new_size = Math.round(histogram.length / consecutive );
float[] n_histogram = new float[new_size];
float current_max = 0;
for(int i = 0; i < histogram.length && Math.ceil(i / consecutive) < new_size; ++i ) {
if( histogram[i] > current_max ) {
current_max = histogram[i];
}
if( i % consecutive == 0){
n_histogram[ (int) Math.floor(i / consecutive ) ] = current_max;
current_max = 0;
}
}
return n_histogram;
}
private void calcHist(DepthMetaData depthMD) throws IOException
{
++frames;
// reset
for (int i = 0; i < histogram.length; ++i)
histogram[i] = 0;
ShortBuffer depth = depthMD.getData().createShortBuffer();
depth.rewind();
int points = 0;
while(depth.remaining() > 0)
{
short depthVal = depth.get();
if (depthVal != 0)
{
histogram[depthVal]++;
points++;
}
}
// if (frames % 200 == 0) {
// System.out.println("After initial populate (in hundreds of occurances): ");
// visualizeHistogram(compact(histogram, 12));
// System.out.println();
// }
for (int i = 1; i < histogram.length; i++)
{
histogram[i] += histogram[i-1];
}
if (points > 0)
{
for (int i = 1; i < histogram.length; i++)
{
histogram[i] = (int)(256 * (1.0f - (histogram[i] / (float)points)));
if (frames % 200 == 0 && visual == true)
bw.write("hist " + histogram[i] + "\n" );
}
}
if (frames % 200 == 0 && visual == true) {
bw.write("After summation (in hundreds of occurances): \n");
visualizeHistogram(histogram);
bw.write("\n");
bw.write("First : " + histogram[1] + "\n");
visual = false;
}
}
void updateDepth() throws IOException
{
try {
DepthMetaData depthMD = depthGen.getMetaData();
context.waitAnyUpdateAll();
calcHist(depthMD);
ShortBuffer depth = depthMD.getData().createShortBuffer();
depth.rewind();
while(depth.remaining() > 0)
{
int pos = depth.position();
short pixel = depth.get();
imgbytes[pos] = (byte)histogram[pixel];
}
} catch (GeneralException e) {
e.printStackTrace();
}
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
public void paint(Graphics g) {
DataBufferByte dataBuffer = new DataBufferByte(imgbytes, width*height);
Raster raster = Raster.createPackedRaster(dataBuffer, width, height, 8, null);
bimg.setData(raster);
g.drawImage(bimg, 0, 0, null);
}
}
/****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* OpenNI is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
package OpenNI.Samples.SimpleViewer;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JFrame;
public class SimpleViewerApplication
{
private SimpleViewer viewer;
private boolean shouldRun = true;
private JFrame frame;
public SimpleViewerApplication (JFrame frame)
{
{
this.frame = frame;
frame.addKeyListener(new KeyListener()
{
@Override
public void keyTyped(KeyEvent arg0) {}
@Override
public void keyReleased(KeyEvent arg0) {}
@Override
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE)
{
shouldRun = false;
}
}
});
}
}
public static void main(String s[]) throws IOException {
JFrame f = new JFrame("OpenNI Simple Viewer");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
SimpleViewerApplication app = new SimpleViewerApplication(f);
app.viewer = new SimpleViewer();
f.add("Center", app.viewer);
f.pack();
f.setVisible(true);
app.run();
}
void run() throws IOException {
while(shouldRun) {
viewer.updateDepth();
viewer.repaint();
}
frame.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment