Hey guys:

Good news! I just fixed the bug by myself. Attached is my modified 
implementation, which works fine with pyqtgraph 0.10.0. I use v1a to 
display the original image and v1b to display the mask.

Thanks,
Xingyu
 
在 2020年4月6日星期一 UTC+8下午10:45:41,Xingyu Liu写道:
>
> Hi guys:
>
> Thank you so much for your helpful discussion! I'm also a new guy to 
> pyqtgraph and trying to find a way to get binary ROI, but when I ran the 
> attached testROI.py, an error occurs:
> '''
> Traceback (most recent call last):
>   File "testROI.py", line 37, in <module>
>     mpossx = roi.getArrayRegion(possx,imgview).astype(int)
>   File 
> "D:\Program\Miniconda3\envs\bs\lib\site-packages\pyqtgraph\graphicsItems\ROI.py",
>  
> line 2027, in getArrayRegion
>     sliced = ROI.getArrayRegion(self, data, img, axes=axes, 
> fromBoundingRect=True, **kwds)
>   File 
> "D:\Program\Miniconda3\envs\bs\lib\site-packages\pyqtgraph\graphicsItems\ROI.py",
>  
> line 1104, in getArrayRegion
>     shape, vectors, origin = self.getAffineSliceParams(data, img, axes, 
> fromBoundingRect=fromBR)
>   File 
> "D:\Program\Miniconda3\envs\bs\lib\site-packages\pyqtgraph\graphicsItems\ROI.py",
>  
> line 1129, in getAffineSliceParams
>     raise Exception("ROI and target item must be members of the same 
> scene.")
> Exception: ROI and target item must be members of the same scene.
> '''
> I guess it is because the function 'ROI.getArrayRegion' has been 
> changed/updated over the years. But as a newbie, I don't quite understand 
> how to fix it ... Could you please give me some suggestions? Thank you very 
> much!!
>
> Best,
> Xingyu 
>
> 在 2015年5月18日星期一 UTC+8下午11:50:20,Julien Lhermitte写道:
>>
>> Great I'm glad to hear this! Thanks for the information by the way, I 
>> didn't know you could do this but it's a great feature to have (add a 
>> function to a class after the fact just like you can do with attributes).
>>
>> I'm not sure which way is more pythonic but I would still rather create a 
>> new class with this new method that inherits all methods members of the ROI 
>> class used in the long run. This might make things easier to keep track of. 
>> If you're not familiar, look up inheritance (like here 
>> <http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/>),
>>  
>> it's pretty simple to implement. But this is a nice quick fix.
>>
>> On Monday, May 18, 2015 at 3:32:31 AM UTC-4, L. Smash wrote:
>>>
>>> Hi,
>>>
>>> thanks for the nice example! I think I've got it now :)
>>>
>>> What I actually was trying to do is to put your code into a 
>>> function/method (like getArrayRegion) and dynamically add it to ROI classes 
>>> of pyqtgraph (PolyLineROI, Ellipse.ROI etc.). The problem I stumbled across 
>>> was an issue with bound\unbound methods (see here 
>>> <http://www.ianlewis.org/en/dynamically-adding-method-classes-or-class-instanc>),
>>>  
>>> that's why I got confused with the self variable. Thanks again for your 
>>> explanation and the example, it helped me very much!
>>>
>>> Best, 
>>> Lars
>>>
>>> Am Freitag, 15. Mai 2015 16:45:27 UTC+2 schrieb Julien Lhermitte:
>>>>
>>>> oops the dimensions were wrong (ROI went out of bounds of data array), 
>>>> I have reattached the correct example.
>>>>
>>>> Also, the *self* variable in my previous example would be the 
>>>> *theObjectCallingIt* in my current example. It can have whatever name 
>>>> you want.
>>>>
>>>> (doing this on my spare time here and there :-( but hope it helps)
>>>>
>>>>
>>>> On Friday, May 15, 2015 at 9:40:12 AM UTC-4, Julien Lhermitte wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>>      I am not completely sure I understand your question but I think 
>>>>> your concern is the self variable. Yes, this was just a quick snippet I 
>>>>> copied and pasted.
>>>>> First, I believe whenever you call a function in an object, the first 
>>>>> parameter given is always a reference to the object itself.
>>>>>
>>>>> So if tiger is part of class Cat, and has member function getColor(), 
>>>>> this function:
>>>>> tiger.getColor() is actually a shortcut for Cat.getColor(tiger), so 
>>>>> when declaring a function in a class, the function should be declared as:
>>>>>
>>>>> def getColor(theObjectCallingIt, otherparameters...):
>>>>>
>>>>> But let's ignore that for now and just create some self contained 
>>>>> code. I have attached it in this message. It generates random data but 
>>>>> it's 
>>>>> always a good idea to use some sort of image with features to test (just 
>>>>> uncomment the relevant lines for that and install PIL image lib for 
>>>>> example)
>>>>>
>>>>> I hope this helps!
>>>>>
>>>>> julien
>>>>>
>>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/615c52df-f262-405c-a9e3-43efed048eeb%40googlegroups.com.
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui

#random data with random shape
data = np.random.random((1000,1000))

#get data from an image instead, uncommment this
#from PIL import Image
#inputImage='duck.jpg'
#pilImage=Image.open(inputImage)
#data = np.asarray(pilImage)
#data = np.sum(data,axis=2)

#mask same shape as data
mask = np.zeros(data.shape)
#prepare points for dummy ROI
points = np.array([[10,10],[400,10],[400,600],[10,600],[10,10]])

#quick setup of image plotting
pg.mkQApp()
w = pg.GraphicsLayoutWidget()
# imgold = pg.image(data)
# img = pg.image(data)
v1a = w.addPlot(row=0,col=0, lockAspect=True)
img1a = pg.ImageItem(data)
v1a.addItem(img1a)
v1a.autoRange()

v1b = w.addPlot(row=1, col=0, lockAspect=True)
img1b = pg.ImageItem()## View ROI
v1b.addItem(img1b)
v1b.autoRange()

w.show()

#make dummy ROI from points
roi = pg.PolyLineROI(points,closed=True)
#add ROI
v1a.addItem(roi)
# img.addItem(roi)
# imgold.addItem(roi)
# imgview = img.getImageItem()

#Now do the selection
cols,rows = data.shape
m = np.mgrid[:cols,:rows]
possx = m[0,:,:]# make the x pos array
possy = m[1,:,:]# make the y pos array
possx.shape = cols,rows
possy.shape = cols,rows
mpossx = roi.getArrayRegion(possx,img1a).astype(int)
mpossx = mpossx[np.nonzero(mpossx)]#get the x pos from ROI
mpossy = roi.getArrayRegion(possy,img1a).astype(int)
mpossy = mpossy[np.nonzero(mpossy)]# get the y pos from ROI
mask[mpossx,mpossy] = data[mpossx,mpossy]

#uncomment these lines to see the result. Comment them to see
#before the result
img1b.setImage(mask)
v1b.autoRange()
print(np.shape(mask))

#for more questions etc, use the help function
#ex, help(img) will tell you all about member functions and vars
# of the class associated to img
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Reply via email to