Está en la página 1de 10

Color Sorter

Edisson Andres Coy Contreras, Julian Ramiro Cicuamia Ayala

April 5, 2018

1 Color classifier (Logistic regression)


The objective of the following activity is to perform a training, to classify a color (blue).
For this training the logistic regression algorithm will be implemented and applied in Python
language.
In the first place, 10 images will be chosen and we choose an RGB and HSV value. 2 pixels per
image will be chosen where one of them is blue and the other is very different from this color. In
total there are 20 data for the training and for each color scale. That is to say in the format RGB
will be 20 values for R, 20 for G and 20 for B, in the same way sera in the case of HSV.
After obtaining the data, we will proceed to bury the algorithm to determine the color blue.

1.1 1. Regression Logistics in RGB format


1.1.1 1.1. Data Obtension
To get the RGB values of each pixel in each image, we import the following libraries.

In [1]: from mpl_toolkits.mplot3d import Axes3D


import matplotlib.pyplot as plt
import numpy as np
import math
import cv2

Followed by them will read the 10 images chosen.

In [2]: m1 = cv2.imread('1.jpg')
m2 = cv2.imread('2.jpg')
m3 = cv2.imread('3.jpg')
m4 = cv2.imread('4.jpg')
m5 = cv2.imread('5.jpg')
m6 = cv2.imread('6.jpg')
m7 = cv2.imread('7.jpg')
m8 = cv2.imread('8.jpg')
m9 = cv2.imread('9.jpg')
m10 = cv2.imread('10.jpg')

Already finished the process of having read the images we choose the pixels.

1
In [3]: b1, g1, r1 = m1[90, 174]/255
b11, g11, r11 = m1[100, 141]/255
b2, g2, r2 = m2[128, 70]/255
b12, g12, r12 = m2[43, 130]/255
b3, g3, r3 = m3[151, 148]/255
b13, g13, r13 = m3[294, 197]/255
b4, g4, r4 = m4[72, 105]/255
b14, g14, r14 = m4[4, 6]/255
b5, g5, r5 = m5[122, 79]/255
b15, g15, r15 = m5[205, 83]/255
b6, g6, r6 = m6[81, 59]/255
b16, g16, r16 = m6[170, 148]/255
b7, g7, r7 = m7[163, 73]/255
b17, g17, r17 = m7[205, 120]/255
b8, g8, r8 = m8[163, 68]/255
b18, g18, r18 = m8[119, 127]/255
b9, g9, r9 = m9[318, 127]/255
b19, g19, r19 = m9[222, 226]/255
b10, g10, r10 = m10[487, 453]/255
b20, g20, r20 = m10[340, 558]/255
After choosing the pixels, we extract their RGB values and put each one in a vector.
In [4]: R = np.array([r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20])
G = np.array([g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13,g14,g15,g16,g17,g18,g19,g20])
B = np.array([b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20])

print('R',R)
print('G',G)
print('B',B)
R [0.39607843 0.33333333 0.15294118 0.04313725 0.02352941 0.08627451
0.00392157 0.00392157 0.24705882 0.23529412 0.00392157 1.
0.51764706 0.01960784 0.42352941 0.84705882 0.65882353 0.12156863
0.91764706 0.85882353]
G [0.54509804 0.60784314 0.47843137 0.28235294 0.34901961 0.30196078
0.15686275 0.35294118 0.30980392 0.66666667 0.00392157 1.
0.51764706 0.01176471 0.36862745 0.06666667 0.61176471 0.12156863
0.42352941 0.99607843]
B [0.79215686 0.8 0.79215686 0.49411765 0.56078431 0.56470588
0.38039216 0.71372549 0.68627451 0.76470588 0.01176471 1.
0.50980392 0.05490196 0.36470588 0.29803922 0.55686275 0.12156863
0.23529412 0.43137255]

The vector "Y" output, is the one that indicates if the pixel taken is blue, if it is blue will be "1",
if it is not "0".
In [5]: y = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

2
1.1.2 1.2. Scatter diagram
It graphs a small diagram of dispersion of the RGB data, where the values of red color are the
zeros in the output "Y" and those of blue are the ones.

In [6]: fig = plt.figure()


ax = fig.add_subplot(111, projection='3d')
ax.scatter(R[0:10], G[0:10], B[0:10], c='b', marker='o')
ax.scatter(R[10:20], G[10:20], B[10:20], c='r', marker='o')

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

1.1.3 1.3. Apply Logistic regression


As in the previous exercise (workshop number 3), to the data obtained from RGB, the algorithm
was applied in Python created in the workshop number 3 and will be trained to classify the color
blue. We’ll have an alpha value of 0.2.

