简体   繁体   中英

Image filtering in Python (image normalization)

I want to write code that does image filtering. I use simple 3x3 kernel and then use scipy.ndimage.filters.convolve() function. After filtering, range of the values is -1.27 to 1.12. How to normalize data after filtering? Do I need to crop values (values less then zero set to zero, and greater than 1 set to 1), or use linear normalization? Is it OK if values after filtering are greater than range [0,1]?

>>> import numpy as np
>>> x = np.random.randn(10)
>>> x
array([-0.15827641, -0.90237627,  0.74738448,  0.80802178,  0.48720684,
    0.56213483, -0.34239788,  1.75621007,  0.63168393,  0.99192999])

You could clip out values outside your range although you would lose that information:

>>> np.clip(x,0,1)
array([ 0.        ,  0.        ,  0.74738448,  0.80802178,  0.48720684,
    0.56213483,  0.        ,  1.        ,  0.63168393,  0.99192999])

To preserve the scaling, you can linearly renormalise into the range 0 to 1:

>>> (x - np.min(x))/(np.max(x) - np.min(x))
array([ 0.27988553,  0.        ,  0.6205406 ,  0.64334869,  0.52267744,
    0.55086084,  0.21063013,  1.        ,  0.57702102,  0.71252388])

Is it OK if values after filtering are greater than range [0,1]?

This is really dependant on your use case for the filtered image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM