【问题标题】:How to correctly subscribe the pixel value over the pixmap in the QGraphicsScene (as it is in OpenCV namedWindow)?如何正确订阅 QGraphicsScene 中像素图上的像素值(就像在 OpenCV 命名窗口中一样)?
【发布时间】:2021-05-16 20:56:15
【问题描述】:

我正在尝试在我的小部件中实现与 cv::namedWindow 中相同的功能。

我们的目标是启用缩放并在原始像素图上直接使用网格和像素颜色值进行叠加。这是示例: сv 图片缩放:

我继承了 QGraphicsView 小部件,将 QGraphicsPixmapItem 添加到 QGraphicsScene 并重新实现了 QWheelEvent,以便现在可以正常放大和缩小。问题始于创建叠加层。

我没有创建一组 QGraphicsLineItems 并将它们添加到场景中以制作网格,而是继承 QGraphicsRectItem 并在其上绘制整个网格。

void QGraphicsOverlayItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPen pen(Qt::black);
    pen.setWidthF(0.02);
    painter->setPen(pen);
    painter->setBrush(Qt::BrushStyle::NoBrush);

    QVector<QLineF> crossLines = createCrossLines();
    painter->drawLines(crossLines);
}

这工作非常快。但是,当我尝试使用 QPainter 绘制文本并将 QFont::pointSizeF() 设置为尽可能小时,它无法正常工作(符号在缩放过程中从小变大,甚至完全消失)。尽管如此,我以这种方式获得的最佳结果如下: QPainter 的 drawText() 结果:

    QFont font(painter->font());
    font.setPointSizeF(0.1);
    font.setLetterSpacing(QFont::SpacingType::AbsoluteSpacing,0.01);
    painter->setFont(font);
    painter->drawText(432,195,"123");

最简单的方法是在场景中添加很多 QGraphicsTextItems 并将它们缩放到正确的大小,但是太慢了。

那么问题是如何直接在 QPixmapItem 上订阅 QGraphicsScene 中像素的颜色值?

【问题讨论】:

    标签: c++ qt qgraphicsview


    【解决方案1】:

    我终于看了一遍openCV的源码,找到了我要找的东西。

    我的答案是 QTransform 矩阵。 OpenCV 开发人员不是通过使用 QGraphicsView 中的场景来显示图像,而是在paintEvent 中直接在视口上绘制图像。 QTransform 矩阵存储在类中,并在paintEvent 开始时传递给QPainter。

    void DefaultViewPort::paintEvent(QPaintEvent *event)
    {
        QPainter painter(viewport());
        painter.setWorldTransform(param_matrixWorld);
        painter.drawImage(QRect(0,0,viewport()->width(),viewport()->height()),image2Draw,QRect(0,0,image2Draw.width(),image2Draw.height()));
    

    如果您知道图像大小与小部件大小的比率,并且您还知道用于绘制图像的 QTransform 矩阵的比例,则很容易计算单个源像素在屏幕上占据多少面积:

    qreal ratioX = width() /  float(image2Draw.width());
    qreal ratioY = height() / float(image2Draw.height());
    double pixel_width  = qtransform_matrixWorld.m11()*ratioX;
    double pixel_height = qtransform_matrixWorld.m11()*ratioY;
    

    如果我们知道 pixel_height,我们可以像这样设置 QFont::pixelSize:

    QFont font = painter->font();
    font.setPixelSize(pixel_height/5);
    painter->setFont(font);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 2013-07-28
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      相关资源
      最近更新 更多