Skip to content

Commit

Permalink
Merge branch dev into gat1
Browse files Browse the repository at this point in the history
  • Loading branch information
gattlin1 committed Oct 8, 2019
2 parents 8b131ab + 6f52f04 commit a500faa
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Server/resources/js/mapInspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var imageObj = undefined; //the stitched map image
if(toolTipBead){
const canvas = $('#mapCanvas');
const location = `(${Math.round(toolTipBead[2][0])}, ${Math.round(toolTipBead[2][1])})`;
const radius = Math.round(toolTipBead[2][2]);
let radius = Math.round(toolTipBead[2][2]);
let type = '';
let rgb = `(${Math.round(toolTipBead[0][0])}, ${Math.round(toolTipBead[0][1])}, ${Math.round(toolTipBead[0][2])})`;
var rectWidth = 325,
Expand All @@ -114,6 +114,7 @@ var imageObj = undefined; //the stitched map image
type = 'Crushed Bead';
rgb = 'N/A';
ctx.fillStyle = $('#crushedBeadOutline').val();
radius = 'N/A';
} else if (toolTipBead[1] === 'waterBead') {
type = 'Water Bead';
ctx.fillStyle = $('#waterBeadOutline').val();
Expand Down
45 changes: 45 additions & 0 deletions lib/color_labeler.py
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]]
54 changes: 26 additions & 28 deletions lib/counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from enum import Enum
from os import listdir, path
from . import util
from . import color_labeler

"""
Description: an enum class to handle the HoughCircle configuration values that are used in cv2.HoughCircles().
Expand Down Expand Up @@ -76,7 +77,6 @@ def getColorBeads(self, houghConfig, detectionParams):
circles = cv2.HoughCircles(blur,cv2.HOUGH_GRADIENT,dp=houghConfig["dp"],minDist=houghConfig["minDist"],
param1=houghConfig["param1"],param2=houghConfig["param2"],minRadius=houghConfig["minRadius"],maxRadius=houghConfig["maxRadius"])


circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# i[0] is x coordinate, i[1] is y coordinate, i[2] is radius
Expand Down Expand Up @@ -134,32 +134,37 @@ def isWater(self, RGB):

def getCrushedBeads(self, image, circles):
temp_img = self.grayScaleMap.copy()

for i in circles[0,:]:
# fills in the circle
cv2.circle(temp_img, (i[0],i[1]) ,i[2], (255,255,255), -1)
#fills the outer edges of the circle
cv2.circle(temp_img,(i[0], i[1]), i[2], (255,255,255), 17)


blur = cv2.GaussianBlur(temp_img, (19, 19), 0)
lab = cv2.cvtColor(self.colorMap, cv2.COLOR_BGR2LAB)
thresh = cv2.threshold(blur, 225, 255, cv2.THRESH_BINARY_INV)[1]
img_output, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
# calculate moments for each contour
M = cv2.moments(c)
cl = color_labeler.ColorLabeler()

# calculate x,y coordinate of center
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
cX, cY = 0, 0
for c in contours:
color = cl.label(lab, c)

x = self.getBrightestColor([cX, cY, 10])
print(x)
if color not in cl.colorsToIgnore:
cv2.drawContours(image, [c], -1, (0, 255, 0), 2)

cv2.circle(image, (cX, cY), 5, (0, 0, 255), -1)
self.crushedBeads.append([[0, 0, 0], 'crushedBead', [cX, cY, 35]])
# compute the center of the contour
M = cv2.moments(c)
if M['m00'] > 0:
cX = int((M["m10"] / M["m00"]))
cY = int((M["m01"] / M["m00"]))
self.crushedBeads.append([[0, 0, 0], 'crushedBead', [cX, cY, 35]])
else:
cX = 0
cY = 0
cv2.circle(image, (cX, cY), 2, (0, 255, 0), 3)

"""
Description: a function that takes an array representing a circle's[x-coord of center, y-coord of center, radius]
Expand Down Expand Up @@ -232,7 +237,7 @@ def getAverageColor(self, circleInfo):
isWater = self.isWater(average)
type = 'waterBead' if isWater else 'bead'
return [[average[0],average[1],average[2]], type, [circleInfo[0],circleInfo[1],circleInfo[2]]] #[[R,G,B], isWater, [x,y,radius]]

def getMiddleColor(self, circleInfo):
img = self.colorMap
imgY = img.shape[0]
Expand All @@ -253,7 +258,7 @@ def getMiddleColor(self, circleInfo):
reds.append(bgrValue[2])
greens.append(bgrValue[1])
blues.append(bgrValue[0])

average = (round(np.mean(reds), 2), round(np.mean(greens), 2), round(np.mean(blues), 2))
isWater = self.isWater(average)
type = 'waterBead' if isWater else 'bead'
Expand Down Expand Up @@ -282,8 +287,8 @@ def getRadiusAverageColor(self, circleInfo):
isWater = self.isWater(average)
type = 'waterBead' if isWater else 'bead'
return [[average[0],average[1],average[2]], type, [circleInfo[0],circleInfo[1],circleInfo[2]]] #[[R,G,B], isWater, [x,y,radius]]


def getFourQuadrantColor(self, circleInfo):
img = self.colorMap
imgY = img.shape[0]
Expand All @@ -305,18 +310,11 @@ def getFourQuadrantColor(self, circleInfo):
reds.append(bgrValue[2])
greens.append(bgrValue[1])
blues.append(bgrValue[0])

average = (round(np.mean(reds), 2), round(np.mean(greens), 2), round(np.mean(blues), 2))
isWater = self.isWater(average)
type = 'waterBead' if isWater else 'bead'
return [[average[0],average[1],average[2]], type, [circleInfo[0],circleInfo[1],circleInfo[2]]] #[[R,G,B], isWater, [x,y,radius]]








"""
Description: a function that takes a bead's radius and x and y coordinates of the center and returns coordinates of every point in
Expand All @@ -334,10 +332,10 @@ def getPointsInCircle(self, radius, centerX, centerY):


"""
Description:
Description:
@param colorFormat: a string that is either 'rgb', 'hsv', 'cmyk', or 'grayscale'
@return void, writes file directly from class attributes
"""
"""
def makeBeadsCSV(self, colorFormat):
newPath = self.imagePath
endIndex = newPath.rfind("/")
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a500faa

Please sign in to comment.