cv::Mat3b Inspector::oniToCV(const openni_wrapper::Image& oni) const { cv::Mat3b img(oni.getHeight(), oni.getWidth()); uchar data[img.rows * img.cols * 3]; oni.fillRGB(img.cols, img.rows, data); int i = 0; for(int y = 0; y < img.rows; ++y) { for(int x = 0; x < img.cols; ++x, i+=3) { img(y, x)[0] = data[i+2]; img(y, x)[1] = data[i+1]; img(y, x)[2] = data[i]; } } return img; }
void DataCapture::saveRgb (const std::string &filename, const openni_wrapper::Image &image) const { // get the meta data associated with the image xn::ImageMetaData& md = const_cast<xn::ImageMetaData&> (image.getMetaData()); /* cout << "image data:" << endl; cout << " - size = " << image.getWidth() << "x" << image.getHeight() << endl; cout << " - size (md) = " << md.FullXRes() << "x" << md.FullYRes() << endl; cout << " - encoding = " << image.getEncoding() << endl; cout << " - bytes/pixel = " << md.BytesPerPixel() << endl; cout << " - data size = " << md.DataSize() << endl; cout << " - pixel format (enum) = " << md.PixelFormat() << endl; */ // copy the rgb values in a buffer int width = image.getWidth(), height = image.getHeight(); unsigned char* buffer = new unsigned char[width*height*3]; image.fillRGB (width, height, buffer, width*3); cv::Mat img (height, width, CV_8UC3); // openCV saves color values in order BGR, so rearrange int idx = 0; for (int r = 0; r < height; ++r) { for (int c = 0; c < width; ++c, idx += 3) { img.data[idx+2] = buffer[idx]; img.data[idx+1] = buffer[idx+1]; img.data[idx] = buffer[idx+2]; } } // save as PNG image cv::imwrite(filename + "_rgb.png", img); // std::cout << "RGB image saved as PNG to " << filename << "_rgb.png" << std::endl; }