Недавно я начал изучать C ++ и Openframeworks.
В настоящее время я работаю над предварительно закодированным и совместно используемым проектом карты глубины от «nocomputer». https://github.com/wearenocomputer/ofxGSVDepthmap
Кажется, все работает и дает хорошую карту глубины. Однако отсутствует код, как сохранить результат. Я попробовал сохранить несколько простых строк как изображение, но не смог. Может ли кто-нибудь помочь мне, как правильно сохранить результат карты глубины?
Спасибо,
Пожалуйста, смотрите ниже основной код:
void ofApp::decodeDepthMap() {
//http://maps.google.com/cbk?output=xml&ll=40.7625000,-73.9741670&dm=1
string depth_map_base64 = "deapthmapinfofromgoogle";
vector<unsigned char> depth_map_compressed(depth_map_base64.length());
int compressed_length = decode_base64(&depth_map_compressed[0], &depth_map_base64[0]);
//Uncompress data with zlib
//TODO: decompress in a loop so we can accept any size
unsigned long length = 512 * 256 + 5000;
vector<unsigned char> depth_map(length);
int zlib_return = uncompress(&depth_map[0], &length, &depth_map_compressed[0], compressed_length);
if (zlib_return != Z_OK) {
cout << "unable to decompress depthmap" << endl;
return;
}
//Load standard data
int headersize = depth_map[0];
int numberofplanes = depth_map[1] | (depth_map[2] << 8);
int mapwidth = depth_map[3] | (depth_map[4] << 8);
int mapheight = depth_map[5] | (depth_map[6] << 8);
int offset = depth_map[7];
if (headersize != 8 || offset != 8) {
cout << "Unexpected depth map header " << endl;
return;
}
vector <unsigned char> depthmapIndices = vector<unsigned char>(mapheight * mapwidth);
memcpy(&depthmapIndices[0], &depth_map[offset], mapheight * mapwidth);
vector<DepthMapPlane> depthmapPlanes = vector<DepthMapPlane>(numberofplanes);
memcpy(&depthmapPlanes[0], &depth_map[offset + mapheight * mapwidth], numberofplanes * sizeof(struct DepthMapPlane));
constructDeptMap(mapwidth, mapheight, depthmapIndices, depthmapPlanes);
}
void ofApp::constructDeptMap(int width, int height, vector <unsigned char> depthmapIndices, vector<DepthMapPlane> depthmapPlanes) {
vector<float> depthmap;
depthmap.clear();
depthmap.resize(width * height);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
float xnormalize = (width - x - 1.0f) / (width - 1.0f);
float ynormalize = (height - y - 1.0f) / (height - 1.0f);
float theta = xnormalize * (2 * PI) + (PI / 2);
float phi = ynormalize * PI;
//convert from spherical to cartesian coordinates
vector<float> v;
v.resize(3);
v[0] = sin(phi) * cos(theta);
v[1] = sin(phi) * sin(theta);
v[2] = cos(phi);
int planeIdx = depthmapIndices[y * width + x];
if (planeIdx > 0) {
DepthMapPlane plane = depthmapPlanes[planeIdx];
float t = abs(plane.d / (v[0] * plane.x + v[1] * plane.y + v[2] * plane.z));
depthmap[y * width + (width - x - 1)] = t;
}
else {
depthmap[y * width + (width - x - 1)] = 0.0f;
}
}
}
ofPixels depthPixels;
depthPixels.allocate(width, height, 1);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
float c = depthmap[y * width + x] / 100 * 255;
depthPixels[y * width + x] = c;
}
}
depthmapimage.setFromPixels(depthPixels);
}