In [7]: a = 1
h = 0
epoca = epoca = np.arange(1, 21)
itera = 20

3
alpha = 0.2

B0 = 0.0
B1 = 0.0
B2 = 0.0
B3 = 0.0

def clasificador(x):
if(x < 0.5):
return 0
else:
return 1
porcentaje = np.ones(20)
porcentaje = porcentaje*100
pred = np.zeros([20])
error = np.zeros([20])
predictionu = np.zeros([20])
n = len(R)
while(a==1):
for i in range(n):
output = B0 + B1 * R[i] + B2 * G[i] + B3 * B[i]
fexpo = math.exp(-(output))
prediction = round((1/(1+fexpo)),3)
predictionu[i] = prediction

pre = clasificador(prediction)
pred[i]=(pre)
error[i] = abs(y[i]-pre)
if error[i] == 0:
continue
else:
B0 = B0 + alpha * (y[i] - prediction) * prediction * (1 - prediction) * 1.0
B1 = B1 + alpha * (y[i] - prediction) * prediction * (1 - prediction) * R[i]
B2 = B2 + alpha * (y[i] - prediction) * prediction * (1 - prediction) * G[i]
B3 = B3 + alpha * (y[i] - prediction) * prediction * (1 - prediction) * B[i]
k = sum(error)
numpredcorre = itera - k
numpredcorre = numpredcorre/itera
numpredcorre = numpredcorre * 100
if k==0:
a=0
else:
a=1
porcentaje[h]= numpredcorre
h = h + 1

print('porcentaje',porcentaje)

4
print('epoca',epoca)

print('Out',predictionu)
print('Prediccion',pred)

