-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_transform.py
More file actions
34 lines (31 loc) · 1.05 KB
/
log_transform.py
File metadata and controls
34 lines (31 loc) · 1.05 KB
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
"""
Applies logarithmic transformation on an image. Takes image, levels and c as parameters.
Returns the new image
"""
import numpy as np
import imutil
def log_transform(im, levels, c):
#create output image
im_out = np.zeros((im.shape[0],im.shape[1],3), dtype=np.uint8)
#if image is gray
if imutil.is_gray(im):
im_out = np.zeros((im.shape[0],im.shape[1]), dtype=np.uint8)
#since opencv handles gray images like 3-channel image, reduce the channel size
im = imutil.correct_gray(im)
#get height and width
#normalize the image to perform logarithmic transform
im_out = im/(levels-1)
#apply the formula
im_out = c*np.log(1+im_out)
#normalize in range 0...255
im_out = (im_out / im_out.max()) * (levels-1)
else:
#convert to YCbCr
im_out = imutil.rgb2ycbcr(im)
dMat = im_out[:, :, 0].astype(float)
dMat = dMat/(levels-1)
dMat = c*np.log(1 + (dMat))
im_out[:, :, 0] = (dMat / dMat.max()) * (levels-1)
#convert back to RGB
im_out = imutil.ycbcr2rgb(im_out)
return np.uint8(im_out)