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
Code:
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
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
{
if(count_selection!=0)
rubberBand->hide();
point1 = e->pos();
}
{
rubberBand->show();
rubberBand
->setGeometry
(QRect(point1,e
->pos
()));
}
{
count_selection++;
QRect rect;
//selection rectangle rect.setTopLeft(point1);
rect.setBottomRight(e->pos()));
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
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?
Re: Visualize an Image and copy a portion
Some reason I cannot open the images, but any way here is simple working code
Code:
class ImageCopier
: public QLabel{
Q_OBJECT
public:
{
setPixmap
(QPixmap(QFileDialog::getOpenFileName(this, tr
("Open Image for Nyquist Test"),
"", tr
("Images (*.jpg;*.png)"))));
}
signals:
void selectionChanged
(const QPixmap & image
);
protected:
{
mStart = event->pos();
mRubberBand->show();
}
{
mRubberBand
->setGeometry
(QRect(mStart, event
->pos
()).
normalized());
}
{
mRubberBand
->setGeometry
(QRect(mStart, event
->pos
()).
normalized());
mRubberBand->hide();
emit selectionChanged(grab(mRubberBand->geometry()));
}
private:
};
Re: Visualize an Image and copy a portion
thanks, I have update my code and now works perfectly!
My final solution is:
Code:
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
ui->ImageDisplay->setPixmap(image); //ImageDisplay is the name of QLabel
}
{
point1 = e->pos();
}
{
rubberBand->show();
rubberBand
->setGeometry
(QRect(point1,e
->pos
()));
}
{
rubberband->hide();
QRect rect;
//selection rectangle rect.setTopLeft(point1);
rect.setBottomRight(e->pos()));
image = grab(rubberband->geometry()); //copy the selected part
ui->label_image_selected->setPixmap(image); //show "image" in the second QLabel
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?
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".
Re: Visualize an Image and copy a portion
Quote:
Originally Posted by
d_stranz
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 ;)