gabriel-ing / SimpliPyTEM

Package to make analysis of transmission electron microscopy images simple.
GNU General Public License v3.0
11 stars 5 forks source link

gaussian_filter of long videos error #17

Open gabriel-ing opened 1 year ago

gabriel-ing commented 1 year ago

There appears to be an error with openCV gaussian_filter causing an error with my filtering. With one video with >900 frames, the gaussian_filter errored with:


AxisError Traceback (most recent call last) Cell In[6], line 2 1 cc=vid.clip_contrast() ----> 2 cc = cc.gaussian_filter(3) 3 cc = cc.make_scalebar() 4 cc = cc.convert_to_8bit()

File ~/opt/anaconda3/envs/Test/lib/python3.10/site-packages/SimpliPyTEM/MicroVideo_class.py:1222, in MicroVideo.gaussian_filter(self, kernal) 1220 #print(kernal) 1221 swapped = cv.GaussianBlur(swapped, (kernal,kernal), 0) -> 1222 swapped = np.swapaxes(swapped, 0, 2) 1224 filtered_object = deepcopy(self)
1225 filtered_object.frames = swapped

File <__array_function__ internals>:200, in swapaxes(*args, **kwargs)

File ~/opt/anaconda3/envs/Test/lib/python3.10/site-packages/numpy/core/fromnumeric.py:594, in swapaxes(a, axis1, axis2) 550 @array_function_dispatch(_swapaxes_dispatcher) 551 def swapaxes(a, axis1, axis2): 552 """ 553 Interchange two axes of an array. 554 (...) 592 593 """ --> 594 return _wrapfunc(a, 'swapaxes', axis1, axis2)

File ~/opt/anaconda3/envs/Test/lib/python3.10/site-packages/numpy/core/fromnumeric.py:57, in _wrapfunc(obj, method, *args, kwds) 54 return _wrapit(obj, method, *args, *kwds) 56 try: ---> 57 return bound(args, kwds) 58 except TypeError: 59 # A TypeError occurs if the object does have such a method in its 60 # class, but its signature is not identical to that of NumPy's. This (...) 64 # Call _wrapit from within the except clause to ensure a potential 65 # exception has a traceback chain. 66 return _wrapit(obj, method, *args, **kwds)

AxisError: axis2: axis 2 is out of bounds for array of dimension 2

gabriel-ing commented 1 year ago

The error here is coming from microvideo:

1221 swapped = cv.GaussianBlur(swapped, (kernal,kernal), 0) -> 1222 swapped = np.swapaxes(swapped, 0, 2) 1224 filtered_object = deepcopy(self) 1225 filtered_object.frames = swapped

The issue is the cv.GaussianBlur is returning a 2 dimensional array rather than a 3D array. leading to an Axis error

gabriel-ing commented 1 year ago

Further investigation suggests its caused by having too many frames of the video, for example:

arr = np.random.rand(1500,1500,500) g = cv.GaussianBlur(arr, (3,3), 0)

Does not error, however:

arr = np.random.rand(1500,1500,500) g = cv.GaussianBlur(arr, (3,3), 0)

Raises an error.

Weirdly, performing this here raised an error rather than returning a 2D array, but I assume that reducing the number of frames will still work. Certainly all the videos with fewer frames I have used have worked fine.

gabriel-ing commented 1 year ago

When I first wrote the function, I filtered each frame individually in a big loop, the same way the median filter works, this presumably is the way to bipass this error. The question is, how big a speed loss is this?

If there is a major speed loss I can always use an exception to catch it, something like: `

    try:
          swapped = cv.GaussianBlur(swapped, (kernal,kernal), 0)
           swapped = np.swapaxes(swapped, 0, 2)
    except:   ***I would like to put the specific error here but the openCV error I'm getting is unnamed

    for frame in frames:
       GaussianBlur(frame)

` etc...