DIPlib / diplib

Quantitative Image Analysis in C++, MATLAB and Python
https://diplib.org
Apache License 2.0
211 stars 48 forks source link

PyDip: VarianceFilter results compared to varif on DIPimage #112

Closed ronligt closed 1 year ago

ronligt commented 2 years ago

[created for Oscar D. Herrera, reported via info@diplib.org]

Component

Describe the bug

I am doing some image analysis on PyDip and comparing it to my results on DIPimage. Both versions are 3.2.0 (python 3.9.10 and Matlab 2021a).

I am doing a comparison of my results from a VarianceFilter performed on an image – both py and DIPimage have the same default condition. For comparison, I am computing the overall mean pixel value in both 16-bit image results – I assume if they are doing the same thing then they should read out the same value. This is what I am doing:

Python

a = dip.ImageReadTIFF(file_name)
a0 = np.uint16(a) # for inital comparison
a0_mean = np.mean(a0)
# perform filter
b = dip.VarianceFilter(a)/5
b0 = np.uint16(b)
b0_mean = np.mean(b0)

MATLAB

a = readim(file_name)
a0m = uint16(a) # for initial comparison
a0_mean = mean(a0m, ‘all’)
% perform filter
b = varif(a)/5
b0 = uint16(b)
b0_mean = mean(b0,’all’)

Results: both a0_mean values are the same, which is great. Unfortunately, the b0_mean values do not match.

comment: what is interesting is that doing just the filter without the division (for example, b = varif(a)) in both software does result in matching mean values so there is something going on when performing division on a Variance dipimage result. Maybe it has to do with the datatype changing to sfloat, I don’t quite understand.

Any thoughts on what I could be doing wrong – you comments would be much appreciated and sorry for the consecutive questions.

System information:

crisluengo commented 2 years ago

Maybe MATLAB and Python have different ways to cast to uint16?

Try comparing np.mean(b) vs mean(b,'all') (i.e. skip the cast to uint16). If they are the same, the issue is in the cast.