gpt4 book ai didi

c++ - Qt 的小数像素大小

转载 作者:行者123 更新时间:2023-11-28 02:20:57 24 4
gpt4 key购买 nike

我想显示带有每个像素的文本和图形注释的缩放图像。这是一个用其值注释灰度图像的每个像素的示例程序:

int main(int argc, char *argv[])
{
const int scale = 44;

QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QImage image("test.bmp");

scene.addPixmap(QPixmap::fromImage(image.scaled(scale*image.width(), scale*image.height())));

for (int x = 0; x < image.width(); ++x)
for (int y = 0; y < image.height(); ++y) {
auto text = new QGraphicsSimpleTextItem();

text->setText(QString::number(qGray(image.pixel(x, y))));
text->setBrush(QBrush(Qt::red));
text->setPos(scale*(x+0.2), scale*(y+0.2));
scene.addItem(text);
}

view.show();

return a.exec();
}

它将图像像素重新缩放为 scalexscale 正方形,并需要相应的坐标重新缩放以进行注释。我想保留 1x1 像素大小并使用此坐标系进行注释:text->setPos(x+0.2, y+0.2) 将替换上面的相应行。可以用 QGraphicsScene 来完成吗?

最佳答案

QGraphicsView有自己的比例,使用它而不是自己重新缩放图像。如果在将图像添加到场景之前缩放图像,像素位置和像素数量当然会发生变化。更简单的方法是使用 QGraphicsView 缩放并保持原始图像大小和像素位置。您用于 QGraphicsItem 的位置也是图像中的相同位置。

对于文本,你可以设置一个标志:

text->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);

无论你放大多少,都让它保持相同的大小。

这应该有效:

const int scale = 44;

QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QImage image("test.bmp");

scene.addPixmap(QPixmap::fromImage(image));

for (int x = 0; x < image.width(); ++x) {
for (int y = 0; y < image.height(); ++y) {
QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem();

text->setText(QString::number(qGray(image.pixel(x, y))));
text->setBrush(QBrush(Qt::red));
text->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
text->setPos(x+0.2, y+0.2);
scene.addItem(text);
}
}
view.scale(scale, scale);
view.show();

return a.exec();

关于c++ - Qt 的小数像素大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32496240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com