Visualize an Image and copy a portion
Solved
General and Desktop
-
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 CPPvoid 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 ? :)
-
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 -
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