How to antialias an image displayed in ImageView

192 views
Skip to first unread message

Oli N

unread,
Feb 20, 2022, 3:47:52 AM2/20/22
to pyqtgraph
Hi everyone-

I hope someone can help me here.

I am displaying 2D numpy arrays in an ImageView widget and am trying to antialias the image - my users tend to zoom in and see the ugly pixels.
low-res-noAA.png
While I have seen people use this:

    pg.setConfigOptions(antialias=True)
 in conjunction with ImageItem within a GraphicsView, I owuld like to use the volume view (with slider) that you get with the ImageView but you don't get with ImageItem.


I haven't found a way to antialias image within an ImageView.

Perhaps there is something Qt/PySide or a more raw OpenGL option I can use to do this?  Unfortunately I do not have a deep enough grasp on this to go any further...

Or perhaps I just don't know how to get the slider in the ImageItem to view slices of a 3D dataset (a stack of 2d images in this case).

Any help would be much appreciated.

Cheers-
 Oli

here's my minimal code:

import sys
from PySide2.QtWidgets import (
    QApplication,
    QHBoxLayout,
    QMainWindow,
    QWidget,
)
import pyqtgraph as pg
import numpy as np

pg.setConfigOptions(antialias=True)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.cw = QWidget(self)
        self.cw.setAutoFillBackground(True)
        self.setCentralWidget(self.cw)

        self.layout = QHBoxLayout()
        self.cw.setLayout(self.layout)

        self.DcmImgWidget = MyImageWidget(parent=self)
        self.layout.addWidget(self.DcmImgWidget)

        self.show()


class MyImageWidget(pg.ImageView):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui.histogram.hide()
        self.ui.roiBtn.hide()
        self.ui.menuBtn.hide()

        # 5 frames of 50x50 random noise ranging from -500 to 500
        img = (1000 * np.random.normal(size=(5, 50, 50))) - 500
        self.setImage(img)


def main():
    app = QApplication()
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

 

Oli N

unread,
Feb 28, 2022, 12:32:20 AM2/28/22
to pyqtgraph
In case someone asks the same question in the future...

I was asking the wrong question - I should have been trying to use something like _blur_ rather than antialias.

I just add the Qt graphics effect and apply this to the ImageItem within the ImageView.
https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QGraphicsEffect.html#more in case like me you didn't know anything about the Qt graphics effects.

My only problem now is that the blur seems to be a fairly expensive operation, and either I try to force use of hardware acceleration (how?) or I selectively enable blur- e.g. disable during pan/zoom and re-enable on releasing mouse buttons- however it is still slow to pan/zoom when zoomed in enough to enable blur.
I think I'm going to be forced into trying something like VisPy or VTK (which I'd rather not do) unless there are any suggestions here.

-Oli
Reply all
Reply to author
Forward
0 new messages