-
Notifications
You must be signed in to change notification settings - Fork 0
/
r2-f.py
139 lines (122 loc) · 4.26 KB
/
r2-f.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
import scipy.spatial as sp
import cv2
import glob
import matplotlib.pyplot as plt
import time
from matplotlib import colors
# return arrays of images ORI, GT
def loadImages(I, color=cv2.COLOR_BGR2RGB):
Iout = []
imageFolder = './sfa/ORI/'
truthFolder = './sfa/GT/'
training = range(1, 783)
testing = range(951, 1119)
if (I == "trainImages"):
for j in training:
filename = "{}img ({}).jpg".format(imageFolder, j)
Iout.append(cv2.cvtColor(cv2.imread(filename), color))
elif (I == "trainGTs"):
for j in training:
filename = "{}img ({}).jpg".format(truthFolder, j)
Iout.append(cv2.cvtColor(cv2.imread(filename), color))
elif (I == "testImages"):
for j in testing:
filename = "{}img ({}).jpg".format(imageFolder, j)
Iout.append(cv2.cvtColor(cv2.imread(filename), color))
elif (I == "testGTs"):
for j in testing:
filename = "{}img ({}).jpg".format(truthFolder, j)
Iout.append(cv2.cvtColor(cv2.imread(filename), color))
else:
print("Not valid dataset")
return Iout
def getBitwiseMatrix(Y):
Y = np.asarray(Y)
threshold = 1
mask = np.where((Y[:,:,:, 0] >= threshold) &
(Y[:,:,:, 1] >= threshold) &
(Y[:,:,:, 2] >= threshold))
Y[:,:,:,:] = 0
Y[mask] = 1
return Y
def preProcessImages(X, Y):
X = np.asarray(X)
Y = np.asarray(Y)
threshold = 27
mask = np.where((Y[:, :, :, 0] >= threshold) &
(Y[:, :, :, 1] >= threshold) &
(Y[:, :, :, 2] >= threshold))
Y[:, :, :, :] = 0
Y[mask] = 255
skinMask = Y
skinMask[mask] = 1
skin = cv2.bitwise_and(X, X, mask=skinMask)
notSkinMask = Y
notSkinMask[:, :, :, :] = 1
notSkinMask[mask] = 0
notSkin = cv2.bitwise_and(X, X, mask=notSkinMask)
return (skinMask, notSkinMask, skin, notSkin)
def extractHist(sets, categories, color, name="RGB"):
for i, col in enumerate(color):
for j, images in enumerate(sets):
# cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) → hist
hist = cv2.calcHist(images, [i], None, [256], [28, 256])
plt.plot(hist, label=categories[j])
plt.xlim([0, 256])
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=2, mode="expand", borderaxespad=0.)
plt.title(name+" "+col)
plt.minorticks_on()
plt.grid(which='major', linestyle='-', linewidth='0.75', color='black')
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='gray')
plt.savefig('./results/{}-{}-2.png'.format(name,col))
plt.gcf().clear()
def generateHistograms():
colors = [
["YCrCb", cv2.COLOR_BGR2YCrCb, ("Y", "Cr", "Cb")],
["HSV", cv2.COLOR_BGR2HSV, ("H", "S", "V")],
["LAB", cv2.COLOR_BGR2LAB, ("L", "A", "B")],
["RGB", cv2.COLOR_BGR2RGB, ("R", "G", "B")]
]
for i in range(0, len(colors)):
color = colors[i][1]
# load images
Xtrain = loadImages(I="trainImages", color=color)
Ytrain = loadImages(I="trainGTs", color=color)
# pre-process images
skinMask, notSkinMask, skin, notSkin = preProcessImages(Xtrain, Ytrain)
# generate histogram
extractHist([skin, notSkin], categories=('skin', 'not skin'), color=colors[i][2], name=colors[i][0])
def generate2DHistogramCrCb():
color = cv2.COLOR_BGR2YCrCb
print ('loading')
# load images
Xtrain = loadImages(I="trainImages", color=color)
Ytrain = loadImages(I="trainGTs", color=color)
print ('loaded')
print ('processing')
# pre-process images
skinMask, notSkinMask, skin, notSkin = preProcessImages(Xtrain, Ytrain)
print ('processed')
sets = [skin, notSkin]
for j, images in enumerate(sets):
# fig2 = plt.figure()
images = images.reshape(-1,3)
Cr = images[:,1]
Cb = images[:,2]
plt.hist2d(Cb, Cr, bins=256, range=[[80, 140], [
120, 160]], cmap='gist_yarg')
plt.xlabel('Cb')
plt.ylabel('Cr')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Counts')
plt.savefig('./results/histogram2d-{}.png'.format( j))
plt.gcf().clear()
# import datasets
trainImages = glob.glob('./dataset/train/ORI/*.jpg')
trainGTs = glob.glob('./dataset/train/GT/*.jpg')
testImages = glob.glob('./dataset/test/ORI/*.jpg')
testGTs = glob.glob('./dataset/test/GT/*.jpg')
generate2DHistogramCrCb()
# generateHistograms()