Implementing 2D Image Transformations in Qt
Scaling Operations
To implement image scaling, define slots in your header file to handle the logic:
protected slots:
void zoomInImage();
void zoomOutImage();
Connect these slots to your UI actions within the createActions() function:
connect(zoomInAction, &QAction::triggered, this, &ImgProcessor::zoomInImage);
connect(zoomOutAction, &QAction::triggered, this, &ImgProcessor::zoomOutImage);
Implement the scaling logic using the QTransform class (the modern replacement to QMatrix). The following example demonstrates how to apply a scaling factor to the current image:
void ImgProcessor::zoomInImage() {
if (currentImage.isNull()) return;
QTransform transform;
transform.scale(2.0, 2.0);
currentImage = currentImage.transformed(transform);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(currentImage));
}
void ImgProcessor::zoomOutImage() {
if (currentImage.isNull()) return;
QTransform transform;
transform.scale(0.5, 0.5);
currentImage = currentImage.transformed(transform);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(currentImage));
}
Rotation Logic
To rotate an image, create a slot that applies a transformation matrix with a specified degree value:
void ImgProcessor::rotateImageNinety() {
if (currentImage.isNull()) return;
QTransform rotationMatrix;
rotationMatrix.rotate(90);
currentImage = currentImage.transformed(rotationMatrix);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(currentImage));
}
The transformed() method generates a new QImage or QPixmap based on the provided affine trensformation, allowing to non-destructive sequential rotations.
Mirroring Operations
The QImage::mirrored() method simplifies flipping an image horizontally or vertically. Use boolean parameters to control the axis:
void ImgProcessor::flipVertical() {
if (currentImage.isNull()) return;
currentImage = currentImage.mirrored(false, true);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(currentImage));
}
void ImgProcessor::flipHorizontal() {
if (currentImage.isNull()) return;
currentImage = currentImage.mirrored(true, false);
showWidget->imageLabel->setPixmap(QPixmap::fromImage(currentImage));
}
Overview of Transformation Matrices
In Qt, transformation matrices provide a mechanism for coordinate system manipulation, including translation, rotation, scaling, and shearing. While QMatrix is available for legacy 2D support, QTransform is recommended for all new development.
- Translation:
matrix.translate(dx, dy)shifts the coordinate system by the specified units. - Rotation:
matrix.rotate(angle)modifies the orientation around the origin. - Scaling:
matrix.scale(sx, sy)multiplies coordinate values to resize the content. - Shearing:
matrix.shear(sh, sv)tilts the coordinates based on horizontal and vertical factors. - Mapping: Use
matrix.map(QPointF)to calculate the new position of a specific point after a transformation has been applied. - Reset: The
reset()function returns the matrix to the identity state, clearing all previous transformations.