Ask Your Question
1

How can I register a keyboard event handier for opencv viz

asked 2017-04-20 02:52:14 -0600

ririgo gravatar image

Hi, I'm using opencv viz library to visualize 3D point clouds. I want to move the camera when I press an arrow key, but I can't find any example for it. Please help me.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-04-20 04:46:44 -0600

LBerger gravatar image

updated 2017-04-21 02:32:19 -0600

you can do like this :

#include <opencv2/opencv.hpp> 
#include <iostream>
using namespace std;
using namespace cv;

#ifdef USE_VTK

void  KeyboardViz3d(const viz::KeyboardEvent &w, void *t)
{
    viz::Viz3d *fen=(viz::Viz3d*)t;
   if (w.action)
       cout << "you pressed "<< w.code<<" = "<<w.symbol<< " in viz window "<<fen->getWindowName()<<"\n";
}

int main (int argc,char **argv)
{

Mat img = imread("f:/lib/opencv/samples/data/lena.jpg", IMREAD_COLOR);
Mat imgh;
cvtColor(img,imgh,CV_BGR2GRAY);

viz::Viz3d fen3D("Mon Image");
fen3D.registerKeyboardCallback(KeyboardViz3d,&fen3D);

int nbF=0;
vector<Point3d> cloud;
vector<int> polygon;
vector<Vec3b> couleur;
double k=1;
int nbPix=0;
for (int i = 1; i<img.rows; i++)
    for (int j = 1; j < img.cols; j++)
    {
        Vec3d p1(j / k, i / k, imgh.at<uchar>(i, j)/k);
        Vec3d p2((j-1) / k, i / k, imgh.at<uchar>(i, j-1) / k);
        Vec3d p3(j / k, (i - 1) / k, imgh.at<uchar>(i-1, j) / k);
        Vec3d p4((j-1) / k, (i - 1) / k, imgh.at<uchar>(i-1, j-1) / k);
        cloud.push_back(p1);
        cloud.push_back(p2);
        cloud.push_back(p3);
        cloud.push_back(p4);
        couleur.push_back(img.at<Vec3b>(i, j));
        couleur.push_back(img.at<Vec3b>(i, j));
        couleur.push_back(img.at<Vec3b>(i, j));
        couleur.push_back(img.at<Vec3b>(i, j));
        polygon.push_back(3);
        polygon.push_back(nbPix);
        polygon.push_back(nbPix + 1);
        polygon.push_back(nbPix + 2);
        polygon.push_back(3);
        polygon.push_back(nbPix + 2);
        polygon.push_back(nbPix + 3);
        polygon.push_back(nbPix);
        nbPix+=4;
    }
viz::WMesh grille(cloud,polygon,couleur);
fen3D.showWidget("I3d", grille);
fen3D.spin();

return 0;
}

#else

int main(int argc, char **argv)
{
cout << " you need VIZ module\n";
return 0;
}
#endif
edit flag offensive delete link more

Comments

Thank you so much.

ririgo gravatar imageririgo ( 2017-04-20 19:11:54 -0600 )edit

I testes it but it didn't work for arrow keys. Of course I don't need to use arrow keys but is there any way to use arrow keys?

ririgo gravatar imageririgo ( 2017-04-20 19:14:13 -0600 )edit

Use symblod a viz::KeyboardEvent field

LBerger gravatar imageLBerger ( 2017-04-21 02:33:14 -0600 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-04-20 02:52:14 -0600

Seen: 72 times

Last updated: Apr 21