Пример #1
0
    def _detectAndEstimateDistance(self):

        self._grayImage = cv2.cvtColor(self._image, cv2.COLOR_BGR2GRAY,
                                       self._grayImage)
        blobs = self._detector.detect(self._grayImage)
        blobsForColors = {}

        for blob in blobs:

            centerXAsInt, centerYAsInt = \
                    (int(n) for n in blob.pt)
            radiusAsInt = int(blob.size)

            minX = max(0, centerXAsInt - radiusAsInt)
            maxX = min(self._imageWidth, centerXAsInt + radiusAsInt)
            minY = max(0, centerYAsInt - radiusAsInt)
            maxY = min(self._imageHeight, centerYAsInt + radiusAsInt)

            region = self._image[minY:maxY, minX:maxX]

            # Get the region's dimensions, which may
            # differ from the blob's diameter if the blob
            # extends past the edge of the image.
            h, w = region.shape[:2]

            meanColor = region.reshape(w * h, 3).mean(0)
            meanHue = ColorUtils.hueFromBGR(meanColor)
            meanSaturation = ColorUtils.saturationFromBGR(meanColor)

            if meanHue < 22.5 or meanHue > 337.5:
                color = COLOR_Red
            elif meanHue < 67.5:
                if meanSaturation < 25.0:
                    color = COLOR_YellowWhite
                else:
                    color = COLOR_AmberYellow
            elif meanHue < 172.5:
                color = COLOR_Green
            elif meanHue < 277.5:
                if meanSaturation < 25.0:
                    color = COLOR_BlueWhite
                else:
                    color = COLOR_BluePurple
            else:
                color = COLOR_Pink

            if color in blobsForColors:
                blobsForColors[color] += [blob]
            else:
                blobsForColors[color] = [blob]

        self._processBlobsForColors(blobsForColors)
        self._enableOrDisableCalibrationButton()