Rev 39 | Rev 42 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include "AlgorithmGrayCode.h"
#include <cmath>
#ifndef log2f
#define log2f(x) (log(x)/log(2.0))
#endif
//using namespace std;
/*
* The purpose of this function is to convert an unsigned
* binary number to reflected binary Gray code.
*
* The operator >> is shift right. The operator ^ is exclusive or.
* Source: http://en.wikipedia.org/wiki/Gray_code
*/
static unsigned int binaryToGray(unsigned int num) {
return (num >> 1) ^ num;
}
/*
* From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
* The purpose of this function is to convert a reflected binary
* Gray code number to a binary number.
*/
static unsigned grayToBinary(unsigned num, unsigned numBits)
{
for (unsigned shift = 1; shift < numBits; shift <<= 1){
num ^= num >> shift;
}
return num;
}
/*
* Function takes the decimal number
* Function takes the Nth bit (1 to 31)
* Return the value of Nth bit from decimal
* Source: http://icfun.blogspot.com/2009/04/get-n-th-bit-value-of-any-integer.html
*/
static int get_bit(int decimal, int N){
// Shifting the 1 for N-1 bits
int constant = 1 << (N-1);
// If the bit is set, return 1
if( decimal & constant ){
return 1;
}
// If the bit is not set, return 0
return 0;
}
static inline int powi(int num, unsigned int exponent){
// NOT EQUIVALENT TO pow()
if(exponent == 0)
return 1;
float res = num;
for(unsigned int i=0; i<exponent-1; i++)
res *= num;
return res;
}
// Algorithm
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
// Number of horizontal encoding patterns
Nhorz = ceilf(log2f((float)screenCols))*2;
// Number of vertical encoding patterns
Nvert = ceilf(log2f((float)screenRows))*2;
// on/off patterns
this->N = 2;
// Set total pattern number
if(dir & CodingDirHorizontal)
this->N += Nhorz;
if(dir & CodingDirVertical)
this->N += Nvert;
// all on pattern
cv::Mat pattern(1, 1, CV_8UC3);
pattern.setTo(cv::Vec3b(255.0,255.0,255.0));
patterns.push_back(pattern);
// all off pattern
pattern.setTo(cv::Vec3b(0.0,0.0,0.0));
patterns.push_back(pattern);
if(dir & CodingDirHorizontal){
// Precompute horizontally encoding patterns
for(unsigned int p=0; p<Nhorz; p++){
cv::Mat patternP(1, screenCols, CV_8UC3);
// Loop through columns in first row
for(unsigned int j=0; j<screenCols; j++){
unsigned int jGray = binaryToGray(j);
// Amplitude of channels
float amp = get_bit(jGray, Nhorz-p);
patternP.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
}
patterns.push_back(patternP);
}
}
if(dir & CodingDirVertical){
// Precompute vertical encoding patterns
for(unsigned int p=0; p<Nvert; p++){
cv::Mat patternP(screenRows, 1, CV_8UC3);
// Loop through rows in first column
for(unsigned int i=0; i<screenRows; i++){
unsigned int iGray = binaryToGray(i);
// Amplitude of channels
float amp = get_bit(iGray, Nvert-p); // Nvert-p-1?
patternP.at<cv::Vec3b>(i,0) = cv::Vec3b(255.0*amp,255.0*amp,255.0*amp);
}
patterns.push_back(patternP);
}
}
}
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
return patterns[depth];
}
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
int nCols = scanLine.cols;
for(int col=1; col<nCols; nCols++){
int labelLeft = scanLine.at<int>(0,col-1);
int labelRight = scanLine.at<int>(0,col);
if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
int orderingRelation = (2 << Nbits)*labelLeft + labelRight;
edges.push_back(cv::Vec4f(col, labelLeft, labelRight, orderingRelation));
}
}
std::sort(edges.begin(), edges.end(), sortingLarger);
}
void AlgorithmGrayCode::getCorrespondences(SMCalibrationParameters calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f>& color){
assert(frames0.size() == N);
assert(frames1.size() == N);
// occlusion maps
cv::Mat occlusion0 = (frames0[0] - frames0[1]) > 0;
cv::Mat occlusion1 = (frames1[0] - frames1[1]) > 0;
// decoded patterns
cv::Mat code0, code1;
int Nbits = (N-2)/2;
for(int i=0; i<Nbits; i++){
cv::Mat bit0 = (frames0[i*2+2] - frames0[i*2+3]) > 0;
code0 += bit0*pow(2, i);
cv::Mat bit1 = (frames1[i*2+2] - frames1[i*2+3]) > 0;
code1 += bit1*pow(2, i);
}
// rectifying homographies
cv::Size frameSize(frames0[0].cols, frames0[0].rows);
cv::Mat R0, R1, P0, P1, Q;
cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, calibration.R1, calibration.T1, R0, R1, P0, P1, Q);
// interpolation maps
cv::Mat map0X, map0Y, map1X, map1Y;
cv::initUndistortRectifyMap(P0, calibration.k0, R0, cv::Mat(), frameSize, CV_32FC1, map0X, map0Y);
cv::initUndistortRectifyMap(P1, calibration.k1, R1, cv::Mat(), frameSize, CV_32FC1, map1X, map1Y);
// remap
cv::remap(occlusion0, occlusion0, map0X, map0Y, cv::INTER_CUBIC);
cv::remap(occlusion1, occlusion1, map1X, map1Y, cv::INTER_CUBIC);
cv::remap(code0, code0, map1X, map1Y, cv::INTER_CUBIC);
cv::remap(code1, code1, map1X, map1Y, cv::INTER_CUBIC);
int nRows = occlusion0.rows;
int nCols = occlusion0.cols;
// matching
for(int row=0; row<nRows; row++){
std::vector<cv::Vec4f> edges0, edges1;
getEdgeLabels(code0.row(row), Nbits, edges0);
getEdgeLabels(code1.row(row), Nbits, edges1);
// REMOVE DOUBLE ENTRIES!
int i=0, j=0;
while(i<edges0.size() && j<edges1.size()){
if(edges0[i][3] == edges1[j][3]){
q0.push_back(cv::Point2f(row, edges0[i][0]));
q1.push_back(cv::Point2f(row, edges1[j][0]));
i += 1;
j += 1;
} else if(edges0[i][3] < edges1[i][3]){
i += 1;
} else if(edges0[i][3] > edges1[i][3]){
j += 1;
}
}
}
// retrieve color information
int nMatches = q0.size();
color.resize(nMatches);
for(int i=0; i<nMatches; i++){
cv::Vec3f color0 = frames0[0].at<cv::Vec3f>(q0[i].y, q0[i].x);
cv::Vec3f color1 = frames1[0].at<cv::Vec3f>(q1[i].y, q1[i].x);
color[i] = 0.5*(color0 + color1);
}
}