Documentos de Académico
Documentos de Profesional
Documentos de Cultura
April 5, 2018
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.
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
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 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
100.0
According to the graph, you can see that the training ended at the time No. 8.
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)
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.
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.]
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
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