【发布时间】:2012-10-10 19:24:48
【问题描述】:
我在 Qt 中旋转图像时遇到了一些问题。每次我用QPainter 旋转我的图像时,它都会变得越来越失真。这是初始图像:
经过一些迭代,它变成:
这是我的代码:
void Ship::Move(int x, int y)
{
QPixmap rotatePixmap(shipPixels.size());
rotatePixmap.fill(Qt::transparent);
QTransform transform;
transform.translate(
rotatePixmap.size().width() / 2,
rotatePixmap.size().height() / 2
);
transform.rotate(degree);
transform.translate(
- rotatePixmap.size().width() / 2,
- rotatePixmap.size().height() / 2
);
QPainter p(&rotatePixmap);
p.setRenderHints(
QPainter::Antialiasing | QPainter::SmoothPixmapTransform,
true
);
p.setTransform(transform);
p.drawPixmap(0, 0, shipPixels);
p.end();
shipPixels = rotatePixmap;
this->setPixmap(shipPixels);
this->move(QPoint(x, y));
degree = 0;
}
对我来说,Qt 保留了图像质量。这种行为完全奇怪。有什么原因吗?
【问题讨论】:
-
每次都需要重新计算新的旋转位图。您正在累积舍入错误。
-
是的,如果您在帧之间保持 shipPixels 不变,而是将其用作锚点,那么您应该很高兴。
-
您看到的效果是不可避免的,因为除了 90 度增量以外的任何旋转量都是有损操作。损失加起来。它与 Qt 无关。
-
@MarkRansom 好点子,尽管理论和实践并不总是相互配合——我敢肯定有几个库会以 90 度增量模糊或扭曲图像! :-/
标签: c++ qt image-rotation rounding-error