Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You should get what you want with std::bitset. For example, the following code should print the descriptor values with 0/1:

std::vector< std::vector<std::bitset< 8 > > > binary_descriptors;
for(int i = 0; i < descriptors.rows; i++) {
  std::vector<std::bitset< 8 > > current_binary_descriptor;
  for(int j = 0; j < descriptors.cols; j++) {
    current_binary_descriptor.push_back( std::bitset< 8 >( descriptors.at<uchar>(i,j) ) );
  }
  binary_descriptors.push_back(current_binary_descriptor);
}

for(size_t i = 0; i < binary_descriptors.size(); i++) {
  for(size_t j = 0; j < binary_descriptors[i].size(); j++) {
    for(size_t k = 0; k < binary_descriptors[i][j].size(); k++) {
      std::cout << binary_descriptors[i][j][k] << " ";
    }
  }

  std::cout << std::endl;
}

There are 3 loops:

  • the first one for iterating over keypoint descriptors
  • the second one for iterating over the packed unsigned char descriptors for one keypoint descriptor
  • the last one for iterating over the 8 bits of an unsigned char.

You cannot cast an unsigned char to a bool to get a binary string. What you are doing is like:

unsigned char c = 20;
bool bit_value = (bool) c;
std::cout << "bit_value=" << bit_value << std::endl;

You will get one for all the values except for zero.

You should get what you want with std::bitset. For example, the following code should print the descriptor values with 0/1:

std::vector< std::vector<std::bitset< 8 > > > binary_descriptors;
for(int i = 0; i < descriptors.rows; i++) {
  std::vector<std::bitset< 8 > > current_binary_descriptor;
  for(int j = 0; j < descriptors.cols; j++) {
    current_binary_descriptor.push_back( std::bitset< 8 >( descriptors.at<uchar>(i,j) ) );
  }
  binary_descriptors.push_back(current_binary_descriptor);
}

for(size_t i = 0; i < binary_descriptors.size(); i++) {
  for(size_t j = 0; j < binary_descriptors[i].size(); j++) {
    for(size_t k = 0; k < binary_descriptors[i][j].size(); k++) {
      std::cout << binary_descriptors[i][j][k] << " ";
    }
  }

  std::cout << std::endl;
}

There are 3 loops:

  • the first one for iterating over keypoint descriptors
  • the second one for iterating over the packed unsigned char descriptors for one keypoint descriptor
  • the last one for iterating over the 8 bits of an unsigned char.

You cannot cast an unsigned char to a bool to get a binary string. What you are doing is like:

unsigned char c = 20;
bool bit_value = (bool) c;
std::cout << "bit_value=" << bit_value << std::endl;

You will get one for all the values except for zero.