PDA

View Full Version : How can I copy image from anohter image



chong_kimkeang
18th September 2012, 04:41
Hi, I have learn Qt for several months and now I have created a program that related to the drawing. I want to get part of an image that user need. They can circle or use the painter to draw the shape to cut the part of the image. Thank in advanced for help me.
8223

pkj
18th September 2012, 05:18
Create a QImage of the same size. Create a QPolygon to represent you drawn area. Now loop through the length and breadth of the image. At each point, if QPoygon::contains(point), set original QImage::pixel else set some default.

chong_kimkeang
18th September 2012, 10:28
Thanks, but can you explain me in more detail because I am new to Qt. If possible, can you please write a short code example. :o

chong_kimkeang
19th September 2012, 05:32
Sorry, what function can I create image the same size? and when I loop the point, still how can I cut the image in the shape I draw?

chong_kimkeang
19th September 2012, 11:02
Sorry, what function can I create image the same size? and when I loop the point, still how can I cut the image in the shape I draw?

ChrisW67
19th September 2012, 22:22
Sorry, what function can I create image the same size?


QImage newImage = oldImage.copy();
// or
QImage newImage(oldImage.size(), oldImage.format());
newImage.fill(Qt::transparent); // or something like this


and when I loop the point, still how can I cut the image in the shape I draw?
Using the approach described above: You need to obtain the outline of the selection area as a QPolygon. Then you can use QPolygon::containsPoint() to see if a given pixel (point) is inside or outside the polygon, i.e. selected or not selected. Then, for each pixel in the original that is inside the polygon set the corresponding pixel in the copy. It is fairly straightforward nested for() loops once you have the polygon.

How you get the polygon depends on how your application handles the drawing... and we don't know that.

There are probably other ways to do this using masks.

chong_kimkeang
20th September 2012, 04:31
Thanks, now I have polygon but how can I crop the image inside the Polygon? what function should I use because I don't know other functions that can copy the image beside copy() that can copy only rectangle. I don't know how can I get the image inside it. Thanks for helping me :)

ChrisW67
20th September 2012, 08:13
Try something like this for a slow approach


for (int x = 0; x < oldImage.width(); ++x) {
for (int y = 0; y < oldImage.width(); ++y) {
QPoint p(x, y);
if (polygon.containsPoint(p, Qt::OddEvenFill)) {
QRgb color = oldImage.pixel(p);
newImage.setPixel(p, color);
}
}
}

(Untested code)

You should investigate ways to use the image composition modes to draw a filled version of your polygon over a copy of the original leaving only the intersection. This would be much faster.

chong_kimkeang
21st September 2012, 02:36
Thanks, however when I loop, it does nothing and I can't even close the app but I think it is because of other part of the code. Thanks all of you very much for giving me, I will ask you again whenever I have problem with Qt. Thanks :)

ChrisW67
21st September 2012, 02:44
Line 2 has an error and should be checking the height not width.

chong_kimkeang
27th September 2012, 04:02
Hello everyone, for doing with polygon, I can't because it always can't be close. So could you tell me how to cut the image by using mask?

ChrisW67
27th September 2012, 09:00
If you don't have a closed polygon then what defines the region you want to copy?

Here is an example of a faster way to do it:


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QImage source("Lenna.png");

// An arbitrary region to cut out
QPolygon region;
region << QPoint(10, 10) << QPoint(100, 200) << QPoint(500, 50);

// Let's do it
QPainterPath path;
path.addPolygon(region);

QImage cutout(source.size(), QImage::Format_ARGB32_Premultiplied);
cutout.fill(Qt::transparent);
QPainter p(&cutout);
p.setClipPath(path);
p.drawImage(0, 0, source);
p.end();

QLabel l;
l.setPixmap(QPixmap::fromImage(cutout));
l.show();

return a.exec();
}

chong_kimkeang
27th September 2012, 11:05
Hello everyone, I am very happy because now I can cut that image. Greatly thank everyone particularly the Expert ChrisW67 who give me the sample code which help me create this success. ;)