How to display clickable RGB image similar to pyqtgraph ImageView?

廉价感情. 提交于 2019-12-20 03:44:06

问题


Despite not being a proficient GUI programmer, I figured out how to use the pyqtgraph module's ImageView function to display an image that I can pan/zoom and click on to get precise pixel coordinates. The complete code is given below. The only problem is that ImageView can apparently only display a single-channel (monochrome) image.

My question: How do I do EXACTLY the same thing as this program (ignoring histogram, norm, and ROI features, which I don't really need), but with the option to display a true color image (e.g., the original JPEG photo)?

    import numpy as np
    from pyqtgraph.Qt import QtCore, QtGui
    import pyqtgraph as pg
    import matplotlib.image as mpimg

    # Load image from disk and reorient it for viewing

    fname = 'R0000187.JPG'    # This can be any photo image file
    photo=np.array(mpimg.imread(fname))
    photo = photo.transpose()
    # select for red color and extract as monochrome image
    img = photo[0,:,:]  # WHAT IF I WANT TO DISPLAY THE ORIGINAL RGB IMAGE?

    # Create app
    app = QtGui.QApplication([])

    ## Create window with ImageView widget
    win = QtGui.QMainWindow()
    win.resize(1200,800)
    imv = pg.ImageView()
    win.setCentralWidget(imv)
    win.show()
    win.setWindowTitle(fname)


    ## Display the data 
    imv.setImage(img)

    def click(event):
        event.accept()  
        pos = event.pos()
        print (int(pos.x()),int(pos.y()))

    imv.getImageItem().mouseClickEvent = click

    ## Start Qt event loop unless running in interactive mode.
    if __name__ == '__main__':
        import sys
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()

回答1:


pyqtgraph.ImageView does support rgb / rgba images. For example:

import numpy as np
import pyqtgraph as pg
data = np.random.randint(255, size=(100, 100, 3))
pg.image(data)

..and if you want to display the exact image data without automatic level adjustment:

pg.image(data, levels=(0, 255))



回答2:


As pointed out by Luke, ImageView() does display RGB, provided the correct array shape is passed. In my sample program, I should have used photo.transpose([1,0,2]) to keep the RGB in the last dimension rather than just photo.transpose(). When ImageView is confronted with an array of dimension (3, W, H), it treats the array as a video consisting of 3 monochrome images, with a slider at the bottom to select the frame.

(Corrected to incorporate followup comment by Luke, below)



来源:https://stackoverflow.com/questions/25956163/how-to-display-clickable-rgb-image-similar-to-pyqtgraph-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!