Results 1 to 5 of 5

Thread: Visualize an Image and copy a portion

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

    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
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    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)

  4. #3
    Join Date
    Feb 2017
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Visualize an Image and copy a portion

    thanks, I have update my code and now works perfectly!
    My final solution is:
    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.  
    10. void Button1_nyq::mousePressEvent(QMouseEvent *e)
    11. {
    12.  
    13. point1 = e->pos();
    14. rubberBand = new QRubberBand(QRubberBand::Rectangle,this );
    15. }
    16.  
    17. void Button1_nyq::mouseMoveEvent(QMouseEvent *e)
    18. {
    19. rubberBand->show();
    20. rubberBand->setGeometry(QRect(point1,e->pos()));
    21. }
    22.  
    23. void Button1_nyq::mouseReleaseEvent(QMouseEvent *e)
    24. {
    25. rubberband->hide();
    26. QRect rect; //selection rectangle
    27. rect.setTopLeft(point1);
    28. rect.setBottomRight(e->pos()));
    29.  
    30. QPixmap image(rect.size());
    31. image = grab(rubberband->geometry()); //copy the selected part
    32. ui->label_image_selected->setPixmap(image); //show "image" in the second QLabel
    To copy to clipboard, switch view to plain text mode 


    Added after 11 minutes:


    One more question, if now I want to maintain the rubberband on the screen? because when I hide the Rubberband it all ok, but if I dont hide it in the second label I have the portion of image selected but also the color of the rubberband.
    So I want keep the rubberband in the first Label, but hide it in the second (where I show the result)... Or I can change the color of the rubberband, maybe I can maintain only the border and have white in the middle, it is possible to do?
    Last edited by Polielia; 28th February 2017 at 09:33.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Visualize an Image and copy a portion

    In the first image, hide the rubberband, copy the selected portion, then show the rubberband again. When the rubberband is visible, it is part of the image on the first label, so copying that part will copy everything including the rubberband.

    A more common way to do rubberbanding which avoids this problem is to use an overlay widget - a widget with the same size as your label and positioned on top of it, but with a transparent background. You use that widget to draw your rubberband, while the real label underneath that isn't changed at all. Once you have used the rubber band in the overlay widget to select the region, you use the same coordinates to select that part of the label widget. Since the rubber band is drawn on the overlay and not on the label, it doesn't appear in the bits you copy to the new label. Google for "qt overlay widget".
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    Polielia (1st March 2017)

  7. #5
    Join Date
    Feb 2017
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Visualize an Image and copy a portion

    Quote Originally Posted by d_stranz View Post
    In the first image, hide the rubberband, copy the selected portion, then show the rubberband again. When the rubberband is visible, it is part of the image on the first label, so copying that part will copy everything including the rubberband.
    thanks, i have used this solution and works fine

Similar Threads

  1. Copy A non-Rectangular Image
    By Parvat in forum Newbie
    Replies: 12
    Last Post: 26th June 2013, 09:49
  2. How can I copy image from anohter image
    By chong_kimkeang in forum Newbie
    Replies: 12
    Last Post: 27th September 2012, 11:05
  3. Replies: 1
    Last Post: 17th October 2011, 13:56
  4. Show portion of an image
    By ucntcme in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2010, 08:10
  5. Displaying a portion of an image
    By tas in forum Qt Programming
    Replies: 4
    Last Post: 28th November 2008, 03: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.