-
Notifications
You must be signed in to change notification settings - Fork 7
/
max_local_saturation.py
54 lines (40 loc) · 1.44 KB
/
max_local_saturation.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
import cv2
import math
import numpy as np
SCALE_LOCAL_SAT = 10
def exc_div_zero(sat_min, sat_max):
if(sat_max):
return (sat_min*1.) / sat_max
else:
return 1
def Max_Local_Sat(image):
height, width, __ = image.shape
sat_image = np.zeros((height, width))
# 1*1 saturation calculate
for i in range(height):
for j in range(width):
sat_image[i, j] = 1 - exc_div_zero(np.min(image[i, j, :]), np.max(image[i, j, :]))
# default SCALE_LOCAL_SAT = 10
pad_size = int(SCALE_LOCAL_SAT/2)
# default padding val = 0
padimage = np.pad(sat_image, ((pad_size,pad_size), (pad_size,pad_size)), 'constant')
sat_image = np.zeros((height, width))
for i in range(height):
for j in range(width):
patch = padimage[i:(i+SCALE_LOCAL_SAT), j:(j+SCALE_LOCAL_SAT)]
sat_image[i, j] = np.max(patch)
return sat_image
if __name__ == '__main__':
fn = 'results/forest.jpg'
src = cv2.imread(fn)
# Image normalization
I = src.astype('float64') / 255.
# Patch_Size = 10*10
# Get local-saturation
saturation = Max_Local_Sat(I)
# Adaptive window size (for large input img)
# cv2.namedWindow('Max_Local_Saturation', cv2.WINDOW_NORMAL)
# cv2.imshow('Max_Local_Saturation', saturation)
cv2.imwrite('results/forest_saturat.jpg', saturation * 255, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
cv2.waitKey(0)