Pyqtgraph: assign a color to a mask

128 views
Skip to first unread message

Jean-Pierre Morichon

unread,
May 18, 2020, 1:13:07 PM5/18/20
to pyqtgraph
Hi All,

I have a greylevel image and a binary mask.
I would like to display the binary mask (img2 below) as overlay to the greylevel image but with a specific color for the binary mask (let's say red)

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

pg.setConfigOptions(imageAxisOrder='row-major')

app = QtGui.QApplication([])

win = pg.GraphicsWindow()
vb = win.addViewBox()
img1 = pg.ImageItem(pg.np.random.normal(size=(100, 100)))
img2 = np.zeros_like(img1, dtype='uint8')
img2[40:60, 40:60] = 255
vb.addItem(img1)
vb.addItem(img2)
img2.setZValue(10)
img2.setOpacity(0.5)
img2.scale(10,10)
win.show()

QtGui.QApplication.instance().exec_()

I have looked for a solution on the net for 3 days wi th no success: the only color stuff is about colormaps but I may be wrong but this is not what I am lookign for.

Your help would be appreciated

Edmondo Giovannozzi

unread,
May 18, 2020, 2:05:58 PM5/18/20
to pyqtgraph
You were almost there:


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

pg.setConfigOptions(imageAxisOrder='row-major')

app = QtGui.QApplication([])

win = pg.GraphicsWindow()
vb = win.addViewBox()

win.addItem(vb) 
v1 = pg.np.random.normal(size=(100, 100))

img1 = pg.ImageItem(v1)
v2 = np.zeros(v1.shape+(3,), dtype='uint8')
v2[:,:,0:3] = 128 
v2[40:60, 40:60, 0:3] = 255
v2[0,:,0]= 255
v2[:,0,1]= 255
v2[-1,:,0]= 255
v2[:,-1,1]= 255

img2 = pg.ImageItem(v2)

vb.addItem(img1)

vb.addItem(img2)
img2.setZValue(10)
img2.setOpacity(0.5)
#img2.scale(10,10)
win.show()
QtGui.QApplication.instance().exec_()


I also added  borders of different colours. 

Edmondo Giovannozzi

unread,
May 18, 2020, 2:11:03 PM5/18/20
to pyqtgraph

This is more similar to that that you have written:

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

pg.setConfigOptions(imageAxisOrder='row-major')

app = QtGui.QApplication([])

win = pg.GraphicsWindow()
vb = win.addViewBox()
v1 = pg.np.random.normal(size=(100, 100))

img1 = pg.ImageItem(v1)
v2 = np.zeros_like(v1, dtype='uint8')

v2[40:60, 40:60] = 255
img2 = pg.ImageItem(v2)

vb.addItem(img1)
vb.addItem(img2)
img2.setZValue(10)
img2.setOpacity(0.5)
#img2.scale(10,10)
win.show()

QtGui.QApplication.instance().exec_()


Il giorno lunedì 18 maggio 2020 19:13:07 UTC+2, Jean-Pierre Morichon ha scritto:

Jean-Pierre Morichon

unread,
May 18, 2020, 2:11:10 PM5/18/20
to pyqtgraph
Very nice, Edmondo!

Thank you very much for your help and for being so quick to answer!
I truly appreciate!
Reply all
Reply to author
Forward
0 new messages