Results 1 to 5 of 5

Thread: Visualize an Image and copy a portion

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2017
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    2

    Default Visualize an Image and copy a portion

    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

    Qt Code:
    1. void Button1_nyq::on_selectimage_nyq_Hom_clicked()
    2. {
    3.  
    4. Imagename = QFileDialog::getOpenFileName(this,tr("Open Image for Nyquist Test"), "", tr("Images (*.jpg)")); //apro l'immagine
    5. //visualize the image in the first Label
    6. QPixmap image(Imagename);
    7. ui->ImageDisplay->setPixmap(image); //ImageDisplay is the name of QLabel
    8. }
    9. int count_selection=0; ///when it is not 0 it mean that the user want to make another selection and I hide the previous
    10.  
    11.  
    12. void Button1_nyq::mousePressEvent(QMouseEvent *e)
    13. {
    14. if(count_selection!=0)
    15. rubberBand->hide();
    16. point1 = e->pos();
    17. rubberBand = new QRubberBand(QRubberBand::Rectangle,this );
    18. }
    19.  
    20. void Button1_nyq::mouseMoveEvent(QMouseEvent *e)
    21. {
    22. rubberBand->show();
    23. rubberBand->setGeometry(QRect(point1,e->pos()));
    24. }
    25.  
    26. void Button1_nyq::mouseReleaseEvent(QMouseEvent *e)
    27. {
    28. count_selection++;
    29. QRect rect; //selection rectangle
    30. rect.setTopLeft(point1);
    31. rect.setBottomRight(e->pos()));
    32. QPixmap image(rect.size());
    33. ui->ImageDisplay->render(&image,QPoint(0,0),QRegion(rect)); //copy the selected part into "image"
    34. ui->label_image_selected->setPixmap(image); //show "image" in the second QLabel
    To copy to clipboard, switch view to plain text mode 

    the actual results are:
    Real image
    http://imgur.com/9f6e0D3
    Selected image
    http://imgur.com/lysvM0q
    Result
    http://imgur.com/s0FydTc

    How can I fix it?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Visualize an Image and copy a portion

    Some reason I cannot open the images, but any way here is simple working code
    Qt Code:
    1. class ImageCopier : public QLabel
    2. {
    3. Q_OBJECT
    4. public:
    5. ImageCopier(QWidget * parent = 0)
    6. : QLabel(parent)
    7. , mRubberBand(new QRubberBand(QRubberBand::Rectangle, this))
    8. {
    9. setPixmap(QPixmap(QFileDialog::getOpenFileName(this, tr("Open Image for Nyquist Test"), "", tr("Images (*.jpg;*.png)"))));
    10. }
    11.  
    12. signals:
    13. void selectionChanged(const QPixmap & image);
    14.  
    15. protected:
    16. void mousePressEvent(QMouseEvent * event)
    17. {
    18. emit selectionChanged(QPixmap());
    19.  
    20. mStart = event->pos();
    21. mRubberBand->setGeometry(QRect(mStart, QSize()));
    22. mRubberBand->show();
    23. }
    24.  
    25. void mouseMoveEvent(QMouseEvent * event)
    26. {
    27. mRubberBand->setGeometry(QRect(mStart, event->pos()).normalized());
    28. }
    29.  
    30. void mouseReleaseEvent(QMouseEvent * event)
    31. {
    32. mRubberBand->setGeometry(QRect(mStart, event->pos()).normalized());
    33. mRubberBand->hide();
    34. emit selectionChanged(grab(mRubberBand->geometry()));
    35. }
    36.  
    37. private:
    38. QRubberBand * mRubberBand;
    39. QPoint mStart;
    40. };
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    Polielia (28th February 2017)

Similar Threads

  1. Copy A non-Rectangular Image
    By Parvat in forum Newbie
    Replies: 12
    Last Post: 26th June 2013, 10:49
  2. How can I copy image from anohter image
    By chong_kimkeang in forum Newbie
    Replies: 12
    Last Post: 27th September 2012, 12:05
  3. Replies: 1
    Last Post: 17th October 2011, 14:56
  4. Show portion of an image
    By ucntcme in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2010, 09:10
  5. Displaying a portion of an image
    By tas in forum Qt Programming
    Replies: 4
    Last Post: 28th November 2008, 04:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.