Пример #1
0
    def __getBackGround3(self, threshold, color):
        """ Method number 4 to detect the background pixels. Detect the background 
      comparing each pixel of local image with the given color using the threshold. 
      (TOO SLOW)
      @param threshold: Edge to detect the color background
      @type threshold: int
      @param color: Color used to detect background
      @type color: cvScalar
      @return: As first parameter the original image changing the different pixel to black pixel.
      As Second parameter a IplImage in Gray Scale where the differences are white pixels. After 
      this can be used as mask.
      """
        def intervale(id1, id2):
            if math.fabs(id1 - id2) < threshold:
                return True
            return False

        differenceI = cv.CloneMat(self.__image)
        size = cv.GetSize(self.__image)
        grayI = cv.CreateImage(cv.GetSize(differenceI), cv.IPL_DEPTH_8U, 1)
        cv.SetZero(grayI)
        for i in range(0, size[0]):
            for j in range(0, size[1]):
                pixel = cv.Get2D(self.__image, j, i)
                if intervale(color[0], pixel[0]) and intervale(
                        color[1], pixel[1]) and intervale(color[2], pixel[2]):
                    cv.Set2D(differenceI, j, i, cv.Scalar(0, 0, 0))
                    cv.Set2D(grayI, j, i, cv.Scalar(255, 255, 255))

        cv.Not(grayI, grayI)
        return differenceI, grayI
Пример #2
0
    def __getBackGround2(self, threshold, preImage):
        """ Method number 3 to detect the background pixels. The background is detected of 
      the local Image using the given preImage. For this it is calculed the difference 
      between them.
      @type threshold: int
      @param preImage: Image used to compare the differences
      @type preImage: IplImage
      @return: As first parameter the original image changing the different pixel to black pixel.
      As Second parameter a IplImage in Gray Scale where the differences are white pixels. After 
      this can be used as mask.
      """

        differenceI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U,
                                     3)
        grayI = cv.CreateImage(cv.GetSize(differenceI), cv.IPL_DEPTH_8U, 1)

        cv.AbsDiff(self.__image, preImage, differenceI)
        cv.CvtColor(differenceI, grayI, cv.CV_BGR2GRAY)

        cv.Threshold(grayI, grayI, threshold, 255, cv.CV_THRESH_BINARY)
        cv.Smooth(grayI, grayI, cv.CV_MEDIAN, 5)
        cv.Smooth(grayI, grayI, cv.CV_GAUSSIAN, 5)

        cv.SetZero(differenceI)
        cv.Add(self.__image, differenceI, differenceI, grayI)
        return differenceI, grayI
Пример #3
0
    def __getBackGround(self, threshold, color):
        """ Method number 1 to detect the background pixels. The background is detected of 
      the local Image using given color. For this it is calculed
      the difference between local Image and another image set up the given color only.
      @param threshold: Edge to detect the color background
      @type threshold: int
      @param color: Color used to detect background
      @type color: cvScalar
      @return: As first parameter the original image changing the different pixel to black pixel.
      As Second parameter a IplImage in Gray Scale where the differences are white pixels. After 
      this can be used as mask.
      
      """
        differenceI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U,
                                     3)
        grayI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 1)
        backgroundI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U,
                                     3)
        cv.Set(backgroundI, color)

        cv.AbsDiff(self.__image, backgroundI, differenceI)
        cv.CvtColor(differenceI, grayI, cv.CV_BGR2GRAY)
        cv.Smooth(grayI, grayI, cv.CV_BLUR, 5)
        cv.Smooth(grayI, grayI, cv.CV_MEDIAN, 5)

        cv.Threshold(grayI, grayI, threshold, 255, cv.CV_THRESH_BINARY)
        cv.SetZero(differenceI)
        cv.And(self.__image, self.__image, differenceI, grayI)
        return differenceI, grayI
Пример #4
0
    def trackClean(self, objectColor, threshold, thickness, lastPoint2,
                   imgScribble):
        """ Applies the technique of tracking to local image with the method of to clear. 
      For this it is used the color of the followed object to show the lowed image.(ONLY WEBCAM)
      @param objectColor: Color of the used object to detect.
      @type objectColor: cvScalar
      @param threshold: Threshould detected color of the object.
      @type threshold: int [0,255]
      @param thickness: Thickness used to draw.
      @type thickness: int [0,255]
      @param lastPoint2: Auxiliar Object
      @param imgScribble: Image where is stored the tracking way.
      @type imgScribble: IplImage
      """

        lastPoint = lastPoint2[0]
        imgThresh = self.__getThresholdImage1(threshold, objectColor)
        moments = cv.Moments(imgThresh, 1)
        moment10 = cv.GetSpatialMoment(moments, 1, 0)
        moment01 = cv.GetSpatialMoment(moments, 0, 1)
        area = cv.GetCentralMoment(moments, 0, 0)
        #print area

        if area == 0:
            currentPoint = None
        else:
            currentPoint = [moment10 / area, moment01 / area]

        if lastPoint != None and currentPoint != None:
            cv.Line(imgScribble, (int(currentPoint[0]), int(currentPoint[1])),
                    (int(lastPoint[0]), int(lastPoint[1])),
                    cv.Scalar(250, 250, 250), thickness, cv.CV_AA)

        mask = cv.CreateImage(cv.GetSize(self.__image), 8, 1)
        cv.SetZero(mask)
        cv.CvtColor(imgScribble, mask, cv.CV_BGR2GRAY)
        blackI = cv.CreateImage(cv.GetSize(self.__image), 8, 3)
        cv.SetZero(blackI)
        cv.And(self.__image, self.__image, blackI, mask)
        self.__image = blackI
        lastPoint2[0] = currentPoint
