Adding alpha to ImageItem causes image to not display

55 views
Skip to first unread message

Alexander Tomlinson

unread,
Aug 3, 2017, 2:18:23 PM8/3/17
to pyqtgraph
Hello,

I have a GraphicsLayoutWidget with two ImageItems overlayed. The bottom one is just a constant feed from a camera. Ocasionally, I capture a frame and overlay with some transparency using opacity option of setImage. This works without issue. However, I want to threshold this overlay at times, and after adding an alpha value the image is no longer displayed. Any help or insight would be appreciated. I've pasted below the relevant code. 

import numpy as np
import pyqtgraph as pg
from PyQt4 import QtGui, QtCore


class Frame(QtGui.QWidget):
    """
    Main frame.
    """
    def __init__(self):
        # a bunch of init stuff
        self.do_threshold = False
        self.overlay_opacity = 0.5

    def update_viewer(self, img_data):
        """
        Makes call to update image viewer.

        :param img_data: image data
        """
        self.image_viewer.update(img_data)

   
def update_overlay(self, img_data_overlay):
        """
        Updates the overlay.

        :param img_data_overlay: image data of the overlay
        """
        if self.do_threshold:
            threshed = np.copy(img_data_overlay)
            threshed
[threshed < self.overlay_threshold] = 0
           
            # make into RGBA
            threshed = np.dstack([threshed] * 3)
            alpha
= np.full((threshed.shape[1], threshed.shape[0], 1), 0.5, np.uint8)
           
# commenting out below line, so that it's RGB, makes it appear again
            threshed = np.concatenate([threshed, alpha], axis=2)

           
self.image_viewer.update_overlay(threshed)

       
else:
            self.image_viewer.update_overlay(img_data_overlay, overlay_opacity=self.overlay_opacity)


class ImageWidget(pg.GraphicsLayoutWidget, object):
    """
    Widget for the display from the camera.
    """
    def __init__(self, parent=None):
        super(ImageWidget, self).__init__(parent=parent)

        vb
= self.addViewBox(row=1, col=1)

       
self.viewer = pg.ImageItem()
       
self.viewer_overlay = pg.ImageItem()

        vb
.addItem(self.viewer)
        vb
.addItem(self.viewer_overlay)
       
       
# set overlay to always be on top
        self.viewer_overlay.setZValue(1)

        vb
.setAspectLocked(True)

   
def update(self, img_data):
        """
        Updates image

        :param img_data: image data
        """
        self.viewer.setImage(img_data, autoLevels=True)

   
def update_overlay(self, img_data_overlay, overlay_opacity=None):
        """
        Updates overlay data

        :param img_data_overlay: image data of overlay
        :param overlay_opacity: transparency of the overlay
        """
        if overlay_opacity is not None:
            self.viewer_overlay.setImage(img_data_overlay, autoLevels=True, opacity=overlay_opacity)
       
else:
            self.viewer_overlay.setImage(img_data_overlay, autoLevels=True)


Alexander Tomlinson

unread,
Aug 4, 2017, 12:09:16 PM8/4/17
to pyqtgraph
Is it perhaps related to this? Though I'm not sure how.
Reply all
Reply to author
Forward
0 new messages