Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Visualize an Image and copy a portion

    General and Desktop
    2
    4
    1449
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Polielia last edited by

      Hi, my goal is to visualize an image (e.g. in a QLabel), allow the use to select a portion of this image and visualize this portion in another QLabel. I have written the code, but I have a problem, the portion of image that I visualize is different from the selected part
      CODE CPP

      void Button1_nyq::on_selectimage_nyq_Hom_clicked() 
      {
      
              Imagename = QFileDialog::getOpenFileName(this,tr("Open Image for Nyquist Test"), "", tr("Images (*.jpg)"));  //open image
              //visualize the image
              QPixmap image(Imagename);
              ui->ImageDisplay->setPixmap(image);  //ImageDisplay is the name of the QLabel
          }
      int count_selection=0; //when it is not 0 it mean that the user want to make another selection and I hide the previous 
      
      void Button1_nyq::mousePressEvent(QMouseEvent *e)
      {
              if(count_selection!=0)
                      rubberBand->hide();
              point1 = e->pos();
              rubberBand = new QRubberBand(QRubberBand::Rectangle,this );
      }
      
      void Button1_nyq::mouseMoveEvent(QMouseEvent *e)
      {
              rubberBand->show();
              rubberBand->setGeometry(QRect(point1,e->pos()));
      }
      
      void Button1_nyq::mouseReleaseEvent(QMouseEvent *e)
      {
              count_selection++;
              QRect rect; //selection rectangle
              rect.setTopLeft(point1);
              rect.setBottomRight(e->pos());
              QPixmap image(Imagename);
              QPixmap a = image.copy(rect); //copy the image selected in a
              ui->label_image_selected->setPixmap(a); //visualize the image in the second QLabel 
          }
      }
      

      .h

      #ifndef BUTTON1_NYQ_H
      #define BUTTON1_NYQ_H
      
      #include <QDialog>
      #include <qrubberband.h>
      
      namespace Ui {
      class Button1_nyq;
      }
      
      class Button1_nyq : public QDialog
      {
          Q_OBJECT
      
      public:
          explicit Button1_nyq(QWidget *parent = 0);
          ~Button1_nyq();
      
      private slots:
          void on_selectimage_nyq_Hom_clicked();
      
          void on_SAVE_clicked();
      
          void mousePressEvent(QMouseEvent *e);
      
          void mouseReleaseEvent(QMouseEvent *e);
      
          void mouseMoveEvent(QMouseEvent *e);
      
      private:
          Ui::Button1_nyq *ui;
          QRubberBand *rubberBand;
      };
      
      #endif // BUTTON1_NYQ_H
      
      

      what i'm doing wrong ? :)

      1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by VRonin

        Problem is that e->pos() is relative to Button1_nyq not to the label displaying the image

        use e->globalPos() and QLabel::mapFromGloabal to convert it back to coordinates relative to the image

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply Reply Quote 3
        • P
          Polielia last edited by Polielia

          thanks to VRonin now it seems better, but I still have a problem. when I visualize the selected part of image, the second label show the image of a identical rect but in a different position, near the real position. How can I fix it?

          void Button1_nyq::on_selectimage_nyq_Hom_clicked() 
          {
          
                  Imagename = QFileDialog::getOpenFileName(this,tr("Open Image for Nyquist Test"), "", tr("Images (*.jpg)"));  //apro l'immagine
                  //visualize the image in the first Label
                  QPixmap image(Imagename);
                  ui->ImageDisplay->setPixmap(image);  //ImageDisplay is the name of QLabel
              }
          int count_selection=0; ///when it is not 0 it mean that the user want to make another selection and I hide the previous 
          
          
          void Button1_nyq::mousePressEvent(QMouseEvent *e)
          {
                  if(count_selection!=0)
                          rubberBand->hide();
                  point1 = e->pos();
                  rubberBand = new QRubberBand(QRubberBand::Rectangle,this );
          }
          
          void Button1_nyq::mouseMoveEvent(QMouseEvent *e)
          {
                  rubberBand->show();
                  rubberBand->setGeometry(QRect(point1,e->pos()));
          }
          
          void Button1_nyq::mouseReleaseEvent(QMouseEvent *e)
          {
                  count_selection++;
                  QRect rect; //selection rectangle
                  rect.setTopLeft(point1);
                  rect.setBottomRight(e->pos()));
                 QPixmap image(rect.size());
                  ui->ImageDisplay->render(&image,QPoint(0,0),QRegion(rect)); //copy the selected part into "image"
                  ui->label_image_selected->setPixmap(image); //show "image" in the second QLabel
          
          

          this is the result
          Real image
          http://imgur.com/9f6e0D3
          Selected image
          http://imgur.com/lysvM0q
          Result
          http://imgur.com/s0FydTc

          1 Reply Last reply Reply Quote 0
          • P
            Polielia last edited by

            I solved my problem, this is my solution:

            void Button1_nyq::on_selectimage_nyq_Hom_clicked() 
            {
             
                    Imagename = QFileDialog::getOpenFileName(this,tr("Open Image for Nyquist Test"), "", tr("Images (*.jpg)"));  //apro l'immagine
                    //visualize the image in the first Label
                    QPixmap image(Imagename);
                    ui->ImageDisplay->setPixmap(image);  //ImageDisplay is the name of QLabel
                }
             
            void Button1_nyq::mousePressEvent(QMouseEvent *e)
            {
             
                    point1 = e->pos();
                    rubberBand = new QRubberBand(QRubberBand::Rectangle,this );
            }
             
            void Button1_nyq::mouseMoveEvent(QMouseEvent *e)
            {
                    rubberBand->show();
                    rubberBand->setGeometry(QRect(point1,e->pos()));
            }
             
            void Button1_nyq::mouseReleaseEvent(QMouseEvent *e)
            {
                    rubberband->hide();
                    QRect rect; //selection rectangle
                    rect.setTopLeft(point1);
                    rect.setBottomRight(e->pos()));
             
                   QPixmap image(rect.size());
                   image = grab(rubberband->geometry()); //copy the selected part
                    ui->label_image_selected->setPixmap(image); //show "image" in the second QLabel
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post