def refineLabels(out, labels, corners, howmany=3): # pick up labels on corners containcorners = [] for corner in corners: x = int(corner[0][0]) y = int(corner[0][1]) hsv = rgb2hsv(out[y, x]) # accept only if candidate color(yellow or blue) if checkBlue(hsv) or checkYellow(hsv): containcorners.append(labels[y][x]) # make table(corners,label) match = [] for label in np.unique(containcorners): match.append([(containcorners == label).sum(), label]) match = sorted(match, reverse=True) match = np.array(match) # refine howmany howmany = howmany if howmany < match.shape[0] else match.shape[0] # select labels with enough corners pickup = [] for i in range(howmany): pickup.append(match[i][1]) # mark bg with 0 mark = 0 labels_ = np.zeros(labels.shape) # for label in np.unique(pickup): mark += 1 labels_[labels == label] = mark # make saturation return labels_
def estimateColr(img, rect, debug=False): [x, y, w, h] = rect roi = img[y:y+h, x:x+w] rgb = cv2.cvtColor(roi,cv2.COLOR_BGR2RGB) totalcolr = [0,0,0] for j in range(h): for i in range(w): totalcolr += rgb[j,i] pixelcount = w * h avergecolr = totalcolr / pixelcount if debug: print("estimatecolr,avrgcolr:",avergecolr) hsv = rgb2hsv(avergecolr) return checkBlue(hsv) or checkYellow(hsv)
def detectcolr(self): c_x = self.width / 2 c_y = self.height / 2 roi = self.bgr[c_y - 15:c_y + 15, c_x - 30:c_x + 30, :] rgb = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB) h, w, c = roi.shape totalcolr = np.zeros(3) for j in range(h): for i in range(w): totalcolr += rgb[j, i] avergecolr = totalcolr / (w * h) hsv = rgb2hsv(avergecolr) if checkBlue(hsv): self.colr = 1 self.returnImg = 255 - self.laplacian elif checkYellow(hsv): self.colr = 2 self.returnImg = self.laplacian.copy() else: self.colr = 0