PDA

View Full Version : Copy A non-Rectangular Image



Parvat
20th June 2013, 10:15
hi

I am able to crop image by using the copy() function by copying image from rectItem that i have drawn,but when i rotate image to 20 degree or any degree and now i want to crop that rotate rect Image, how can i copy a nonRectangular Image
Help me guys :).

Santosh Reddy
20th June 2013, 15:40
The rotated image can be copied the same way as not rotated image, what is problem it doing so?

Parvat
21st June 2013, 07:20
i did try to copy the roated image from the Rect but u see as soon as i rotate the Rect the Rect is no longer a Rectangle it becomes a Ploygon so now how can i copy that because copy function only take Rect Paramter.....so how ll i copy that new Rect(thats now a polygon)...hope U got what i mean to say.


or is there any alternate way to copy or crop from any shape beside only Rect

ChrisW67
25th June 2013, 06:58
Rotated or not, a rectangle is still a rectangle.

QImage::copy() can only copy a rectangle that is aligned with the sides of the image. If you want to copy a rectangular area that is rotated some number of degrees from horizontal then you need to rotate the QImage in the opposite direction and take a horizontal rectangular copy() from that image.

Of course, if you have the content of the rectangle before you rotated it then, in an ideal world, that is what it contains after you rotate it also.

Parvat
25th June 2013, 07:57
If you want to copy a rectangular area that is rotated some number of degrees from horizontal then you need to rotate the QImage in the opposite direction and take a horizontal rectangular copy() from that image.

.

Sorry Chris but i m still not getting it .
this is My code plzz check it .


//Image loading is Done.
//File Crop Code

void FileCropper::onFileCrop()
{
myLeft=ui->myLeftSpinBox->value();
myRight=ui->myRightSpinBox->value();
myTop=ui->myTopSpinBox->value();
myBottom=ui->myBottomSpinBox->value();
QPixmap pixmap = myBeforePixItem->pixmap();

QRectF newRect = myRectItem->rect();
QPointF point = myRectItem->scenePos();
float x = point.x();
float y = point.y();
QRectF moveRect( x + newRect.left() , y + newRect.top() , newRect.width(), newRect.height() );
qDebug()<<"X:"<<x;
qDebug()<<"Y:"<<y;
QPixmap newImage = pixmap.copy( moveRect.toRect() );
myAfterScene.setSceneRect( 0, 0, newRect.width(), newRect.height());
ui->myAfterGraphicsVew->fitInView(myAfterScene.sceneRect(),Qt::KeepAspectR atio);
ui->myAfterGraphicsVew->centerOn(newRect.width()/2,newRect.height()/2);
myAfterPixItem->setPixmap( newImage );
}
//my Rect Code

void FileCropper::onChange()
{
myLeft=ui->myLeftSpinBox->value();
myRight=ui->myRightSpinBox->value();
myTop=ui->myTopSpinBox->value();
myBottom=ui->myBottomSpinBox->value();
QPixmap pixmap = myBeforePixItem->pixmap();
QRectF newRect( myLeft, myTop, pixmap.width() - myLeft -myRight, pixmap.height() - myTop -myBottom );
myRectItem->setRect( newRect );
}
//Rotate Code
void FileCropper::onRotate(int val)
{
QRectF rotateRect(0,0,myBeforePixItem->pixmap().width(),myBeforePixItem->pixmap().height());
QTransform t;
t.translate(rotateRect.center().x(),rotateRect.cen ter().y());
t.rotate(val);
t.translate(-rotateRect.center().x(),-rotateRect.center().y());
myRectItem->setTransform(t,false);
}

ChrisW67
25th June 2013, 23:39
You started off wanting to copy a rectangular area, now you want to crop an image.

The resulting image must be rectangular. What exactly do you want to occur to the areas outside the rotated rectangle? What size should the resulting image be; the same size as the original, or the minimum size to hold the rotated rectangle?

Here is one approach to extract an arbitrary area:


#include <QtGui>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

// http://en.wikipedia.org/wiki/File:Lenna.png
QPixmap source("Lenna.png");

// My desired rectangle
QRect area(150, 150, 300, 50);
QTransform transform;
transform.translate(area.center().x(), area.center().y());
transform.rotate(45);
transform.translate(-area.center().x(), -area.center().y());

QBitmap mask(source.size());
mask.fill(Qt::color1);
QPainter p(&mask);
p.setPen(Qt::color0);
p.setBrush(Qt::color0);
p.setTransform(transform);
p.drawRect(area);
p.end();

QPixmap dest(source);
dest.setAlphaChannel(mask);

QLabel before;
before.setPixmap(source);
QLabel after;
after.setPixmap(dest);

before.show();
after.show();

return app.exec();
}

Parvat
26th June 2013, 07:52
i have send you my app image where in 1st image it does the cropping(copying ) of image perfectly but in 2 image i have rotate the rectangle and when i crop the result is in 2nd Image .

i want to send my exe file how can i send u so that u can check what i meant to say?
9187918891879188
thanks for the approach :);) .

ChrisW67
26th June 2013, 08:05
i want to send my exe file how can i send u so that u can check what i meant to say?
Don't. I won't run it even if you gave me one that would run on my system, and I suspect no-one else will either. There are obvious reasons for banning executables.

It is not the forum's job to discern your requirements or test your software. If you want to explain your requirement then do so in English, with the aid of pictures (screen shots or mock-ups). You can easily upload images to this forum. Perhaps then we will understand exactly what it is you want.

Parvat
26th June 2013, 08:08
k sorry for that i got it i have uploaded images .....

ChrisW67
26th June 2013, 09:27
I still don't know what you expected to see.
For this input and selected rectangular area:
9189
Did you expect this:
9190
or this:
9191
(black areas transparent) or something else?

Parvat
26th June 2013, 09:47
I still don't know what you expected to see.
For this input and selected rectangular area:
9189
Did you expect this:
9190
or this:
9191
(black areas transparent) or something else?

Finally you got it what i mean to say
For the image you gave
i expected 1st output .
9190
back area Transparent

Thanks Chris for understanding my query.

ChrisW67
26th June 2013, 10:04
Then my previous example code is exactly what you need.
Construct a rectangle of the specified size.
Construct a transform to rotate and shift it to the specified coordinates.
Make a mask using a transformed painter
Apply the mask as the alpha channel of a copy of the image.
Profit.

Parvat
26th June 2013, 10:49
Thanks .finally got it . :D