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:
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.
2 | No.2 Revision |
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:
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.