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
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
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
def __getThresholdImage1(self, threshold, color): """ Tecnique number 2 to detect the current possition of a specific color in the local image. @param threshold: Allowed threshold to search. @param color: Value of the color to search. @return The mask where is found the intervale of chosen colours. """ differenceI = cv.CreateImage(cv.GetSize(self.__image), cv.IPL_DEPTH_8U, 3) grayI = cv.CreateImage(cv.GetSize(differenceI), 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.Not(grayI, grayI) return grayI
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