-
Notifications
You must be signed in to change notification settings - Fork 1
/
parameter_selector.py
153 lines (129 loc) · 5.19 KB
/
parameter_selector.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
140
141
142
143
144
145
146
147
148
149
150
151
152
import cv2
import numpy as np
import math
import random
from collections import deque
import circles_utils
# Penalty for the percentage of white pixels on the goal function
PENALTY_WHITE = 5
# Penalty for the percentage of black pixels on the goal function
PENALTY_BLACK = 1
# Number of iterations for the iterated local search (ils)
MAX_IT = 50
# Number of iterations for the local search
MAX_ITER_LS = 6
# Minimum value for the param1 of the cv2.HoughCircles function
LOWER_P1 = 8
# Minimum value for the param2 of the cv2.HoughCircles function
LOWER_P2 = 10
# Minimum value for the minimum distance between the centers of circles in the cv2.HoughCircles function
LOWER_MIN_DIST = 100
# ===Description: ----------------------------------------------------------------------------------
# calculates the cost of the objective function
# ---Arguments: ------------------------------------------------------------------------------------
# black: percentage of black pixels
# white: percentage of white pixels
# --------------------------------------------------------------------------------------------------
def calc_cost(black, white):
return PENALTY_BLACK*black + PENALTY_WHITE*white
# ===Description: ----------------------------------------------------------------------------------
# applies a local search on the neighborhood of the given solution
# ---Arguments: ------------------------------------------------------------------------------------
# img_bin: image with a threshold applied
# sol: initial solution
# cost: initial cost
# min_rad: minimum radius
# max_rad: maximum radius
# --------------------------------------------------------------------------------------------------
def local_search(img_gray_scale,img_bin,sol,cost,min_rad,max_rad):
cur_sol = sol.copy()
cur_cost = cost
best_sol = sol.copy()
best_cost = cost
improved = False
ratio = random.random() + 0.1
inc_dec = random.randint(0,1)
pos = random.randint(0,3)
for i in range(1,MAX_ITER_LS+1):
n = cur_sol[pos]
if(inc_dec == 0):
if(pos == 0):
cur_sol[pos] = max(int(n - (n*ratio)/i),1)
elif(pos == 1):
cur_sol[pos] = max(int(n - (n*ratio)/i),LOWER_MIN_DIST-1)
elif(pos == 2):
cur_sol[pos] = max(int(n - (n*ratio)/i),LOWER_P1-1)
elif(pos == 3):
cur_sol[pos] = max(int(n - (n*ratio)/i),LOWER_P2-1)
else:
cur_sol[pos] = int(n + (n*ratio)/i)
circles = cv2.HoughCircles(img_gray_scale, cv2.HOUGH_GRADIENT, cur_sol[0], cur_sol[1], param1=cur_sol[2], param2=cur_sol[3], minRadius=min_rad, maxRadius=max_rad)
if(circles is None):
continue
circles = np.int16(np.around(circles))
c = circles_utils.select_circle(img_bin,circles[0])
p_black, p_white = circles_utils.count_pixels(img_bin,c)
cur_cost = calc_cost(p_black,p_white)
# Acceptance criteria
if(cur_cost <= best_cost):
improved = True
best_cost = cur_cost
best_sol = cur_sol.copy()
elif(improved == True):
break
return best_sol
# ===Description: ----------------------------------------------------------------------------------
# Implementation of the Iterated Local Search Meta-Heuristic
# ---Arguments: ------------------------------------------------------------------------------------
# img_bin: image with a threshold applied
# sol: initial solution
# min_rad: minimum radius
# max_rad: maximum radius
# --------------------------------------------------------------------------------------------------
def ils(img_gray_scale,img_bin,sol,min_rad,max_rad):
cur_sol = sol.copy()
best_sol = cur_sol.copy()
circles = cv2.HoughCircles(img_gray_scale, cv2.HOUGH_GRADIENT, cur_sol[0], cur_sol[1], param1=cur_sol[2], param2=cur_sol[3], minRadius=min_rad, maxRadius=max_rad)
circles = np.int16(np.around(circles))
c = circles_utils.select_circle(img_bin,circles[0])
p_black, p_white = circles_utils.count_pixels(img_bin,c)
cur_cost = calc_cost(p_black,p_white)
best_cost = cur_cost
pos = random.randint(0,3)
hist_pos = deque([pos])
for i in range(MAX_IT):
inc_dec = random.randint(0,1)
ratio = random.randrange(1,6)/10
while(hist_pos.count(pos) > 0):
pos = random.randint(0,3)
hist_pos.append(pos)
if(len(hist_pos) > 0):
hist_pos.popleft()
cur_sol = best_sol.copy()
n = cur_sol[pos]
if(inc_dec == 1):
cur_sol[pos] += int(n*ratio)
else:
if(pos == 0):
cur_sol[pos] = max(int(n - (n*ratio)),1)
elif(pos == 1):
cur_sol[pos] = max(int(n - (n*ratio)),LOWER_MIN_DIST-1)
elif(pos == 2):
cur_sol[pos] = max(int(n - (n*ratio)),LOWER_P1-1)
elif(pos == 3):
cur_sol[pos] = max(int(n - (n*ratio)),LOWER_P2-1)
#--------------------------------------------------------------------------------
# Local search
cur_sol = local_search(img_gray_scale,img_bin,cur_sol,cur_cost,min_rad,max_rad)
circles = cv2.HoughCircles(img_gray_scale, cv2.HOUGH_GRADIENT, cur_sol[0], cur_sol[1], param1=cur_sol[2], param2=cur_sol[3], minRadius=min_rad, maxRadius=max_rad)
if(circles is None):
continue
circles = np.int16(np.around(circles))
c = circles_utils.select_circle(img_bin,circles[0])
p_black, p_white = circles_utils.count_pixels(img_bin,c)
cur_cost = calc_cost(p_black,p_white)
# Acceptance
if(cur_cost < best_cost):
best_cost = cur_cost
best_sol = cur_sol.copy()
return best_sol