Example #1
0
    def __init__(self, history=200, nMixtures=5, backgroundRatio=0.7,
                 noiseSigma=15, learningRate=0.7):

        try:
            import cv2
        except ImportError:
            raise ImportError(
                "Cannot load OpenCV library which is required by SimpleCV")
        if not hasattr(cv2, 'BackgroundSubtractorMOG'):
            raise ImportError("A newer version of OpenCV is needed")

        self._error = False
        self._ready = False
        self._diff_img = None
        self._color_img = None
        self._blob_maker = BlobMaker()

        self.history = history
        self.nMixtures = nMixtures
        self.backgroundRatio = backgroundRatio
        self.noiseSigma = noiseSigma
        self.learningRate = learningRate

        self._bsmog = cv2.BackgroundSubtractorMOG(history, nMixtures,
                                                  backgroundRatio, noiseSigma)
    def extract(self, img):
        """
        This method takes in a image and returns some basic morphology
        characteristics about the largest blob in the image. The
        if a _color image is provided the threshold operation is applied.
        """
        ret = None
        if self._threshold_operation is not None:
            bitwise = self._threshold_operation(img)
        else:
            bitwise = img.binarize()

        if self._blob_maker is None:
            self._blob_maker = BlobMaker()

        fs = self._blob_maker.extract_from_binary(bitwise, img)
        if fs is not None and len(fs) > 0:
            fs = fs.sort_area()
            ret = []
            ret.append(fs[0].area / fs[0].perimeter)
            ret.append(fs[0].aspect_ratio)
            ret.append(fs[0].hu[0])
            ret.append(fs[0].hu[1])
            ret.append(fs[0].hu[2])
            ret.append(fs[0].hu[3])
            ret.append(fs[0].hu[4])
            ret.append(fs[0].hu[5])
            ret.append(fs[0].hu[6])
        return ret
 def __init__(self, thresh_operation=None):
     """
     The threshold operation is a function of the form
     binaryimg = threshold(img)
     the simplest example would be:
     def binarize_wrap(img):
     """
     self._nbins = 9
     self._blob_maker = BlobMaker()
     self.threshold_operation = thresh_operation
Example #4
0
 def __setstate__(self, state):
     self.__dict__ = state
     self._blob_maker = BlobMaker()
Example #5
0
class MOGSegmentation(SegmentationBase):
    """
    Background subtraction using mixture of gaussians.
    For each pixel store a set of gaussian distributions and try to fit new
    pixels into those distributions. One of the distributions will represent
    the background.

    history - length of the pixel history to be stored
    nMixtures - number of gaussian distributions to be stored per pixel
    backgroundRatio - chance of a pixel being included into the background model
    noiseSigma - noise amount
    learning rate - higher learning rate means the system will adapt faster to
    new backgrounds
    """

    _error = False
    _diff_img = None
    _color_img = None
    _model_img = None
    _ready = False

    # OpenCV default parameters
    history = 200
    nMixtures = 5
    backgroundRatio = 0.7
    noiseSigma = 15
    learningRate = 0.7
    bsMOG = None

    def __init__(self, history=200, nMixtures=5, backgroundRatio=0.7,
                 noiseSigma=15, learningRate=0.7):

        try:
            import cv2
        except ImportError:
            raise ImportError(
                "Cannot load OpenCV library which is required by SimpleCV")
        if not hasattr(cv2, 'BackgroundSubtractorMOG'):
            raise ImportError("A newer version of OpenCV is needed")

        self._error = False
        self._ready = False
        self._diff_img = None
        self._color_img = None
        self._blob_maker = BlobMaker()

        self.history = history
        self.nMixtures = nMixtures
        self.backgroundRatio = backgroundRatio
        self.noiseSigma = noiseSigma
        self.learningRate = learningRate

        self._bsmog = cv2.BackgroundSubtractorMOG(history, nMixtures,
                                                  backgroundRatio, noiseSigma)

    def add_image(self, img):
        """
        Add a single image to the segmentation algorithm
        """
        if img is None:
            return

        self._color_img = img
        self._diff_img = Image(
            self._bsmog.apply(img.cvnarray, None, self.learningRate),
            cv2image=True
        )
        self._ready = True
        return

    def is_ready(self):
        """
        Returns true if the camera has a segmented image ready.
        """
        return self._ready

    def is_error(self):
        """
        Returns true if the segmentation system has detected an error.
        Eventually we'll construct a syntax of errors so this becomes
        more expressive
        """
        return self._error  # need to make a generic error checker

    def reset_error(self):
        """
        Clear the previous error.
        """
        self._error = False
        return

    def reset(self):
        """
        Perform a reset of the segmentation systems underlying data.
        """
        self._model_img = None
        self._diff_img = None

    @property
    def raw_image(self):
        """
        Return the segmented image with white representing the foreground
        and black the background.
        """
        return self._diff_img

    @property
    def segmented_image(self, white_fg=True):
        """
        Return the segmented image with white representing the foreground
        and black the background.
        """
        return self._diff_img

    @property
    def segmented_blobs(self):
        """
        return the segmented blobs from the fg/bg image
        """
        ret = []
        if self._color_img is not None and self._diff_img is not None:
            ret = self._blob_maker.extract_from_binary(self._diff_img,
                                                       self._color_img)
        return ret

    def __getstate__(self):
        state = self.__dict__.copy()
        self._blob_maker = None
        self._diff_img = None
        del state['_blob_maker']
        del state['_diff_img']
        return state

    def __setstate__(self, state):
        self.__dict__ = state
        self._blob_maker = BlobMaker()
