-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode4.py
More file actions
104 lines (78 loc) · 3.22 KB
/
code4.py
File metadata and controls
104 lines (78 loc) · 3.22 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Preprocessing and trying to intensify the red.
import pandas as pd
from PIL import Image, ImageDraw, ImageFont
import cv2
import numpy as np
from PIL import Image
# Path and image names.
JPGImagesPATH = './Data/vott-csv-export/JPGImages/'
jpg1_str = 'A_3D_L0646_144.jpg'
#im_cv = cv2.imread(str(JPGImagesPATH)+str(jpg1_str))
#cv2.imwrite(str(JPGImagesPATH)+str(jpg1_str), im_cv)
#pil_img = Image.fromarray(im_cv)
# pil_img.save('preprocessing'+str(jpg1_str)+'.jpg')
# Simplest color balancing
# https://gist.github.com/hobson/e3b8805a558d974d48336e133dfb2bbd#file-simple_cb-py
""" White balance (color balance)
Adjust colors to flatten color histogram peaks and broaden color spectrum for better color contrast.
This is also sometimes called white balancing or probability distribution whitening.
References:
- Color balance algorithm in [OpenCV](http://www.morethantechnical.com/2015/01/14/simplest-color-balance-with-opencv-wcode/)
- Ported to python by [DavidYKay](https://github.com/DavidYKay)
- Optimized (`O(N^2)` -> `O(N)`) by @alxrsngartn funded in part by NSF grant number 1722399 to [Aira](http://github.com/aira) (@jmeyers-aira)
- Incorporated into [nlpia](github.com/aira/nlpia) by @hobson
Dependencies:
- Python >= 2.7.8
- OpenCV == 2.4.10
"""
def apply_mask(matrix, mask, fill_value):
masked = np.ma.array(matrix, mask=mask, fill_value=fill_value)
return masked.filled()
def apply_threshold(matrix, low_value, high_value):
low_mask = matrix < low_value
matrix = apply_mask(matrix, low_mask, low_value)
high_mask = matrix > high_value
matrix = apply_mask(matrix, high_mask, high_value)
return matrix
def simple_colorbalance(img, percent):
""" Applies Simple Color balancing to RBG image
Args:
img: numpy array of an image in RGB space
percent: [0, 100], cutoff value for light and dark pixels
Returns:
Color balanced image
Examples:
Flatten (broaden) the color histogram for a cup of coffee photo.
>>> from skimage.data import coffee
>>> img = coffee()
>>> img.std().round(0)
74.0
>>> x = simple_colorbalance(img, 9)
>>> x.std().round(0)
80.0
"""
assert img.shape[2] == 3
assert 0 <= percent <= 100
half_percent = percent / 200
channels = cv2.split(img)
out_channels = []
for channel in channels:
assert len(channel.shape) == 2
# find the low and high precentile values (based on the input percentile)
height, width = channel.shape[:2]
vec_size = width * height
flat = channel.reshape(vec_size)
assert len(flat.shape) == 1
low_val = np.percentile(flat, half_percent * 100)
high_val = np.percentile(flat, (1 - half_percent) * 100)
# saturate below the low percentile and above the high percentile
thresholded = apply_threshold(channel, low_val, high_val)
# scale the channel
normalized = cv2.normalize(
thresholded, thresholded.copy(), 0, 255, cv2.NORM_MINMAX)
out_channels.append(normalized)
return cv2.merge(out_channels)
if __name__ == '__main__':
import doctest
doctest.testmod()
# TODO: preprocessing one image and applying multiple filters and generating new photos to test.