BloodAxe/OpenCV-Tutorial

Mastering OpenCV Chapter 3 : generating 3d points

Reena24 opened this issue · 1 comments

I need a bit of help with this code. I am trying to generate 3d points for the keypoints extracted from the webcam. It executes the code but the 3d points cannot be viewed.
code
to compute the pose of each 2d point extracted I used the code

Transformation ARPipeline::computePose(const std::vectorcv::Point2f& points, const CameraCalibration& calibration,cv::Mat& camframe)
{
points3d.resize(points.size());

for(int i = 0; i < points.size();i++)
{
    points3d[i]=cv::Point3f(points[i].x, points[i].y, 0.0);
}

cv::Mat Rvec;

cv::Mat_ Tvec;
cv::Mat raux,taux;
cv::solvePnPRansac(points3d, points, calibration.getIntrinsic(), calibration.getDistorsion(),raux,taux);
raux.convertTo(Rvec,CV_32F);
taux.convertTo(Tvec ,CV_32F);

cv::Mat_ rotMat(3,3);
cv::Rodrigues(Rvec, rotMat);

// Copy to transformation matrix
for (int col=0; col<3; col++)
{
for (int row=0; row<3; row++)
{
pose3d.r().mat[row][col] = rotMat(row,col); // Copy rotation component
}
pose3d.t().data[col] = Tvec(col); // Copy translation component
}

// Since solvePnP finds camera location, w.r.t to marker pose, to get marker pose w.r.t to the camera we invert it.
pose3d = pose3d.getInverted();
return pose3d;
}

then to display the 3d point I use
drawPoint()
{
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT| GL_POINT|GL_POINTS|GL_POINT_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_BLEND);
glEnable(GL_COLOR_MATERIAL);
glColor3f(0,1,1);
glPointSize(10);
glBegin(GL_POINTS);
std::cout<<"XXX"<<std::endl;
for(int i=0;i<m_points2.size();i++)
glVertex3f(m_points2[0].x, m_points2[0].y, 0.0);
glEnd();
glPopAttrib();
}

it is called by the function

void ARDrawingContext::drawAugmentedScene()
{
// Init augmentation projection
Matrix44 projectionMatrix;
int w = m_backgroundImage.cols;
int h = m_backgroundImage.rows;
buildProjectionMatrix(m_calibration, w, h, projectionMatrix);

glMatrixMode(GL_PROJECTION);
glLoadMatrixf(projectionMatrix.data);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Matrix44 glMatrix = patternPose.getMat44();

glLoadMatrixf(reinterpret_cast<const GLfloat*>(&glMatrix.data[0]));

drawPoints();
}

I use the same approach as in chapter 3 of mastering OpenCV the only difference is I am trying to display a map of 3d points instead of cube and there is no pattern it only consists of keypoints from the web cam. I cant make out if any line is missing in the drawPoint function . I have used the same functions
void ARDrawingContext::drawCameraFrame(), buildProjectionMatrix()
void void ARDrawingContext::drawAugmentedScene and draw()
the extra is drawPoint()

am not sure which parameter is missing can you please help

Sorry, this doesn't look as an issue. Please follow documentation of GLES to draw set of 3D points on your screen.