-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_preprocess.py
More file actions
40 lines (27 loc) · 957 Bytes
/
Copy pathimage_preprocess.py
File metadata and controls
40 lines (27 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Image pre-processing
from scipy import ndimage
import numpy as np
global SIZE
SIZE = 96
# Normalization transformation
def imanorm(image, new_min=0, new_max=255):
return (image-min(image)) * (new_max-new_min)/new_max + new_min
# Histogram equalization
def histeq(image,nbr_bins=256):
""" Histogram equalization of a grayscale image. """
# get image histogram
imhist,bins = np.histogram(image,nbr_bins,normed=True)
cdf = imhist.cumsum() # cumulative distribution function
cdf = 255 * cdf / cdf[-1] # normalize
# use linear interpolation of cdf to find new pixel values
ima = np.interp(image,bins[:-1],cdf)
return ima
# Image derivatives
def imaderiv(ima):
ima = ima.reshape(SIZE, SIZE)
imx = np.zeros(ima.shape)
ndimage.filters.sobel(ima,1,imx)
imy = np.zeros(ima.shape)
ndimage.filters.sobel(ima,1,imy)
magnitude = np.sqrt(imx**2+imy**2)
return magnitude.flatten()