Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding other padding methods #202

Open
MathiasHurst opened this issue May 31, 2020 · 19 comments
Open

Adding other padding methods #202

MathiasHurst opened this issue May 31, 2020 · 19 comments
Assignees

Comments

@MathiasHurst
Copy link

Hi,
is it possible to add different padding methods to ufo? Very helpful would be a padding mode as in python numpy.pad mode=median.
I am aware that speed will be reduced compared to the default opencl "padding" modes.
Bests,
Mathias

@tfarago
Copy link
Contributor

tfarago commented Jun 4, 2020

Which ones would you like to have? Also specify the essence of it (median of the whole image, of the rows/cols...). Do you need them in the pad task or also in the reconstructors?

@tfarago tfarago self-assigned this Jun 4, 2020
@MathiasHurst
Copy link
Author

It is needed for Image correlation. Therefore implementation in the pad task should be sufficient. The opencl padding methods (clamp, camp_to_edge) do not allow to find the maximum of the correlation by a simple theresholding due to background effects. I tested the stuff with np.median, this uses the median of each row / column and for padding, what worked fine. However I was not able to find out which value is written into padded region where there is no column / row end see attached picture. I do not think that this plays a important role, for this we can just use the mean or median value of the image.
python_median_padded

@tfarago
Copy link
Contributor

tfarago commented Jun 4, 2020

BTW, do you have a use case when this is necessary?

@tfarago
Copy link
Contributor

tfarago commented Jun 4, 2020

I am asking because for correlation just zero-padding usually works fine.

@MathiasHurst
Copy link
Author

Hi Tomas, I correlated rescaled fresnel diffraction patterns as described e.g. in https://aip.scitation.org/doi/pdf/10.1063/1.2719653

I also tested zero padding. There was a bright gradient which was stronger than the correlation peak it self.
I attached some images to show you

Image 1 zero padded data
Image 2 Correlation output using python zero padding
Image 3 Correlation output using python median padding
Image 4 Correlation output using ufo clamp to edge padding

I can also try to zero pad in ufo but i did not find the command on read the docs there is only the link to opencls sampler_t documentation.

python_zero_padded-1
python_zero_padded_correlation-1
python_median_padded_correlation-1
ufo_clamp_to_edge_correlation-1

@tfarago
Copy link
Contributor

tfarago commented Jun 4, 2020

And what happens when you apply sobel filter before the correlation?

@MathiasHurst
Copy link
Author

much better, but there is still a stronger peak (see attached picture). I think this can be handled by cropping. But also the maximum is affected by on Pixel after edge detection filter.

Selection_006

@tfarago
Copy link
Contributor

tfarago commented Jun 4, 2020

Don't do LR sobel, do really a 2D magnitude of the derivative and take a look at the peak.

@tfarago
Copy link
Contributor

tfarago commented Jun 4, 2020

see e.g. how scikit-image does 2D sobel

@MathiasHurst
Copy link
Author

i used this scipy.ndimage.sobel but will also try the one you proposed. LR is only the filename of the inital image

@tfarago
Copy link
Contributor

tfarago commented Jun 4, 2020

Don't use scipy, it doesn't do the 2D version, or use scipy and calculate the 2D magnitude yourself.

@tfarago
Copy link
Contributor

tfarago commented Jun 4, 2020

magnitude = np.sqrt(sobel(im, axis=0) ** 2 + sobel(im, axis=1) ** 2)

@MathiasHurst
Copy link
Author

Thanks for help but it is still not sufficent :D Do you have another idea.
Selection_007
Selection_008

@tfarago
Copy link
Contributor

tfarago commented Jun 4, 2020

Please send me an e-mail with a link to the two images you are trying to correlate, I will check it.

@tfarago
Copy link
Contributor

tfarago commented Jun 8, 2020

It's better to first subtract the means of the two images (https://en.wikipedia.org/wiki/Cross-correlation#Zero-normalized_cross-correlation_(ZNCC).

ifft2(fft2(LR - LR.mean()) * np.conjugate(fft2(HR - HR.mean(), s=LR.shape))).real

gives the correct result below (with just zero-padding, the default when padding directly in numpy's fft2 with the s argument).
LR_HR_correlation

@MathiasHurst
Copy link
Author

Thank you! Do you know how to implement this in UFO:

e.g. read path=2196Mn_LR_0p625_region1/rescaled/*.tif number=2500 ! SUBTRACT MEAN ! pad x=0 y=0 width=8192 height=8192 ! ...

I found measure metric=mean and calculate but was not able to pass the result of measure to calculate.

@tfarago
Copy link
Contributor

tfarago commented Jun 8, 2020

Unfortunately, that's not possible right now.

@MathiasHurst
Copy link
Author

Can this be implemented, or do you have an idea for a workaround?

@tfarago
Copy link
Contributor

tfarago commented Jun 9, 2020

It certainly can be implemented, the question is how to make it in such a way that it's general enough for other operations (what if you want to multiply with standard deviation tomorrow?) 😉 I will need to think about it a little. In the meantime, can you remove the mean in python or fiji (both excellent scripting options)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants