-
Notifications
You must be signed in to change notification settings - Fork 0
/
resistor_calculate.py
276 lines (218 loc) · 11.7 KB
/
resistor_calculate.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# import the necessary packages
import sys
from tkinter import filedialog
import numpy as np
import cv2
import math
import tkinter as tk
from PIL import ImageTk, Image
def process_resistor_image():
# Load the image
file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg *.jpeg *.png")])
image = cv2.imread(file_path)
# Convert image from BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
rows,columns,channels = image.shape
#rows,columns,channels
#print the shape of image
print ('Image shape: ',image.shape)
#get mean color value of the image
mean_color = cv2.mean(image)
#print the mean
print ('mean: ',mean_color)
#make 3 new arrays with size equal to number of columns in small image
numbers_of_dark_pixels_in_each_column = [0 for i in range(columns)]
addition_of_row_numbers_having_dark_pixels_in_each_column = [0 for i in range(columns)]
average_of_row_numbers_having_dark_pixels_in_each_column = [0 for i in range(columns)]
#travel row by row downwards in each column from left to right in small image
global num
for x in range(0,columns):
num = 0
for y in range(0,rows):
#get pixel value in small image and assign to a new variable res
res = image[y,x]
#res is empty if no image loaded, then continue
if res is None:
continue
#if the respecitve values stored in res are greater than half of the respective values in the mean color of the small image
#then pass (these are the light parts of the image)
#if not, increment num by 1 and set respective array position of avgy equal to column number as the value already stored plus y
if res[0] > mean_color[0] * 0.5 and res[1] > mean_color[1] * 0.5 and res[2] > mean_color[2] * 0.5:
pass
else:
num += 1
addition_of_row_numbers_having_dark_pixels_in_each_column[x] += y
#once the iteration of rows along a column is over,
#if num is greater than 2, set value in nums at index equal to column number, as num
#and set value in avgy at index equal to column number, as (value already there divided by num)
if num > 2:
numbers_of_dark_pixels_in_each_column[x] = num
average_of_row_numbers_having_dark_pixels_in_each_column[x]=((addition_of_row_numbers_having_dark_pixels_in_each_column[x]) / num)
average_dark_pixels = 0
count = 0
for x in range(0, columns):
if numbers_of_dark_pixels_in_each_column[x] > 0:#if nums(column number) is greater than 0, then do this
average_dark_pixels += numbers_of_dark_pixels_in_each_column[x] #add the values in nums array that are greater than 0
count += 1 #count the number of values greater than 0
#gettin row and column values to extract out part of image and draw rectangle
x_min = -1
x_max = 0
if count > 0: #if there are values greater than 0 in nums array
average_dark_pixels /= count #get the average times the num has been greater than 0 by dividing total by number of values greater than 0
for x in range(0,columns):
if numbers_of_dark_pixels_in_each_column[x] > average_dark_pixels + 15: #if nums(column number) is greater than ((average times the num has been greater than 0) + 15)
if x_min < 0:#this will happen only in first iteration as minx is -1 and after that minx is updated to x
x_min = x #set minx as respective index of nums array
x_max = x # set maxx as respective index of nums array
if x_min >= 0: #if minx is greater than or equal to 0 (ie. respective index of nums array saved above is higher than 0)
y_min = average_of_row_numbers_having_dark_pixels_in_each_column[x_min]
y_max = average_of_row_numbers_having_dark_pixels_in_each_column[x_max]
x_min_2 = x_min - 20
if x_min_2 < 0:
x_min_2 = 0
if x_min_2 > len(average_of_row_numbers_having_dark_pixels_in_each_column) - 1:
x_min_2 = len(average_of_row_numbers_having_dark_pixels_in_each_column) - 1
x_max_2 = x_max + 20
if x_max_2 < 0:
x_max_2 = 0
if x_max_2 > len(average_of_row_numbers_having_dark_pixels_in_each_column) - 1:
x_max_2 = len(average_of_row_numbers_having_dark_pixels_in_each_column) - 1
ratio = (average_of_row_numbers_having_dark_pixels_in_each_column[x_max_2] - average_of_row_numbers_having_dark_pixels_in_each_column[x_min_2]) / ((x_max_2) - (x_min_2))
y_min = (int) (ratio * (x_min - x_min_2) + average_of_row_numbers_having_dark_pixels_in_each_column[x_min_2])
y_max = (int) (ratio * (x_max - x_max_2) + average_of_row_numbers_having_dark_pixels_in_each_column[x_max_2])
w = x_max - x_min#width of part of image to be extracted out
if w > 10 and ratio < 0.7 and ratio > -0.7:
x1=min(x_min,x_max)
x2=max(x_min,x_max)
y1=min(y_min+15,y_max+15,y_max-15,y_min-15)
y2=max(y_min+15,y_max+15,y_max-15,y_min-15)
res = cv2.resize(image[y1:y2, x1:x2],(columns,50),fx=0, fy=0, interpolation = cv2.INTER_NEAREST)#cut out the colour bands part from the image
cv2.rectangle(image, (x1,y1), (x2,y2),(0, 255, 0),2); #draw a green rectangle where the part of image was extracted on original image
n = 3
res_rows,res_columns,res_channels = res.shape
background = [0 for k in range(n)]
for x in range(res_columns-50,res_columns):
value = res[0, x]
for i in range(0,3):
background[i] += value[i] / 50.0
background_distances = [0 for k in range(res_columns)]
average_distance = 0
for x in range(0,res_columns-5):# -5 removed
c1 = background
c2 = res[0,x]
background_distances[x] = (math.pow(c1[0] - c2[0], 2)
+ math.pow(c1[1] - c2[1], 2)
+ math.pow(c1[2] - c2[2], 2))/100
average_distance += (background_distances[x] / res_columns)
res= cv2.fastNlMeansDenoising(res, None, 10, 7, 30)
res = cv2.GaussianBlur(res,(15,15),0)
res = cv2.GaussianBlur(res,(15,15),0)
cv2.imshow('res',cv2.cvtColor(res, cv2.COLOR_BGR2RGB))
#the brighter values of colors
bright_codes = [(0, 0, 0), #black
(156, 102, 51), #brown
(255, 0, 0), #red
(255, 102, 0), #orange
(255, 255, 0), #yellow
(0, 255, 0), #green
(0, 0, 255), #blue
(200, 0, 255), #violet
(128, 128, 128), #gray
(255, 255, 255)] #white
#the darker values of colors
dark_codes = [(20, 20, 20), #black
(71, 53, 38), #brown
(204, 0, 0), #red
(255, 51, 0), #orange
(255, 204, 102), #yellow
(30, 200, 50), #green
(40, 73, 86), #blue
(110, 0, 51), #violet
(73, 65, 62), #gray
(250, 250, 250)] #white
color_name = ["black","brown","red","orange","yellow","green","blue","violet","gray","white"]
#getting colors of the bands
coldet = [0]*res_columns
for x in range(0,res_columns):
if background_distances[x] > average_distance :
col = res[0, x]
mini = 100000
minc = -1
for c in range(0,len(dark_codes)):
c2 = dark_codes[c]
distance = (math.pow(col[0] - c2[0], 2)
+ math.pow(col[1] - c2[1], 2)
+ math.pow(col[2] - c2[2], 2))
if distance < mini:
mini = distance
minc = c
coldet[x] = minc
if minc >= 0:
pass
else:
coldet[x] = -1
print("coldet",coldet)
numconti = 0
numcodes = 0
sumcodes = [0]*res_columns
result = [0 for k in range(n)]
found = False
for x in range(0,res_columns):
if coldet[x] == -1 and numconti > 20:
sumc = [0]*len(dark_codes)
for i in range(0,numconti - 20):
sumc[sumcodes[i]] += 1
print("sumc",sumc)
maxnum = 0
code = -1
for i in range(0,len(dark_codes)):
if sumc[i] > maxnum:
maxnum = sumc[i]
code = i
if code is not -1:
#display colours and colour names
cv2.rectangle(image, ((int)(numcodes*columns/4),rows-40), ((int)((numcodes +1 )*columns/4),rows),bright_codes[code],-1)#Original
cv2.putText(image, color_name[code], ((int)(numcodes * columns / 4), rows-10),cv2.FONT_HERSHEY_DUPLEX, 1.0, (0, 0, 0))
result[numcodes] = code
print("code", code)
numcodes += 1
if numcodes >= 3:
found = True
break
numconti = 0
sumcodes = [0]*res_columns
elif coldet[x] >= 0:
if numconti > 10:
sumcodes[numconti - 10] = coldet[x]
if numconti == 10:
pass
numconti += 1
else:
numconti = 0
print("result",result)
final_result = result[0] * 10 + result[1]
final_result *= math.pow(10, result[2])
resistance = final_result
unit = " Ohm"
if resistance >= 1000.0:
resistance /= 1000
unit = "k Ohm"
if resistance >= 1000000.0:
resistance /= 1000000
unit = "m Ohm"
answer = f'{resistance:.1f}' + unit
cv2.putText(image, answer, (50, 50),cv2.FONT_HERSHEY_DUPLEX, 1.0, (0, 0, 0))
print("Resistance : ",answer)
final = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imshow("Final Output", final)
# Create the main application window
window = tk.Tk()
window.title("Resistor Image Processor")
# Create a label to display the image
image_label = tk.Label(window)
image_label.pack()
# Create a button to process the image
process_button = tk.Button(window, text="Process Image", command=process_resistor_image)
process_button.pack()
# Run the main GUI event loop
window.mainloop()