Hi,
I'm working with Voxel SDK and the OPT8241 board.
I would like to know if there is the possibility to capture the frame without a callback, I'm going to explain my problem:
I want to view the depth map in a windows, I'm using the library glut (opengl)
If I want to have the continuous frame show in the windows I have to use a function called glutIdleFunc(IdleFunc) that I can't pass any parameters by function, inside this function I want to acquire one frame and refresh the window depth map. How can I do?
Another problem that I get now is: I want to separate the Init_camera that consinst in find the camera on usb and connect to the camera, and a second function that capture a frame one time each call. But when I try to use this two separate function I got an error on the variable DepthCameraPtr.
Anyone can help me? Thak you
These are the two function:
DepthCameraPtr depthCamera //global
void Init_camera()
{
int argc = 0;
char **argv = NULL;
CSimpleOpt s_save(argc, argv, argumentSpecifications);
logger.setDefaultLogLevel(LOG_INFO);
uint16_t vid = 0;
Vector<uint16_t> pids;
String serialNumber;
vid = 0x0451; // is the vendor ID in hex
pids.push_back(37125); // is the product ID in decimal of 0x9105
serialNumber = { '9', '1', '6', '0' .... };
char *endptr_save;
while (s_save.Next())
{
if (s_save.LastError() != SO_SUCCESS)
{
std::cout << s_save.GetLastErrorText(s_save.LastError()) << ": '" << s_save.OptionText() << "' (use -h to get command line help)" << std::endl;
help();
}
switch (s_save.OptionId())
{
default:
help();
break;
};
}
if (vid == 0 || pids.size() == 0 || pids[0] == 0)
{
logger(LOG_ERROR) << "Required argument missing." << std::endl;
help();
}
CameraSystem sys_save;
// Get all valid detected devices
const Vector<DevicePtr> &devices = sys_save.scan();
DevicePtr toConnect_save;
std::cout << "Detected devices: " << std::endl;
for (auto &d_save : devices)
{
std::cout << d_save->id() << std::endl;
if (d_save->interfaceID() == Device::USB)
{
USBDevice &usb = (USBDevice &)*d_save;
if (usb.vendorID() == vid && (serialNumber.size() == 0 || usb.serialNumber() == serialNumber))
{
for (auto pid : pids)
if (usb.productID() == pid)
toConnect_save = d_save;
}
}
}
if (!toConnect_save)
{
logger(LOG_ERROR) << "No valid device found for the specified VID:PID:serialnumber" << std::endl;
}
depthCamera = sys_save.connect(toConnect_save);
if (!depthCamera)
{
logger(LOG_ERROR) << "Could not load depth camera for device " << toConnect_save->id() << std::endl;
}
if (!depthCamera->isInitialized())
{
logger(LOG_ERROR) << "Depth camera not initialized for device " << toConnect_save->id() << std::endl;
}
std::cout << "Successfully loaded depth camera for device " << toConnect_save->id() << std::endl;
FilterPtr p = sys_save.createFilter("Voxel::IIRFilter", DepthCamera::FRAME_RAW_FRAME_PROCESSED);
if (!p)
{
logger(LOG_ERROR) << "Failed to get IIRFilter" << std::endl;
}
depthCamera->addFilter(p, DepthCamera::FRAME_RAW_FRAME_PROCESSED);
p = sys_save.createFilter("Voxel::IIRFilter", DepthCamera::FRAME_RAW_FRAME_PROCESSED);
if (!p)
{
logger(LOG_ERROR) << "Failed to get IIRFilter" << std::endl;
}
p->set("gain", 0.2f);
depthCamera->addFilter(p, DepthCamera::FRAME_RAW_FRAME_PROCESSED, 0);
}
GLvoid IdleFunc()
{
TimeStampType lastTimeStamp = 0;
std::cout << " sono qui ";
area_frame portion_frame;
portion_frame.y_upper = 240; // ---------- ---------------- <-- y_lower
portion_frame.y_lower = 0; // --OO--OO-- - -
portion_frame.x_left = 0; // ----xx---- - -
portion_frame.x_right = 320; // --OO--OO-- x_right --> ---------------- <-- x_left, y_upper
float threshold_obstacle = 0.6;
depthCamera->registerCallback(DepthCamera::FRAME_DEPTH_FRAME, [&](DepthCamera &dc, const Frame &frame, DepthCamera::FrameType c) {
const DepthFrame *d_save = dynamic_cast<const DepthFrame *>(&frame);
if (dc.stop() == 1)
{
dc.start();
}
std::cout << "sono qui 2";
if (!d_save)
{
std::cout << "Null frame captured? or not of type ToFRawFrame" << std::endl;
return;
}
std::cout << "Capture frame " << d_save->id << "@" << d_save->timestamp;
auto width = d_save->size.width;
int occorrenze = 0, contatore_pixel = 0;
int index = portion_frame.y_lower * width + portion_frame.x_left;
float min_obstacle = d_save->depth[index];
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
for (auto ii = portion_frame.y_lower; ii < portion_frame.y_upper; ii++)
{
// //for (auto jj = 0; jj < 320; jj++, index++)
for (auto jj = portion_frame.x_left; jj < portion_frame.x_right; jj++, index++)
{
if (d_save->depth[index] < threshold_obstacle)
{
glVertex3f(jj / 320, ii / 240, 1);
if (d_save->depth[index] < min_obstacle)
{
min_obstacle = d_save->depth[index];
}
occorrenze++;
}
contatore_pixel++;
}
index = index + width - portion_frame.x_right + portion_frame.x_left;
}
glEnd();
glFlush();
glutPostRedisplay();
std::cout << " Num occorrenze ostacoli vicini " << occorrenze << " distanza minima (m) " << min_obstacle << std::endl;
if (lastTimeStamp != 0)
{
std::cout << " (" << 1E6 / (d_save->timestamp - lastTimeStamp) << " fps)";
}
dc.stop();
});
if (depthCamera->start())
{
FrameRate r;
if (depthCamera->getFrameRate(r))
{
//logger(LOG_INFO) << "Capturing at a frame rate of " << r.getFrameRate() << " fps" << std::endl;
}
depthCamera->wait();
}
else
{
//logger(LOG_ERROR) << "Could not start the depth camera " << depthCamera1->id() << std::endl;
}
}