porcentaje [ 95. 85. 80. 80. 80. 80. 95. 100. 100. 100. 100. 100. 100. 100.
100. 100. 100. 100. 100. 100.]
epoca [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
Out [0.502 0.503 0.504 0.501 0.503 0.502 0.5 0.505 0.502 0.503 0.495 0.498
0.496 0.495 0.495 0.489 0.495 0.495 0.487 0.491]
Prediccion [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

The error percentage for each period is plotted.

In [8]: plt.plot(epoca, porcentaje, 'b')


plt.grid(color = '0.5', linestyle = '--', linewidth = 1)
plt.show()

The values of the estimators when the training process ended was:

In [9]: print('B0',B0)
print('B1',B1)
print('B2',B2)
print('B3',B3)

B0 -0.022247453800000018
B1 -0.04706450844470588

5
B2 0.0016296198933333141
B3 0.058713087176470585

We calculate the final error percentage.

In [10]: numpredcorre = itera - k


numpredcorre = numpredcorre/itera
numpredcorre = numpredcorre * 100
accuracy = numpredcorre
print(accuracy)

100.0

According to the graph, you can see that the training ended at the time No. 8.

1.2 2. Regression Logistics in HSV format


Now it will perform the same procedure used for the RGB format, but now in HSV, then let’s start.

1.2.1 2.1 Data Decteccion


Convert from RGB format to HSV format

In [11]: im1 = cv2.cvtColor(m1, cv2.COLOR_BGR2HSV)


im2 = cv2.cvtColor(m2, cv2.COLOR_BGR2HSV)
im3 = cv2.cvtColor(m3, cv2.COLOR_BGR2HSV)
im4 = cv2.cvtColor(m4, cv2.COLOR_BGR2HSV)
im5 = cv2.cvtColor(m5, cv2.COLOR_BGR2HSV)
im6 = cv2.cvtColor(m6, cv2.COLOR_BGR2HSV)
im7 = cv2.cvtColor(m7, cv2.COLOR_BGR2HSV)
im8 = cv2.cvtColor(m8, cv2.COLOR_BGR2HSV)
im9 = cv2.cvtColor(m9, cv2.COLOR_BGR2HSV)
im10 = cv2.cvtColor(m10, cv2.COLOR_BGR2HSV)

The HSV values are obtained from each image.

In [12]: h1, s1, v1 = im1[58, 104]/255


h11, s11, v11 = im1[116, 38]/255
h2, s2, v2 = im2[40, 80]/255
h12, s12, v12 = im2[148, 60]/255
h3, s3, v3 = im3[112, 130]/255
h13, s13, v13 = im3[280, 183]/255
h4, s4, v4 = im4[89, 149]/255
h14, s14, v14 = im4[10, 41]/255
h5, s5, v5 = im5[175, 69]/255
h15, s15, v15 = im5[92, 64]/255
h6, s6, v6 = im6[202, 107]/255

6
h16, s16, v16 = im6[120, 77]/255
h7, s7, v7 = im7[283, 145]/255
h17, s17, v17 = im7[215, 15]/255
h8, s8, v8 = im8[107, 135]/255
h18, s18, v18 = im8[150, 184]/255
h9, s9, v9 = im9[93, 251]/255
h19, s19, v19 = im9[250, 176]/255
h10, s10, v10 = im10[272, 198]/255
h20, s20, v20 = im10[485, 437]/255

H = np.array([h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18,h19,h20])
S = np.array([s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20])
V = np.array([v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20])

print('H',H)
print('S',S)
print('V',V)

H [0.64705882 0.43137255 0.67843137 0.64705882 0.52156863 0.45098039


0.43137255 0.47058824 0.67843137 0.64705882 0. 0.40784314
0.11764706 0.05490196 0.4 0.40392157 0. 0.41176471
0.09019608 0.37647059]
S [0.05882353 0.10196078 0.06666667 0.04705882 0.14509804 0.09019608
0.10980392 0.05098039 0.10196078 0.07058824 0. 0.71372549
0.01960784 0.36078431 0.93333333 0.70588235 0. 1.
0.04705882 0.71764706]
V [0.1372549 0.11372549 0.23137255 0.16862745 0.18823529 0.2627451
0.10588235 0.15294118 0.15294118 0.11372549 0.91764706 0.5372549
0.42352941 0.14117647 0.57254902 0.47843137 1. 0.65098039
0.99607843 0.76470588]

It uses the same "Y" output, which is used in the RGB format, since the pixels that were chosen
for the HSV format were made in the same order.

1.2.2 2.2 Scatter diagram


In [13]: fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(H[0:10], S[0:10], V[0:10], c='b', marker='o')
ax.scatter(H[10:20], S[10:20], V[10:20], c='r', marker='o')

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

7
1.2.3 2.3 Apply Logistic regression
In [14]: B0 = 0.0
B1 = 0.0
B2 = 0.0
B3 = 0.0
e=1
epoca = np.arange(1, 21)
itera = 20
alpha = 0.2
porcentaje = np.ones(20)
porcentaje = porcentaje*100
pred = np.zeros([20])
error = np.zeros([20])
predictionu = np.zeros([20])
z = 0
n = len(H)
while(e==1):
for i in range(n):
output = B0 + B1 * H[i] + B2 * S[i] + B3 * V[i]
fexpo = math.exp(-(output))
prediction = round((1/(1+fexpo)),3)
predictionu[i] = prediction

pre = clasificador(prediction)
pred[i]=(pre)

8
error[i] = abs(y[i]-pre)
if error[i] == 0:
continue
else:
B0 = B0 + alpha * (y[i] - prediction) * prediction * (1 - prediction) * 1.0
B1 = B1 + alpha * (y[i] - prediction) * prediction * (1 - prediction) * H[i
B2 = B2 + alpha * (y[i] - prediction) * prediction * (1 - prediction) * S[i
B3 = B3 + alpha * (y[i] - prediction) * prediction * (1 - prediction) * V[i
k = sum(error)
numpredcorre = itera - k
numpredcorre = numpredcorre/itera
numpredcorre = numpredcorre * 100
if k==0:
e=0
else:
e=1
porcentaje[z] = numpredcorre
z = z + 1

print('porcentaje',porcentaje)
print('epoca',epoca)

print('Out',predictionu)
print('Prediccion',pred)

porcentaje [ 95. 90. 90. 95. 100. 100. 100. 100. 100. 100. 100. 100. 100. 100.
100. 100. 100. 100. 100. 100.]
epoca [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
Out [0.505 0.503 0.504 0.505 0.503 0.502 0.503 0.503 0.505 0.505 0.493 0.496
0.498 0.498 0.494 0.496 0.493 0.494 0.493 0.494]
Prediccion [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

The error percentage for each period is plotted

In [15]: plt.plot(epoca, porcentaje, 'b')


plt.grid(color = '0.5', linestyle = '--', linewidth = 1)
plt.show()

9
The values of the estimators when the training process ended was:
In [16]: print('B0',B0)
print('B1',B1)
print('B2',B2)
print('B3',B3)
B0 0.0007908935999999984
B1 0.03747253899607843
B2 -0.022404457239215688
B3 -0.02949928911372549

We calculate the final error percentage.


In [17]: numpredcorre = itera - k
numpredcorre = numpredcorre/itera
numpredcorre = numpredcorre * 100
accuracy = numpredcorre
print(accuracy)
100.0

According to the graph, you can see that the training ended at the time No. 6.

1.3 3. Summary
• Training in the HSV format, less in entering the blue color, unlike training in the RGB format.

10

También podría gustarte