Skip to content

Instantly share code, notes, and snippets.

@bengfarrell
Created December 12, 2012 02:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bengfarrell/4264238 to your computer and use it in GitHub Desktop.
Save bengfarrell/4264238 to your computer and use it in GitHub Desktop.
Customized version of SimpleViewer sample from OpenNI where we combine depth and RGB data
/****************************************************************************
* *
* 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 org.OpenNI.Samples.SimpleViewer;
import com.sun.deploy.uitoolkit.ui.ConsoleWindow;
import org.OpenNI.*;
import java.awt.color.ColorSpace;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import java.awt.*;
import java.awt.image.*;
class SimpleViewerCustom extends Component {
/**
*
*/
private static final long serialVersionUID = 1L;
private OutArg<ScriptNode> scriptNode;
private Context context;
private DepthGenerator depthGen;
private ImageGenerator imageGen;
private byte[] clrbytes;
private byte[] imgbytes;
private float histogram[];
private BufferedImage bimg;
int width, height;
private final String SAMPLE_XML_FILE = "Config/SamplesConfig.xml";
public SimpleViewerCustom() {
try {
scriptNode = new OutArg<ScriptNode>();
context = Context.createFromXmlFile(SAMPLE_XML_FILE, scriptNode);
depthGen = DepthGenerator.create(context);
imageGen = ImageGenerator.create(context);
depthGen.getAlternativeViewpointCapability().setViewpoint(imageGen);
DepthMetaData depthMD = depthGen.getMetaData();
ImageMetaData imgMD = imageGen.getMetaData();
histogram = new float[10000];
width = depthMD.getFullXRes();
height = depthMD.getFullYRes();
System.out.print(width + "x" + height + "\n");
imgbytes = new byte[width*height];
clrbytes = new byte[width*height*3];
DataBufferByte dataBuffer = new DataBufferByte(clrbytes, width*height * 3);
WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, width, height, width * 3, 3, new int[]{0, 1, 2}, null);
ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[]{8, 8, 8}, false, false, ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
bimg = new BufferedImage(colorModel, raster, false, null);
bimg.setData(raster);
} catch (GeneralException e) {
e.printStackTrace();
System.exit(1);
}
}
private void calcHist(DepthMetaData depthMD, ImageMetaData imageMD)
{
// reset
for (int i = 0; i < histogram.length; ++i)
histogram[i] = 0;
ShortBuffer depth = depthMD.getData().createShortBuffer();
//ByteBuffer img = imageMD.getData().createByteBuffer();
depth.rewind();
int points = 0;
while(depth.remaining() > 0)
{
short depthVal = depth.get();
if (depthVal != 0)
{
histogram[depthVal]++;
points++;
}
}
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)));
}
}
}
void updateDepth()
{
try {
DepthMetaData depthMD = depthGen.getMetaData();
ImageMetaData imageMD = imageGen.getMetaData();
context.waitAnyUpdateAll();
ShortBuffer depth = depthMD.getData().createShortBuffer();
ByteBuffer img = imageMD.getData().createByteBuffer();
//depth.rewind();
//img.rewind();
while(depth.remaining() > 0)
{
int pos = depth.position();
short pixel = depth.get();
if (pixel > 1600) {
pixel = 0;
img.get(); img.get(); img.get();
clrbytes[pos*3 + 0] = 127;
clrbytes[pos*3 + 1] = 0;
clrbytes[pos*3 + 2] = 0;
} else {
clrbytes[pos*3 + 0] = img.get();
clrbytes[pos*3 + 1] = img.get();
clrbytes[pos*3 + 2] = img.get();
}
}
} catch (GeneralException e) {
e.printStackTrace();
}
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
public void paint(Graphics g) {
DataBufferByte dataBuffer = new DataBufferByte(clrbytes, width*height*3);
WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, width, height, width * 3, 3, new int[]{0, 1, 2}, null);
ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[]{8, 8, 8}, false, false, ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
bimg = new BufferedImage(colorModel, raster, false, null);
bimg.setData(raster);
g.drawImage(bimg, 0, 0, null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment