Reputation: 1008
I want to store ORB descriptors calculated by openCV into a std::bitset<256>
.
As there are several descriptors per frame/image I want to use std::vector<std::bitset<256>>
per frame.
And as there are multiple frames in a video, the structure std::vector<std::vector<std::bitset<256>>>
is used at end.
As openCV stores the desciptors into cv::Mat
, I'm questioning myself how to get the descriptors as fast/efficent as possible out of it? So I digged around and found an answer which does not use logic operators like SHIFT
or AND
. Is there any faster way?
Maybe there's a better struct based on std
as std::vector<std::vector<std::bitset<256>>>
to speed it up too?
Here's a short piece of code for easier understanding of the problem.
#include <iostream>
#include <vector>
#include <bitset>
#include <opencv2/opencv.hpp>
int main(int argc, char ** argv)
{
// opencv
cv::Mat color, gray, descs;
std::vector<cv::KeyPoint> kpts;
cv::Ptr<cv::ORB> detector = cv::ORB::create();
cv::VideoCapture video(argv[1]);
// my desired datastruct
using my_desc_t = std::bitset<256>; // one descriptor
using my_frame_t = std::vector<my_desc_t>; // one frame
using my_video_t = std::vector<my_frame_t>; // one video
my_video_t my_video;
my_video.resize(video.get(CV_CAP_PROP_FRAME_COUNT));
// processing
for (size_t i=0,j=video.get(CV_CAP_PROP_FRAME_COUNT); i!=j; ++i)
{
if (video.read(color))
{
// detect and compute
cv::cvtColor(color,gray,CV_BGR2GRAY);
detector->detectAndCompute(gray,cv::Mat(),kpts,descs);
// fill
// TODO
// 8 (uchar) * 32 = 256 bits each orb descriptor.
// how to use logical operators to copy it
// as fast as possible into my_video?
}
}
}
I compile it with clang++ -std=c++11 experiment.cpp -o experiment -lopencv_core -lopencv_imgproc -lopencv_videoio -lopencv_features2d
Upvotes: 1
Views: 793
Reputation: 1008
Here's the answer
// bitset to Mat:
std::bitset<256> bs(11); // some demo value
Mat m(1,32,CV_8U, reinterpret_cast<uchar*>(&bs)); // 32 bytes
cerr << bs << endl;
cerr << m << endl;
// Mat to bitset
std::bitset<256> bs2;
memcpy(&bs2, m.data, 32);
cerr << bs2 << endl;
Upvotes: 1