QLabel OpenCV Image Coordinates in Qt

安稳与你 提交于 2019-12-24 13:23:13

问题


I would like to do manipulations on the image with OpenCV based on mouseClicks.

I am using QLabel to display cv::Mat images. Now my problem is with getting the mouse clicks positions with respect to the image. So, I would like (0,0) at topleft corner of the image.

Following is my mousePressEvent, but these are not correct co-ordinates.

void MainWindow::mousePressEvent( QMouseEvent* ev )
{


//This seems to work thanks to Pavel
        QPoint P = ui->label->mapFrom(this, ev->pos())



    //if( ui->label->underMouse() )
    {
        QMessageBox msgBox;
        //m

sgBox.setText(QString("Click Detected X=")+QString::number(mFirstX)+QString(" Y=")+QString::number(mFirstY));
            msgBox.setText("x ="+QString::number(P.x()) + " y= " + QString::number(P.y()));
            msgBox.exec();
        }


    }

    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
      if (event->type() == QEvent::MouseMove)
      {


           QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    ///////
    */// This seem to still give wrong position, these values do not match to those I get when I /// click
    ///////
            const QPoint P = ui->label->mapFrom(this, mouseEvent->pos());
            //statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mo
useEvent->pos().y()));
        statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(P.x()).arg(P.y()));
      }

      return false;
    }*

Please help.


回答1:


You need to set QLabel's alignment to Qt::AlignTop | Qt::AlignLeft and ensure that its scaledContents property is false. You should use ui->label->mapFrom(this, ev->pos()) to convert MainWindow coordinates to label coordinates.



来源:https://stackoverflow.com/questions/21732551/qlabel-opencv-image-coordinates-in-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!