Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Qt have a way to find bounding box of an image?

Tags:

qt

Given a .png image with a transparent background, I want to find the bounding box of the non-transparent data. Using nested for loops with QImage.pixel() is painfully slow. Is there a built-in method of doing this in Qt?

like image 220
retracile Avatar asked Sep 15 '10 19:09

retracile


1 Answers

There is one option that involves using a QGraphicsPixmapItem and querying for the bounding box of the opaque area (QGraphicsPixmapItem::opaqueArea().boundingRect()). Not sure if it is the best way but it works :) It might be worth digging into Qt's source code to see what code is at the heart of it.

The following code will print out the width and height of the image followed by the width and height of the opaque portions of the image:

QPixmap p("image.png");
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(p);
std::cout << item->boundingRect().width() << "," << item->boundingRect().height() << std::endl;
std::cout << item->opaqueArea().boundingRect().width() << "," << item->opaqueArea().boundingRect().height() << std::endl;
like image 75
Arnold Spence Avatar answered Nov 08 '22 14:11

Arnold Spence