-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import cv2 | ||
import numpy as np | ||
from collections import OrderedDict | ||
from scipy.spatial import distance as dist | ||
|
||
class ColorLabeler: | ||
def __init__(self): | ||
self.colorsToIgnore = ['darkgray', 'darkblue', 'black'] | ||
|
||
colors = OrderedDict({ | ||
"red": (255, 0, 0), | ||
"green": (0, 255, 0), | ||
"blue": (0, 0, 255), | ||
"black": (0, 0, 0), | ||
"yellow": (255, 255, 0), | ||
"darkblue": (0, 0, 71), | ||
"gray": (96, 96, 96), | ||
"darkgray": (70, 70, 70), | ||
"white": (255, 255, 255), | ||
}) | ||
|
||
self.lab = np.zeros((len(colors), 1, 3), dtype="uint8") | ||
self.colorNames = [] | ||
|
||
for (i, (name, rgb)) in enumerate(colors.items()): | ||
self.lab[i] = rgb | ||
self.colorNames.append(name) | ||
|
||
self.lab = cv2.cvtColor(self.lab, cv2.COLOR_RGB2LAB) | ||
|
||
def label(self, img, c): | ||
mask = np.zeros(img.shape[:2], dtype="uint8") | ||
cv2.drawContours(mask, [c], -1, 255, -1) | ||
mask = cv2.erode(mask, None, iterations=2) | ||
mean = cv2.mean(img, mask=mask)[:3] | ||
|
||
minDist = (np.inf, None) | ||
|
||
for (i, row) in enumerate(self.lab): | ||
d = dist.euclidean(row[0], mean) | ||
|
||
if d < minDist[0]: | ||
minDist = (d, i) | ||
|
||
return self.colorNames[minDist[1]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ Flask==1.0.2 | |
gevent==1.3.6 | ||
pillow==5.3.0 | ||
imutils==0.5.3 | ||
scipy==1.3.1 | ||
matplotlib |