Color extraction
This commit is contained in:
parent
8cdde12d1e
commit
49b5eef6b2
|
@ -0,0 +1 @@
|
|||
*.png
|
213
3/contours.py
213
3/contours.py
|
@ -5,12 +5,19 @@
|
|||
#
|
||||
# Distributed under terms of the MIT license.
|
||||
|
||||
|
||||
# cpy = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
|
||||
# cv.drawContours(cpy, [np.int0(rect)], 0, (0, 0, 255), 2)
|
||||
# cv.imshow('arst', cpy)
|
||||
# cv.waitKey()
|
||||
|
||||
import copy
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
def main():
|
||||
for k in range(1, 11):
|
||||
# for k in [5]:
|
||||
filename = 'set/A' + str(k) + '.png'
|
||||
src = cv.imread(filename)
|
||||
|
||||
|
@ -20,34 +27,21 @@ def main():
|
|||
continue
|
||||
|
||||
src = crop_resistor(src, box)
|
||||
h, w, c = src.shape
|
||||
|
||||
if len(src) is 0:
|
||||
continue
|
||||
|
||||
img = np.zeros((1, w, 3), np.uint8)
|
||||
for i in range(w):
|
||||
sumB = 0
|
||||
sumR = 0
|
||||
sumG = 0
|
||||
for j in range(h):
|
||||
sumB += src[j, i, 0]
|
||||
sumR += src[j, i, 1]
|
||||
sumG += src[j, i, 2]
|
||||
|
||||
img[0, i] = [ sumB/h, sumR/h, sumG/h ]
|
||||
|
||||
kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])
|
||||
img = cv.filter2D(img, -1, kernel)
|
||||
simple = simplify_image(src)
|
||||
find_bands(simple)
|
||||
|
||||
# src = src.mean(3)
|
||||
# print src[0]
|
||||
# s = None
|
||||
# cv.reduce(src, s, 0, cv.REDUCE_SUM, cv.CV_32S)
|
||||
|
||||
# cv.imshow('arst', img)
|
||||
cv.imwrite(str(k) + '.png', img)
|
||||
cv.imshow(str(k), simple)
|
||||
cv.waitKey()
|
||||
# cv.imwrite(str(k) + '.png', simple)
|
||||
|
||||
|
||||
def rotate_pt(box, off, phi):
|
||||
|
@ -90,14 +84,15 @@ def find_resistor(img):
|
|||
img = cv.dilate(img, kernel, 2)
|
||||
img = cv.erode(img, kernel, 2)
|
||||
img, contours, hierarchy = cv.findContours(img, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
|
||||
rect = None
|
||||
|
||||
if len(contours) is not 0:
|
||||
max_index, max_area = max(enumerate([cv.contourArea(x) for x in contours]), key = lambda x: x[1])
|
||||
max_contour = contours[max_index]
|
||||
rect = cv.minAreaRect(max_contour)
|
||||
return cv.boxPoints(rect)
|
||||
rect = cv.boxPoints(rect)
|
||||
|
||||
return None
|
||||
return rect
|
||||
|
||||
|
||||
def crop_resistor(img, box):
|
||||
|
@ -113,13 +108,13 @@ def crop_resistor(img, box):
|
|||
phi = -phi
|
||||
|
||||
rotate_pt(box, center, -phi)
|
||||
box = np.int0(box)
|
||||
box = sort_coords(box)
|
||||
|
||||
w, h, c = img.shape
|
||||
rotM = cv.getRotationMatrix2D(center, np.degrees(phi), 1.0)
|
||||
img = cv.warpAffine(img, rotM, (h*2, w*2))
|
||||
img = cv.warpAffine(img, rotM, (int(h*1.4), int(w*1.4)))
|
||||
|
||||
box = np.int0(box)
|
||||
x = box[0][0]
|
||||
y = box[0][1]
|
||||
w = box[2][0] - box[0][0]
|
||||
|
@ -129,97 +124,139 @@ def crop_resistor(img, box):
|
|||
img = img[y:(y+h), x:(x+w)]
|
||||
|
||||
return img
|
||||
cv.imwrite(str(i) + '.png', img)
|
||||
|
||||
|
||||
def simplify_image(img):
|
||||
# kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])
|
||||
# img = cv.filter2D(img, -1, kernel)
|
||||
|
||||
h, w, c = img.shape
|
||||
simple = np.zeros((10, w, 3), np.uint8)
|
||||
for i in range(w):
|
||||
sumB = 0
|
||||
sumR = 0
|
||||
sumG = 0
|
||||
for j in range(h):
|
||||
sumB += img[j, i, 0]
|
||||
sumR += img[j, i, 1]
|
||||
sumG += img[j, i, 2]
|
||||
|
||||
for k in range(10):
|
||||
simple[k, i] = [ sumB/h, sumR/h, sumG/h ]
|
||||
|
||||
N = 16;
|
||||
simple /= N;
|
||||
simple *= N;
|
||||
# simple = cv.filter2D(simple, -1, kernel)
|
||||
return simple
|
||||
|
||||
|
||||
def find_bands(img):
|
||||
i = 0
|
||||
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV);
|
||||
h, w, c = img.shape
|
||||
|
||||
# while hsv[0, i][0] >= 0 and i < w:
|
||||
while i < w:
|
||||
check_color(hsv[0, i])
|
||||
i += 1
|
||||
|
||||
|
||||
def check_color(px):
|
||||
print px
|
||||
|
||||
# def check_color_range(h, s, v, col):
|
||||
# if (h > col.lowH)
|
||||
|
||||
|
||||
colors = {
|
||||
'black': {
|
||||
'iLowH': 0,
|
||||
'iHighH': 179,
|
||||
'iLowS': 0,
|
||||
'iHighS': 121,
|
||||
'iLowV': 0,
|
||||
'iHighV': 57,
|
||||
'lowH': 0,
|
||||
'highH': 179,
|
||||
'lowS': 0,
|
||||
'highS': 121,
|
||||
'lowV': 0,
|
||||
'highV': 57,
|
||||
},
|
||||
'brown': {
|
||||
'iLowH': 7,
|
||||
'iHighH': 68,
|
||||
'iLowS': 0,
|
||||
'iHighS': 253,
|
||||
'iLowV': 0,
|
||||
'iHighV': 187,
|
||||
'lowH': 3,
|
||||
'highH': 20,
|
||||
'lowS': 200,
|
||||
'highS': 255,
|
||||
'lowV': 80,
|
||||
'highV': 140,
|
||||
},
|
||||
'red': {
|
||||
'iLowH': 0,
|
||||
'iHighH': 4,
|
||||
'iLowS': 124,
|
||||
'iHighS': 255,
|
||||
'iLowV': 74,
|
||||
'iHighV': 255,
|
||||
'lowH': 170,
|
||||
'highH': 5,
|
||||
'lowS': 124,
|
||||
'highS': 255,
|
||||
'lowV': 74,
|
||||
'highV': 255,
|
||||
},
|
||||
'orange': {
|
||||
'iLowH': 7,
|
||||
'iHighH': 85,
|
||||
'iLowS': 216,
|
||||
'iHighS': 255,
|
||||
'iLowV': 151,
|
||||
'iHighV': 255,
|
||||
'lowH': 3,
|
||||
'highH': 20,
|
||||
'lowS': 200,
|
||||
'highS': 255,
|
||||
'lowV': 141,
|
||||
'highV': 255,
|
||||
},
|
||||
'yellow': {
|
||||
'iLowH': 10,
|
||||
'iHighH': 73,
|
||||
'iLowS': 250,
|
||||
'iHighS': 255,
|
||||
'iLowV': 225,
|
||||
'iHighV': 255,
|
||||
'lowH': 20,
|
||||
'highH': 30,
|
||||
'lowS': 190,
|
||||
'highS': 255,
|
||||
'lowV': 100,
|
||||
'highV': 200,
|
||||
},
|
||||
'green': {
|
||||
'iLowH': 19,
|
||||
'iHighH': 165,
|
||||
'iLowS': 151,
|
||||
'iHighS': 255,
|
||||
'iLowV': 165,
|
||||
'iHighV': 194,
|
||||
'lowH': 45,
|
||||
'highH': 80,
|
||||
'lowS': 120,
|
||||
'highS': 255,
|
||||
'lowV': 30,
|
||||
'highV': 150,
|
||||
},
|
||||
'blue': {
|
||||
'iLowH': 74,
|
||||
'iHighH': 149,
|
||||
'iLowS': 111,
|
||||
'iHighS': 255,
|
||||
'iLowV': 86,
|
||||
'iHighV': 255,
|
||||
'lowH': 90,
|
||||
'highH': 120,
|
||||
'lowS': 111,
|
||||
'highS': 255,
|
||||
'lowV': 50,
|
||||
'highV': 255,
|
||||
},
|
||||
'purple': {
|
||||
'iLowH': 121,
|
||||
'iHighH': 150,
|
||||
'iLowS': 159,
|
||||
'iHighS': 194,
|
||||
'iLowV': 97,
|
||||
'iHighV': 164,
|
||||
'lowH': 140,
|
||||
'highH': 169,
|
||||
'lowS': 100,
|
||||
'highS': 220,
|
||||
'lowV': 10,
|
||||
'highV': 150,
|
||||
},
|
||||
'gray': {
|
||||
'iLowH': 0,
|
||||
'iHighH': 80,
|
||||
'iLowS': 0,
|
||||
'iHighS': 80,
|
||||
'iLowV': 34,
|
||||
'iHighV': 139,
|
||||
'lowH': 0,
|
||||
'highH': 80,
|
||||
'lowS': 0,
|
||||
'highS': 85,
|
||||
'lowV': 100,
|
||||
'highV': 190,
|
||||
},
|
||||
'white': {
|
||||
'iLowH': 0,
|
||||
'iHighH': 85,
|
||||
'iLowS': 0,
|
||||
'iHighS': 43,
|
||||
'iLowV': 144,
|
||||
'iHighV': 255,
|
||||
'lowH': 0,
|
||||
'highH': 85,
|
||||
'lowS': 0,
|
||||
'highS': 30,
|
||||
'lowV': 150,
|
||||
'highV': 255,
|
||||
},
|
||||
'gold': {
|
||||
'iLowH': 18,
|
||||
'iHighH': 77,
|
||||
'iLowS': 93,
|
||||
'iHighS': 238,
|
||||
'iLowV': 81,
|
||||
'iHighV': 255,
|
||||
'lowH': 15,
|
||||
'highH': 30,
|
||||
'lowS': 160,
|
||||
'highS': 230,
|
||||
'lowV': 100,
|
||||
'highV': 200,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Reference in New Issue