Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Visualize an Image and copy a portion
Forum Updated to NodeBB v4.3 + New Features

Visualize an Image and copy a portion

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.8k Views
  • 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 Offline
    P Offline
    Polielia
    wrote on last edited by
    #1

    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
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      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
      3
      • P Offline
        P Offline
        Polielia
        wrote on last edited by Polielia
        #3

        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
        0
        • P Offline
          P Offline
          Polielia
          wrote on last edited by
          #4

          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
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved