convert bitset descriptor -> cv::Mat

百般思念 提交于 2020-03-06 05:12:45

参考https://blog.csdn.net/magicyang87/article/details/7246771

1.bitset转opencv cv::Mat

注意
(1)Mat 的列为32列,类型为uchar
(2)BRIEF::bitset 的长度为256
(3)每8个构成一个uchar
这个测试ok

vector<BRIEF::bitset> temp_brief_descriptors;
extractor(image, keypoints, temp_brief_descriptors);
brief_descriptors_mat=Mat::zeros(brief_descriptors.size(),32,CV_8UC1);
int row=0;
 for(iter = temp_brief_descriptors.begin(); iter!=temp_brief_descriptors.end();iter++)
 {
     BRIEF::bitset bits=*iter;
     for (int i = 0; i < 32; i += 8)
     {
         char ch;
         for (int j = 0; j < 8; ++j)
         {
             if (bits.test(i + j))	// 第i + j位为1
                 ch |= (1 << j);
             else
                 ch &= ~(1 << j);
         }
         brief_descriptors_mat.at<uchar>(row, i)=(uchar)ch;

     }
     row++;
 }
void convert_bitset_to_Mat(vector<BRIEF::bitset> temp_brief_descriptors,cv::Mat& out_brief_descriptors_mat)
{
    //2.convert bitset to Mat
    out_brief_descriptors_mat=cv::Mat::zeros(temp_brief_descriptors.size(),32,CV_8UC1);
    int row=0;
    for(vector<BRIEF::bitset> :: iterator iter = temp_brief_descriptors.begin(); iter!=temp_brief_descriptors.end();iter++)
    {
        BRIEF::bitset bits=*iter;

        for (int i = 0; i < 32; i++)
        {
            char ch=' ';
            int n_offset=i*8;
            for (int j = 0; j < 8; j++)
            {
                if (bits.test(n_offset + j))	// 第i + j位为1
                    ch |= (1 << j);
                else
                    ch &= ~(1 << j);
            }
            out_brief_descriptors_mat.at<uchar>(row, i)=(uchar)ch;
        }
        row++;
    }

}


2.opencv cv::Mat转bitset

这段代码测试有问题!找不出原因!

//1.convert Mat to bitset
    for(int i=0;i<brief_descriptors_mat.rows;i++)
    {
        BRIEF::bitset bits;
        bits.resize(256);
        bits.reset();
        for (int j = 0; j < 32; j ++)
        {
            char ch=(char) brief_descriptors_mat.at<uchar>(i, j);
            int n_offset = j * 8;
            for (int k = 0; k < 8; ++k)
            {
                bits.set(n_offset + k, ch & (1 << k));
            }
        }
        brief_descriptors.push_back(bits);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!