我正在尝试创建一个直方图,显示Kinect扫描的距离与它们的出现次数。我已经调整了直方图示例代码来创建深度直方图,但它当前在深度图像宽度上多次显示每个像素(从左到右)的深度。 我要做的是重新排序深度信息,使其范围从最低值(不是0)到x轴的最高值,并在y上显示它们的出现次数。我正在使用Processing,所以我不确定这是否是适合发布的网站,但我已经尝试了发布论坛而没有得到任何帮助。如果有人能告诉我哪里出错了,那就太棒了。我当前的代码如下,可以找到我当前输出的屏幕截图here
import SimpleOpenNI.*;
SimpleOpenNI kinect;
void setup() {
size(1200, 580);
kinect = new SimpleOpenNI(this);
kinect.enableDepth();
}
void draw () {
kinect.update();
PImage depthImage = kinect.depthImage();
image (depthImage, 11, 0);
int[] depthValues = kinect.depthMap();
int[] hist = new int[716800];
for (int x = 11; x < depthImage.width; x++) {
for (int y = 0; y < depthImage.height; y++) {
int i = x + y * 640;
hist[i] = depthValues[i];
}
}
int histMax = max(hist);
stroke(20);
for (int i = 0; i < depthImage.width; i += 2) {
int which = int(map(i, 0, depthImage.width, 0, histMax));
int y = int(map(hist[which], 0, histMax, depthImage.height, 0));
line(i, depthImage.height, i, y);
}
}
答案 0 :(得分:0)
我想你在这里问两个问题。
如何让直方图从0-N开始:
使用Processing的sort()
function对数组进行排序。
hist = sort(hist); // sorts your array numerically
如何让直方图填满屏幕:
我不完全确定它为什么要绘制两次,但我认为你可以清理你的代码。
// how far apart are the bars - set based on screen dimensions
int barSpacing = width / hist.length;
for (int i=0; i<hist.length; i++) {
// get value and map into usable range (note 10 not 0 for min)
int h = int(map(hist[i], 0,histMax, 10,height));
// set x position onscreen
int x = i * barSpacing;
// draw the bar
line(x,height, x,height-h);
}