Пример #5
0
    def __getBackGround1(self, threshold, color):
        """ Method number 2 to detect the background pixels. The background is detected of 
      the local Image using given color. For this it is calculed
      the difference between local Image and another image set up the given color only in 
      each one channel of color.
      @param threshold: Edge to detect the color background
      @type threshold: int
      @param color: Color used to detect background
      @type color: cvScalar
      @return: As first parameter the original image changing the different pixel to black pixel.
      As Second parameter a IplImage in Gray Scale where the differences are white pixels. After 
      this can be used as mask.
      """

        backgroundI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U,
                                     3)
        cv.Set(backgroundI, color)

        i0, i1, i2, i3, b0, b1, b2, b3, d0, d1, d2, d3 = tuple([
            cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 1)
            for i in range(12)
        ])

        differenceI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U,
                                     3)
        grayI = cv.CreateImage(cv.GetSize(differenceI), cv.IPL_DEPTH_8U, 1)

        cv.Split(self.__image, i0, i1, i2, None)
        cv.Split(backgroundI, b0, b1, b2, None)

        cv.AbsDiff(i0, b0, d0)
        cv.AbsDiff(i1, b1, d1)
        cv.AbsDiff(i2, b2, d2)

        cv.Threshold(d0, d0, threshold, 255, cv.CV_THRESH_BINARY)
        cv.Threshold(d1, d1, threshold, 255, cv.CV_THRESH_BINARY)
        cv.Threshold(d2, d2, threshold, 255, cv.CV_THRESH_BINARY)

        cv.Merge(d0, d1, d2, None, differenceI)
        cv.CvtColor(differenceI, grayI, cv.CV_BGR2GRAY)

        cv.Smooth(grayI, grayI, cv.CV_BLUR, 5)
        cv.Smooth(grayI, grayI, cv.CV_MEDIAN, 5)
        cv.Threshold(grayI, grayI, threshold, 255, cv.CV_THRESH_BINARY)

        cv.SetZero(differenceI)

        cv.Add(self.__image, differenceI, differenceI, grayI)

        return differenceI, grayI
Пример #6
0
    def trackColor(self, objectColor, threshold, penColor, thickness,
                   lastPoint2, imgScribble, typeS, typeDraw, disable):
        """ Applies the technique of tracking to local image. For this it is used the color
      of the followed object to draw the track in the image. It is possible apply two differents
      techniques to follow the object and also thickness is chosen. (ONLY WEBCAM)
      @param objectColor: Color of the used object to detect.
      @type objectColor: cvScalar
      @param threshold: Threshould detected color of the object.
      @type threshold: int [0,255]
      @param penColor: Drawing used color
      @type penColor: cvScalar
      @param thickness: Thickness used to draw.
      @type thickness: int [0,255]
      @param lastPoint2: Auxiliar Object
      @param imgScribble: Image where is stored the tracking way.
      @type imgScribble: IplImage
      @param typeS: Type of method used to detect the object
      @type typeS: int[0,1]
      @param typeDraw: If 1 the transparence is disabled.
      @param disable: Parameter of pause option
      """
        lastPoint = lastPoint2[0]
        #penColor = objectColor
        imgThresh = None
        if typeS == 1:
            lowerColor = cv.Scalar(objectColor[0] - threshold,
                                   objectColor[1] - threshold,
                                   objectColor[2] - threshold)
            upperColor = cv.Scalar(objectColor[0] + threshold,
                                   objectColor[1] + threshold,
                                   objectColor[2] + threshold)
            imgThresh = self.__getThresholdImage(lowerColor, upperColor)
        elif typeS == 0:
            imgThresh = self.__getThresholdImage1(threshold, objectColor)

        moments = cv.Moments(imgThresh, 1)
        moment10 = cv.GetSpatialMoment(moments, 1, 0)
        moment01 = cv.GetSpatialMoment(moments, 0, 1)
        area = cv.GetCentralMoment(moments, 0, 0)
        #print area

        if area == 0 or disable == True:
            currentPoint = None
        else:
            currentPoint = [moment10 / area, moment01 / area]

        if lastPoint != None and currentPoint != None:
            cv.Line(imgScribble, (int(currentPoint[0]), int(currentPoint[1])),
                    (int(lastPoint[0]), int(lastPoint[1])), penColor,
                    thickness, cv.CV_AA)

        mask = cv.CreateImage(cv.GetSize(self.__image), 8, 1)
        cv.CvtColor(imgScribble, mask, cv.CV_BGR2GRAY)
        cv.Threshold(mask, mask, 20, 255, cv.CV_THRESH_BINARY)
        cv.Not(mask, mask)
        blackI = cv.CreateImage(cv.GetSize(self.__image), 8, 3)
        cv.SetZero(blackI)
        cv.And(self.__image, self.__image, blackI, mask)

        if typeDraw == 0:  # Totally Transparent
            cv.Add(self.__image, imgScribble, self.__image)
        else:  # Totally Opaque
            cv.Add(blackI, imgScribble, self.__image)
        #self.__image = blackI

        lastPoint2[0] = currentPoint