PDA

View Full Version : Subpixel precision when translating an image



gebbissimo
17th September 2018, 09:01
Does Qt allow to translate (or crop) images with sub-pixel accuracy?

The use case is a 2D map with a vehicle at the origin. The map shall also be translated in case the vehicle moves e.g. 0.5 pixels. I believe this should be feasible in principle using bilinear interpolation or similar.

Minimal example:



#include <QtWidgets/QApplication>
#include <QtGui/QImage>
#include <QLabel>

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

QApplication app(argc, argv);

// Parameters
QString PATH_IMG_IN = "../img_test_rect.jpg";
QString PATH_IMG_OUT = "../img_out.png";
float TRANSLATE_IN_PX = 0.5;

// load image
QImage img;
img.load(PATH_IMG_IN);

// rotate image.
QTransform trans;
trans.translate(0,TRANSLATE_IN_PX);
QImage img_new = img.transformed(trans, Qt::SmoothTransformation);

// save image
img_new.save(PATH_IMG_OUT, nullptr, 100);

// optional: Get info about true transformation matrix
QTransform trans_true = QImage::trueMatrix(trans, img.width(), img.height());

return app.exec();
}



I also tried using "void QPainter::drawImage(const QPointF &point, const QImage &image)" without success. Moreover, I found a few threads without answer such as https://stackoverflow.com/questions/40551281/translate-image-by-very-small-step-by-opencv-in-c/40552885?noredirect=1#comment68348425_40552885

It seems to me that Qt does not enable this feature at the moment, but I am just guessing, not knowing.