-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCD.py
104 lines (73 loc) · 2.69 KB
/
DCD.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
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
#!env python
# -*- coding: utf-8 -*-
# Dominant Color Descriptor of 2D image.
# file created by A.Chabira
# original matlab implementaion by https://github.com/Molen1945
# License: Public Domain
#
# reference:
# Shao, H., Wu, Y., Cui, W., Zhang, J. (2008). Image retrieval based on MPEG-7 dominant color descriptor. Proceedings of the 9th International Conference for Young Computer Scientists, ICYCS 2008, 753–757. https://doi.org/10.1109/ICYCS.2008.89
import numpy as np
import imageio
import skimage.color
def DCD(img, img_type='RGB', order=8):
''' Calculates the Dominant Color Descriptor described in the MPEG-7 standard
arguments
img : either HSV image, or RGB image
img_type : input image type either 'HSV' or 'RGB'
order : n first dominant colors
return
2 numpy arrays, one with size equal to input image, and second ###72
'''
if img_type == 'RGB':
img = skimage.color.rgb2hsv(img)
# HSV component sorting
H = 360 * img[:, :, 0]
S = img[:, :, 1]
V = img[:, :, 2]
# HSV Component Quantization
Hq = QunatizeH(H)
Sq = QunatizeSV(S)
Vq = QunatizeSV(V)
# HSV matrix generation
C = np.round(9 * Hq + 3 * Sq + Vq)
m, n = C.shape
color, _ = np.histogram(C, bins=72, range=(0, 72))
Pi = color / (m * n)
M = order
# Pi values are sorted in descending order and stored in Qj
# I : the index of the Pi values that have been sorted in descending order
I = np.argsort(Pi)[::-1] # indices of sorted elements
Qj = np.sort(Pi)[::-1]
# Take the first 8 values of Qj
Qj = Qj[0:M]
Pi1 = np.zeros(72)
I = I[0:M]
Pi1[I] = Qj
P = Pi1 / sum(Qj)
return (P, C)
def QunatizeH(H):
''' Calculates the Quantization of the Hue channel of an image
arguments
H : Hue channel of HSV image
return
numpy array with size equal to input image
'''
bins = np.array([20, 40, 75, 155, 190, 270, 295, 316])
ix = np.digitize(H, bins=bins)
return ix
def QunatizeSV(SV):
''' Calculates the Quantization of either the Saturation or Value channels of an image
arguments
SV : either S or V channels of an image
return
numpy array with size equal to input image
'''
bins = np.array([1, 0.7, 0.2])
ix = np.digitize(SV, bins=bins, right=True)
return ix
if __name__ == '__main__':
input = 'https://raw.githubusercontent.com/mikolalysenko/lena/master/lena.png'
I = imageio.imread(input, pilmode='RGB')
P, C = DCD(I, img_type='RGB', order=8)
print('done')