class MorphologyFeatureExtractor(FeatureExtractorBase):
    """
    This features extractor collects some basic morphology information about a
    given image. It is assumed that the object to be recognized is the largest
    object in the image. The user must provide a segmented white on black blob
    image. This operation then straightens the image and collects the data.
    """
    _nbins = 9
    _blob_maker = None
    threshold_operation = None

    def __init__(self, thresh_operation=None):
        """
        The threshold operation is a function of the form
        binaryimg = threshold(img)
        the simplest example would be:
        def binarize_wrap(img):
        """
        self._nbins = 9
        self._blob_maker = BlobMaker()
        self.threshold_operation = thresh_operation

    def setThresholdOperation(self, thresh_op):
        """
        The threshold operation is a function of the form
        binaryimg = threshold(img)

        Example:
        >>> def binarize_wrap(img):
        >>>    return img.binarize()
        """
        self._threshold_operation = thresh_op

    def extract(self, img):
        """
        This method takes in a image and returns some basic morphology
        characteristics about the largest blob in the image. The
        if a _color image is provided the threshold operation is applied.
        """
        ret = None
        if self._threshold_operation is not None:
            bitwise = self._threshold_operation(img)
        else:
            bitwise = img.binarize()

        if self._blob_maker is None:
            self._blob_maker = BlobMaker()

        fs = self._blob_maker.extract_from_binary(bitwise, img)
        if fs is not None and len(fs) > 0:
            fs = fs.sort_area()
            ret = []
            ret.append(fs[0].area / fs[0].perimeter)
            ret.append(fs[0].aspect_ratio)
            ret.append(fs[0].hu[0])
            ret.append(fs[0].hu[1])
            ret.append(fs[0].hu[2])
            ret.append(fs[0].hu[3])
            ret.append(fs[0].hu[4])
            ret.append(fs[0].hu[5])
            ret.append(fs[0].hu[6])
        return ret

    def get_field_names(self):
        """
        This method gives the names of each field in the features vector in the
        order in which they are returned. For example, 'xpos' or 'width'
        """
        ret = []
        ret.append('area over perim')
        ret.append('AR')
        ret.append('Hu0')
        ret.append('Hu1')
        ret.append('Hu2')
        ret.append('Hu3')
        ret.append('Hu4')
        ret.append('Hu5')
        ret.append('Hu6')
        return ret

    def get_num_fields(self):
        """
        This method returns the total number of fields in the features vector.
        """
        return self._nbins

    def __getstate__(self):
        att = self.__dict__.copy()
        self._blob_maker = None
        del att['_blob_maker']
        return att

    def __setstate__(self, state):
        self.__dict__ = state
        self._blob_maker = BlobMaker()