def Magnitude(self, dx, dy, Mask=None, precise=True, method="cv"):
        '''Calculates the magnitude of the gradient using precise and fast approach'''

        dxconv = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels)
        dyconv = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels)
        dxdest = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels)
        dydest = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels)
        magdest = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels)
        magnitude = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F,
                                   dx.channels)
        magnitudetemp = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F,
                                       dx.channels)
        zero = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_32F, dx.channels)

        cv.Convert(dx, dxconv)
        cv.Convert(dy, dyconv)

        if precise:
            cv.Pow(dxconv, dxdest, 2)
            cv.Pow(dyconv, dydest, 2)
            cv.Add(dxdest, dydest, magdest)
            cv.Pow(magdest, magnitude, 1. / 2)
        else:
            #Add the |dx| + |dy|
            return None

        if method == "slow":
            size = cv.GetSize(magnitude)

            for x in range(size[0]):
                for y in range(size[1]):
                    if Mask == None:
                        pass
                    elif Mask[y, x] > 0:
                        pass
                    else:
                        magnitude[y, x] = 0

            final = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_8U,
                                   dx.channels)
            cv.ConvertScaleAbs(magnitude, final)
        else:
            cv.Add(zero, magnitude, magnitudetemp, Mask)
            final = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_8U,
                                   dx.channels)
            cv.ConvertScaleAbs(magnitudetemp, final)

        if self.visualize:
            magnitude2 = cv.CreateImage(cv.GetSize(dy), cv.IPL_DEPTH_8U, 1)
            cv.EqualizeHist(final, magnitude2)
            while True:
                cv.NamedWindow("Magnitude")
                cv.ShowImage("Magnitude", magnitude2)
                c = cv.WaitKey(5)
                if c > 0:

                    break
        cv.DestroyAllWindows()

        return final
Exemple #2
0
 def _mixImageAlphaMask(self, wipeSettings, level, image1, image2, image2mask, mixMat):
     if(level < 0.99):
         wipeMode, wipePostMix, wipeConfig = wipeSettings
         if((wipeMode == WipeMode.Fade) or (wipeMode == WipeMode.Default)):
             valueCalc = int(256 * (1.0 - level))
             rgbColor = cv.CV_RGB(valueCalc, valueCalc, valueCalc)
             whiteColor = cv.CV_RGB(255, 255, 255)
             cv.Set(mixMat, whiteColor)
             cv.Set(mixMat, rgbColor, image2mask)
             cv.Mul(image1, mixMat, image1, 0.004)
             valueCalc = int(256 * level)
             rgbColor = cv.CV_RGB(valueCalc, valueCalc, valueCalc)
             cv.Zero(mixMat)
             cv.Set(mixMat, rgbColor, image2mask)
             cv.Mul(image2, mixMat, image2, 0.004)
             cv.Add(image1, image2, image1)
             return image1
         else:
             if(wipePostMix == False):
                 image2, image2mask = self._wipeImage(wipeMode, wipeConfig, level, image2, image2mask, mixMat, False)
                 cv.Copy(image2, image1, image2mask)
                 return image1
             else:
                 cv.Copy(image1, mixMat)
                 cv.Copy(image2, mixMat, image2mask)
                 return self._wipeMix(wipeMode, wipeConfig, level, image1, mixMat, image2)
     cv.Copy(image2, image1, image2mask)
     return image1
Exemple #3
0
    def locateMarker(self, frame):
        self.frameReal = cv.CloneImage(frame)
        self.frameImag = cv.CloneImage(frame)
        self.frameRealThirdHarmonics = cv.CloneImage(frame)
        self.frameImagThirdHarmonics = cv.CloneImage(frame)

        # Calculate convolution and determine response strength.
        cv.Filter2D(self.frameReal, self.frameReal,
                    self.matReal)  # src, dst, kernel
        cv.Filter2D(self.frameImag, self.frameImag,
                    self.matImag)  # src, dst, kernel

        cv.Mul(self.frameReal, self.frameReal,
               self.frameRealSq)  # src, src, dst
        cv.Mul(self.frameImag, self.frameImag,
               self.frameImagSq)  # src, src, dst
        cv.Add(self.frameRealSq, self.frameImagSq, self.frameSumSq)

        # Calculate convolution of third harmonics for quality estimation.
        cv.Filter2D(self.frameRealThirdHarmonics, self.frameRealThirdHarmonics,
                    self.matRealThirdHarmonics)
        cv.Filter2D(self.frameImagThirdHarmonics, self.frameImagThirdHarmonics,
                    self.matImagThirdHarmonics)

        min_val, max_val, min_loc, max_loc = cv.MinMaxLoc(self.frameSumSq)
        self.lastMarkerLocation = max_loc
        (xm, ym) = max_loc
        self.determineMarkerOrientation(frame)
        #	self.determineMarkerQuality_naive(frame)
        self.determineMarkerQuality_Mathias(frame)
        #        self.determineMarkerQuality()
        return max_loc
    def draw(self, img, pixmapper, bounds):
        '''draw the icon on the image'''

        if self.hidden:
            return

        if self.trail is not None:
            self.trail.draw(img, pixmapper, bounds)

        icon = self.img()
        (px, py) = pixmapper(self.latlon)

        # find top left
        px -= icon.width / 2
        py -= icon.height / 2
        w = icon.width
        h = icon.height

        (px, py, sx, sy, w, h) = self.clip(px, py, w, h, img)

        cv.SetImageROI(icon, (sx, sy, w, h))
        cv.SetImageROI(img, (px, py, w, h))
        cv.Add(icon, img, img)
        cv.ResetImageROI(img)
        cv.ResetImageROI(icon)

        # remember where we placed it for clicked()
        self.posx = px + w / 2
        self.posy = py + h / 2
Exemple #5
0
def getthresholdedimg(im):

	# this function take RGB image.Then convert it into HSV for easy colour detection 
	# and threshold it with yellow and blue part as white and all other regions as black.Then return that image
	
	global imghsv
	imghsv = cv.CreateImage(cv.GetSize(im),8,3)
	
	# Convert image from RGB to HSV
	cv.CvtColor(im,imghsv,cv.CV_BGR2HSV)
					
	# creates images for blue 
	imgblue   = cv.CreateImage(cv.GetSize(im),8,1)
	
	# creates blank image to which color images are added
	imgthreshold = cv.CreateImage(cv.GetSize(im),8,1)
	
	# determine HSV color thresholds for yellow, blue, and green
	# cv.InRange(src, lowerbound, upperbound, dst)
	# for imgblue, lowerbound is 95, and upperbound is 115
	cv.InRangeS(imghsv, cv.Scalar(55,100,100), cv.Scalar(155,255,255), imgblue)  #55/155 original, 105 seems to be lower threshold needed to eliminate flag detection
	
	# add color thresholds to blank 'threshold' image
	cv.Add(imgthreshold, imgblue,   imgthreshold)

	return imgthreshold
Exemple #6
0
 def _mixImageAdd(self, wipeSettings, level, image1, image2, mixMat):
     if(level < 0.99):
         wipeMode, wipePostMix, wipeConfig = wipeSettings
         if((wipeMode == WipeMode.Fade) or (wipeMode == WipeMode.Default)):
             cv.ConvertScaleAbs(image2, image2, level, 0.0)
             cv.Add(image1, image2, mixMat)
             return mixMat
         else:
             if(wipePostMix == False):
                 image2, _ = self._wipeImage(wipeMode, wipeConfig, level, image2, None, mixMat, False)
                 cv.Add(image1, image2, mixMat)
                 return mixMat
             else:
                 cv.Add(image1, image2, mixMat)
                 return self._wipeMix(wipeMode, wipeConfig, level, image1, mixMat, image2)
     cv.Add(image1, image2, mixMat)
     return mixMat
Exemple #7
0
def colorFilterCombine(img, color1, color2, s):
    imgFiltered = cv.CreateImage(cv.GetSize(img), 8, 1)
    imgColor1 = cv.CreateImage(cv.GetSize(img), 8, 1)
    imgColor2 = cv.CreateImage(cv.GetSize(img), 8, 1)
    imgCalibrate = cv.CreateImage(cv.GetSize(img), 8, 1)

    imgColor1 = colorFilter(img, color1, s)
    imgColor2 = colorFilter(img, color2, s)

    cv.Add(imgColor1, imgColor2, imgFiltered)

    if s != []:
        for value in s:
            imgCalibrate = colorFilter(img, "calibrate", value)
            cv.Add(imgFiltered, imgCalibrate, imgFiltered)
            print value[0], value[1], value[2], "added"

    return imgFiltered
Exemple #8
0
def edge_image(image):
    swap_image = gray_swap(image)

    image1 = cv.CreateImage((image.width, image.height), image.depth,
                            image.nChannels)
    image2 = cv.CreateImage((image.width, image.height), image.depth,
                            image.nChannels)
    image_all = cv.CreateImage((image.width, image.height), image.depth,
                               image.nChannels)

    image_x = sobel_image(image, 1, 0)
    image_y = sobel_image(image, 0, 1)
    cv.Add(image_x, image_y, image1)

    image_x = sobel_image(swap_image, 1, 0)
    image_y = sobel_image(swap_image, 0, 1)
    cv.Add(image_x, image_y, image2)

    cv.Add(image1, image2, image_all)

    return image_all
def getthresholdedimg(im):

	'''this function take RGB image.Then convert it into HSV for easy colour detection and threshold it with yellow and blue part as white and all other regions as black.Then return that image'''
	global imghsv
	imghsv = cv.CreateImage(cv.GetSize(im),8,3)
	cv.CvtColor(im,imghsv,cv.CV_BGR2HSV)				# Convert image from RGB to HSV

	# A little change here. Creates images for blue and yellow (or whatever color you like).
	imgyellow = cv.CreateImage(cv.GetSize(im),8,1)
	imgblue   = cv.CreateImage(cv.GetSize(im),8,1)
	imggreen  = cv.CreateImage(cv.GetSize(im),8,1) # glen added this

	imgthreshold=cv.CreateImage(cv.GetSize(im),8,1)

	cv.InRangeS(imghsv, cv.Scalar(20,100,100),  cv.Scalar(30,255,255),  imgyellow)	# Select a range of yellow color in HSV, where 20 is low H and 30 is high H
	cv.InRangeS(imghsv, cv.Scalar(100,100,100), cv.Scalar(120,255,255), imgblue  )	# Select a range of blue color 
	cv.InRangeS(imghsv, cv.Scalar(150,100,100), cv.Scalar(170,255,255), imggreen ) 	# glen added this; select a range of green color
	cv.Add(imgthreshold, imgyellow, imgthreshold)
	cv.Add(imgthreshold, imgblue,   imgthreshold)
	cv.Add(imgthreshold, imggreen,  imgthreshold)
	
	return imgthreshold
Exemple #10
0
def blend_views(bldIm, frame, mask, frec, max_rec):
    maskMat = cv.fromarray(mask)

    dispX = int(np.round(frec.left - max_rec.left))
    dispY = int(np.round(frec.top - max_rec.top))

    bldROI = cv.GetImage(bldIm)

    cv.SetImageROI(bldROI, (dispX, dispY, int(np.round(
        frec.width())), int(np.round(frec.height()))))
    cv.Add(bldROI, cv.fromarray(frame), bldROI, maskMat)
    cv.ResetImageROI(bldROI)
    cv.Copy(bldROI, bldIm)

    return bldIm
Exemple #11
0
def run(self): 
    while True: 
        img = cv.QueryFrame( self.capture ) 

        #blur the source image to reduce color noise 
        cv.Smooth(img, img, cv.CV_BLUR, 3); 

        #convert the image to hsv(Hue, Saturation, Value) so its  
        #easier to determine the color to track(hue) 
        hsv_img = cv.CreateImage(cv.GetSize(img), 8, 3) 
        cv.CvtColor(img, hsv_img, cv.CV_BGR2HSV) 

        #limit all pixels that don't match our criteria, in this case we are  
        #looking for purple but if you want you can adjust the first value in  
        #both turples which is the hue range(120,140).  OpenCV uses 0-180 as  
        #a hue range for the HSV color model 
        thresholded_img =  cv.CreateImage(cv.GetSize(hsv_img), 8, 1) 
        cv.InRangeS(hsv_img, (120, 80, 80), (140, 255, 255), thresholded_img) 

        #determine the objects moments and check that the area is large  
        #enough to be our object 
        moments = cv.Moments(thresholded_img, 0) 
        area = cv.GetCentralMoment(moments, 0, 0) 

        #there can be noise in the video so ignore objects with small areas 
        if(area > 100000): 
            #determine the x and y coordinates of the center of the object 
            #we are tracking by dividing the 1, 0 and 0, 1 moments by the area 
            x = cv.GetSpatialMoment(moments, 1, 0)/area 
            y = cv.GetSpatialMoment(moments, 0, 1)/area 

            #print 'x: ' + str(x) + ' y: ' + str(y) + ' area: ' + str(area) 

            #create an overlay to mark the center of the tracked object 
            overlay = cv.CreateImage(cv.GetSize(img), 8, 3) 

            cv.Circle(overlay, (x, y), 2, (255, 255, 255), 20) 
            cv.Add(img, overlay, img) 
            #add the thresholded image back to the img so we can see what was  
            #left after it was applied 
            cv.Merge(thresholded_img, None, None, None, img) 

        #display the image  
        cv.ShowImage(color_tracker_window, img) 

        if cv.WaitKey(10) == 27: 
            break 
Exemple #12
0
 def _mixImageReplace(self, wipeSettings, level, image1, image2, mixMat):
     if(level < 0.99):
         wipeMode, wipePostMix, wipeConfig = wipeSettings
         if((wipeMode == WipeMode.Fade) or (wipeMode == WipeMode.Default)):
             if(image1 != None):
                 cv.ConvertScaleAbs(image2, image2, level, 0.0)
                 cv.ConvertScaleAbs(image1, image1, 1.0 - level, 0.0)
                 cv.Add(image1, image2, mixMat)
                 return mixMat
             else:
                 cv.ConvertScaleAbs(image2, mixMat, level, 0.0)
                 return mixMat
         else:
             if(wipePostMix == False):
                 image2, mixMask = self._wipeImage(wipeMode, wipeConfig, level, image2, None, mixMat, False)
                 if(image1 == None):
                     return image2
                 cv.Copy(image2, image1, mixMask)
                 return image1
             else:
                 return self._wipeMix(wipeMode, wipeConfig, level, image1, image2, mixMat)
     return image2
Exemple #13
0
		
		# UPDATED 9/22: 20 X AND Y PIXEL MINIMUM TO BE APPENDED TO CENTROID LISTS
		if (55 < cv.Get2D(imghsv,centroidy,centroidx)[0] < 155) and ypix > 20 and xpix > 20: 
			blue.append((centroidx,centroidy))

	# draw colors in windows; exception handling is used to avoid IndexError.	
	# after drawing is over, centroid from previous part is removed from list by pop. 
	# so in next frame, centroids in this frame become initial points of line to draw	
	
	# draw blue box around blue blimp blob
	try:
		cv.Circle(imdraw, blue[1], 5, (255,0,0))
		cv.Line(imdraw, blue[0], blue[1], (255,0,0), 3, 8, 0) 
		print('xpix:'+str(xpix))
		blue.pop(0)
		print("centroid x:" + str(centroidx))
		print("centroid y:" + str(centroidy))
		print("")		
	except IndexError:
		print "no blimp detected"	

	# adds 
	cv.Add(test,imdraw,test)
	
	# display windows previously created
	cv.ShowImage("Real", color_image) 
	
	if cv.WaitKey(33) == 1048603:
		cv.DestroyWindow("Real")
		break
######################################################
    def tangent(self, dx, dy, Mask=None, method="cv"):
        '''This function calculates the gradient orientation of each pixel 
        that is in Mask'''

        tangent = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_8U, 1)
        divSize = cv.GetSize(dx)
        tangent16U = cv.CreateImage(divSize, cv.IPL_DEPTH_32F, 1)
        cv.SetZero(tangent16U)
        if method == "slow":
            for x in range(divSize[0]):
                for y in range(divSize[1]):
                    if Mask == None:

                        tang = math.atan2(dy[y, x], dx[y, x]) * self.constant
                        tangent16U[y, x] = int(tang)

                    elif Mask[y, x] > 0:
                        tang = math.atan2(dy[y, x], dx[y, x]) * self.constant
                        tangent16U[y, x] = int(tang)
                    elif Mask[y, x] == 0:
                        tangent16U[y, x] = 0
        elif method == "cv":
            #Calculated the arctan2 which give -pi to pi range
            #I create numpy arrays then reshape them in to 1 Dimesnion.
            #Next, I calculated arctan2 and change the range to 0-2pi and make it in degrees
            #I reshape back to picture format and return the picture

            #Numpy formatting
            (width, height) = cv.GetSize(dx)
            matdx = cv.CreateMat(height, width, cv.CV_16SC1)
            matdy = cv.CreateMat(height, width, cv.CV_16SC1)
            cv.SetZero(matdx)
            cv.SetZero(matdy)
            cv.Copy(dx, matdx, Mask)

            cv.Copy(dy, matdy, Mask)
            a = numpy.asarray(matdx)
            b = numpy.asarray(matdy)

            #Reshaping to one dimension
            ar = numpy.reshape(a, a.size)
            br = numpy.reshape(b, b.size)

            #Calculating Arc Tangent with quadrant information
            c = numpy.arctan2(br, ar)
            #Turning it to -180 to 180 range

            z = numpy.multiply(c, self.constant)
            result = z.astype(numpy.int32)

            result[result < 0] += 360
            tang = numpy.reshape(result, (height, width))
            tang = tang.astype(numpy.float32)
            mat = cv.fromarray(tang)
            cv.Copy(mat, tangent16U)
        else:
            dxTemp = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_16S, 1)
            dyTemp = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_16S, 1)
            zero = cv.CreateImage(cv.GetSize(dx), cv.IPL_DEPTH_16S, 1)

            cv.Add(zero, dx, dxTemp, Mask)
            cv.Add(zero, dy, dyTemp, Mask)

            dx = dxTemp
            dy = dyTemp

            for x in range(divSize[0]):
                for y in range(divSize[1]):
                    if Mask[y, x] == 0:
                        tangent16U[y, x] = 0
                        continue
                    tang = math.atan2(dy[y, x], dx[y, x]) * self.constant
                    tangent16U[y, x] = int(tang)

        if self.visualize:
            #tangent2 = cv.CreateImage(cv.GetSize(dy), cv.CV_16SC1, 1)
            cv.ConvertScaleAbs(tangent16U, tangent)
            cv.EqualizeHist(tangent, tangent)
            while True:
                cv.NamedWindow("Tangent")
                cv.ShowImage("Tangent", tangent)
                c = cv.WaitKey(5)
                if c > 0:
                    break
        cv.DestroyAllWindows()

        return tangent16U
Exemple #15
0
    def __init__(self):
        rospy.init_node('avi2ros', anonymous=True)
        
        self.input = rospy.get_param("~input", "")
        self.output = rospy.get_param("~output", "video_output")       
        self.fps = rospy.get_param("~fps", 25)
        self.loop = rospy.get_param("~loop", False)
        self.width = rospy.get_param("~width", "")
        self.height = rospy.get_param("~height", "")
        self.start_paused = rospy.get_param("~start_paused", False)
        self.show_viz = not rospy.get_param("~headless", False)
        self.show_text = True

        image_pub = rospy.Publisher(self.output, Image, queue_size=10)
        
        rospy.on_shutdown(self.cleanup)
        
        video = cv.CaptureFromFile(self.input)
        fps = int(cv.GetCaptureProperty(video, cv.CV_CAP_PROP_FPS))
        
        """ Bring the fps up to the specified rate """
        try:
            fps = int(fps * self.fps / fps)
        except:
            fps = self.fps
    
        if self.show_viz:
            cv.NamedWindow("AVI Video", True) # autosize the display
            cv.MoveWindow("AVI Video", 650, 100)

        bridge = CvBridge()
                
        self.paused = self.start_paused
        self.keystroke = None
        self.restart = False
        
        # Get the first frame to display if we are starting in the paused state.
        frame = cv.QueryFrame(video)
        image_size = cv.GetSize(frame)
        
        if self.width and self.height and (self.width != image_size[0] or self.height != image_size[1]):
            rospy.loginfo("Resizing! " + str(self.width) + " x " + str(self.height))
            resized_frame = cv.CreateImage((self.width, self.height), frame.depth, frame.channels)
            cv.Resize(frame, resized_frame)
            frame = cv.CloneImage(resized_frame)
                        
        text_frame = cv.CloneImage(frame)
        cv.Zero(text_frame)
    
        while not rospy.is_shutdown():
            """ Handle keyboard events """
            self.keystroke = cv.WaitKey(1000 / fps)

            """ Process any keyboard commands """
            if 32 <= self.keystroke and self.keystroke < 128:
                cc = chr(self.keystroke).lower()
                if cc == 'q':
                    """ user has press the q key, so exit """
                    rospy.signal_shutdown("User hit q key to quit.")
                elif cc == ' ':
                    """ Pause or continue the video """
                    self.paused = not self.paused
                elif cc == 'r':
                    """ Restart the video from the beginning """
                    self.restart = True
                elif cc == 't':
                    """ Toggle display of text help message """
                    self.show_text = not self.show_text
                
            if self.restart:
                #video = cv.CaptureFromFile(self.input)
                print "restarting video from beginning"
                cv.SetCaptureProperty(video, cv.CV_CAP_PROP_POS_AVI_RATIO, 0)
                self.restart = None
    
            if not self.paused:
                frame = cv.QueryFrame(video)
                if frame and self.width and self.height:
                    if self.width != image_size[0] or self.height != image_size[1]:
                        cv.Resize(frame, resized_frame)
                        frame = cv.CloneImage(resized_frame)
                
            if frame == None:
                if self.loop:
                    self.restart = True
            else:
                if self.show_text:
                    frame_size = cv.GetSize(frame)
                    text_font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.2, 1, 0, 1, 8)
                    cv.PutText(text_frame, "Keyboard commands:", (20, int(frame_size[1] * 0.6)), text_font, cv.RGB(255, 255, 0))
                    cv.PutText(text_frame, " ", (20, int(frame_size[1] * 0.65)), text_font, cv.RGB(255, 255, 0))
                    cv.PutText(text_frame, "space - toggle pause/play", (20, int(frame_size[1] * 0.72)), text_font, cv.RGB(255, 255, 0))
                    cv.PutText(text_frame, "     r - restart video from beginning", (20, int(frame_size[1] * 0.79)), text_font, cv.RGB(255, 255, 0))
                    cv.PutText(text_frame, "     t - hide/show this text", (20, int(frame_size[1] * 0.86)), text_font, cv.RGB(255, 255, 0))
                    cv.PutText(text_frame, "     q - quit the program", (20, int(frame_size[1] * 0.93)), text_font, cv.RGB(255, 255, 0))
                
                cv.Add(frame, text_frame, text_frame)
                if self.show_viz:
                    cv.ShowImage("AVI Video", text_frame)
                cv.Zero(text_frame)
                
                try:
                    test = np.asarray(frame[:,:])
                    publishing_image = bridge.cv2_to_imgmsg(test, "bgr8")
                    image_pub.publish(publishing_image)
                except CvBridgeError, e:
                    print e         
Exemple #16
0
    def create_kde_with_trips(self, all_trips, cell_size, gaussian_blur):

        # print ("trips path: ") + str(trips_path)
        print("cell size: ") + str(cell_size)
        print("gaussian blur: ") + str(gaussian_blur)
        conf = ConfigurationManager()
        prefix = conf.getProperty(Constants.output, Constants.prefix)

        sys.stdout.write("\nFinding bounding box... ")
        sys.stdout.flush()

        min_lat = all_trips[0].locations[0].latitude
        max_lat = all_trips[0].locations[0].latitude
        min_lon = all_trips[0].locations[0].longitude
        max_lon = all_trips[0].locations[0].longitude

        # 寻找地图的边界,会存在temp/bounding_boxes/ 目录下
        for trip in all_trips:
            for location in trip.locations:
                if (location.latitude < min_lat):
                    min_lat = location.latitude

                if (location.latitude > max_lat):
                    max_lat = location.latitude

                if (location.longitude < min_lon):
                    min_lon = location.longitude

                if (location.longitude > max_lon):
                    max_lon = location.longitude

        print("done.")

        # find bounding box for data
        min_lat -= 0.0003
        max_lat += 0.0003
        min_lon -= 0.0005
        max_lon += 0.0005

        diff_lat = max_lat - min_lat
        diff_lon = max_lon - min_lon

        trip_file = open(prefix + "bounding_boxes/bounding_box_1m.txt", 'w')
        bound_str = str(min_lat) + " " + str(min_lon) + " " + str(
            max_lat) + " " + str(max_lon)
        trip_file.write(bound_str)
        trip_file.close()

        width = int(diff_lon * spatialfunclib.METERS_PER_DEGREE_LONGITUDE /
                    cell_size)
        height = int(diff_lat * spatialfunclib.METERS_PER_DEGREE_LATITUDE /
                     cell_size)
        yscale = height / diff_lat  # pixels per lat
        xscale = width / diff_lon  # pixels per lon

        # aggregate intensity map for all traces
        # themap = cv.CreateMat(height,width,cv.CV_8U)
        themap = cv.CreateMat(height, width, cv.CV_16UC1)
        cv.SetZero(themap)

        ## Build an aggregate intensity map from all the edges

        trip_counter = 1

        for trip in all_trips:

            if ((trip_counter % 10 == 0) or (trip_counter == len(all_trips))):
                sys.stdout.write("\rCreating histogram (trip " +
                                 str(trip_counter) + "/" +
                                 str(len(all_trips)) + ")... ")
                sys.stdout.flush()
            trip_counter += 1

            temp = cv.CreateMat(height, width, cv.CV_8UC1)
            cv.SetZero(temp)
            temp16 = cv.CreateMat(height, width, cv.CV_16UC1)
            cv.SetZero(temp16)

            for (orig, dest) in pairwise(trip.locations):
                oy = height - int(yscale * (orig.latitude - min_lat))
                ox = int(xscale * (orig.longitude - min_lon))
                dy = height - int(yscale * (dest.latitude - min_lat))
                dx = int(xscale * (dest.longitude - min_lon))
                cv.Line(temp, (ox, oy), (dx, dy), (32), 1, cv.CV_AA)
            #  图片 线段的第一个点 第二个点 线条颜色 线粗细 线类型 shift(点坐标中的小数位数)
            #   参数解释:8(8连通线) 4(4连通线) CV_AA(抗锯齿线)

            # accumulate trips into themap
            cv.ConvertScale(temp, temp16, 1, 0)
            # 源数组 目标数组 比例因子 将值添加到缩放后的源数组元素
            # 使用可选的线性变换将一个数组转换为另外一个数组
            # 用途:将一个数组复制到另外一个数组
            cv.Add(themap, temp16, themap)

        lines = cv.CreateMat(height, width, cv.CV_8U)
        cv.SetZero(lines)

        print("done.")

        trip_counter = 1

        for trip in all_trips:

            if ((trip_counter % 10 == 0) or (trip_counter == len(all_trips))):
                sys.stdout.write("\rCreating drawing (trip " +
                                 str(trip_counter) + "/" +
                                 str(len(all_trips)) + ")... ")
                sys.stdout.flush()
            trip_counter += 1

            for (orig, dest) in pairwise(trip.locations):
                oy = height - int(yscale * (orig.latitude - min_lat))
                ox = int(xscale * (orig.longitude - min_lon))
                dy = height - int(yscale * (dest.latitude - min_lat))
                dx = int(xscale * (dest.longitude - min_lon))
                cv.Line(lines, (ox, oy), (dx, dy), (255), 1, cv.CV_AA)

        # save the lines
        cv.SaveImage(prefix + "raw_data.png", lines)
        print("done.")
        # print "Intensity map acquired."
        sys.stdout.write("Smoothing... ")
        sys.stdout.flush()

        # # create the mask and compute the contour
        cv.Smooth(themap, themap, cv.CV_GAUSSIAN, gaussian_blur, gaussian_blur)
        cv.SaveImage(prefix + "kde.png", themap)

        print("done.")
        print("\nKDE generation complete.")
Exemple #17
0
def add_image(image1, image2):
    new_image = cv.CreateImage((image1.width, image1.height), image1.depth, 1)
    cv.Add(image1, image2, new_image, None)
    return new_image
Exemple #18
0
    def find(self, img):
        started = time.time()
        gray = self.Cached('gray', img.height, img.width, cv.CV_8UC1)
        cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

        sobel = self.Cached('sobel', img.height, img.width, cv.CV_16SC1)
        sobely = self.Cached('sobely', img.height, img.width, cv.CV_16SC1)

        cv.Sobel(gray, sobel, 1, 0)
        cv.Sobel(gray, sobely, 0, 1)
        cv.Add(sobel, sobely, sobel)

        sobel8 = self.Cached('sobel8', sobel.height, sobel.width, cv.CV_8UC1)
        absnorm8(sobel, sobel8)
        cv.Threshold(sobel8, sobel8, 128.0, 255.0, cv.CV_THRESH_BINARY)

        sobel_integral = self.Cached('sobel_integral', img.height + 1,
                                     img.width + 1, cv.CV_32SC1)
        cv.Integral(sobel8, sobel_integral)

        d = 16
        _x1y1 = cv.GetSubRect(
            sobel_integral,
            (0, 0, sobel_integral.cols - d, sobel_integral.rows - d))
        _x1y2 = cv.GetSubRect(
            sobel_integral,
            (0, d, sobel_integral.cols - d, sobel_integral.rows - d))
        _x2y1 = cv.GetSubRect(
            sobel_integral,
            (d, 0, sobel_integral.cols - d, sobel_integral.rows - d))
        _x2y2 = cv.GetSubRect(
            sobel_integral,
            (d, d, sobel_integral.cols - d, sobel_integral.rows - d))

        summation = cv.CloneMat(_x2y2)
        cv.Sub(summation, _x1y2, summation)
        cv.Sub(summation, _x2y1, summation)
        cv.Add(summation, _x1y1, summation)
        sum8 = self.Cached('sum8', summation.height, summation.width,
                           cv.CV_8UC1)
        absnorm8(summation, sum8)
        cv.Threshold(sum8, sum8, 32.0, 255.0, cv.CV_THRESH_BINARY)

        cv.ShowImage("sum8", sum8)
        seq = cv.FindContours(sum8, cv.CreateMemStorage(), cv.CV_RETR_EXTERNAL)
        subimg = cv.GetSubRect(img, (d / 2, d / 2, sum8.cols, sum8.rows))
        t_cull = time.time() - started

        seqs = []
        while seq:
            seqs.append(seq)
            seq = seq.h_next()

        started = time.time()
        found = {}
        print 'seqs', len(seqs)
        for seq in seqs:
            area = cv.ContourArea(seq)
            if area > 1000:
                rect = cv.BoundingRect(seq)
                edge = int((14 / 14.) * math.sqrt(area) / 2 + 0.5)
                candidate = cv.GetSubRect(subimg, rect)
                sym = self.dm.decode(
                    candidate.width,
                    candidate.height,
                    buffer(candidate.tostring()),
                    max_count=1,
                    #min_edge = 6,
                    #max_edge = int(edge)      # Units of 2 pixels
                )
                if sym:
                    onscreen = [(d / 2 + rect[0] + x, d / 2 + rect[1] + y)
                                for (x, y) in self.dm.stats(1)[1]]
                    found[sym] = onscreen
                else:
                    print "FAILED"
        t_brute = time.time() - started
        print "cull took", t_cull, "brute", t_brute
        return found
import cv2.cv as cv  #or simply import cv

im = cv.LoadImage("../img/lena.jpg")
im2 = cv.LoadImage("../img/fruits-larger.jpg")
cv.ShowImage("Image1", im)
cv.ShowImage("Image2", im2)

res = cv.CreateImage(cv.GetSize(im2), 8, 3)

cv.Add(
    im, im2, res
)  #Add every pixels together (black is 0 so low change and white overload anyway)
cv.ShowImage("Add", res)

cv.AbsDiff(im, im2, res)  # Like minus for each pixel im(i) - im2(i)
cv.ShowImage("AbsDiff", res)

cv.Mul(im, im2, res)  #Multiplie each pixels (almost white)
cv.ShowImage("Mult", res)

cv.Div(im, im2,
       res)  #Values will be low so the image will likely to be almost black
cv.ShowImage("Div", res)

cv.And(im, im2, res)  #Bit and for every pixels
cv.ShowImage("And", res)

cv.Or(im, im2, res)  # Bit or for every pixels
cv.ShowImage("Or", res)

cv.Not(im, res)  # Bit not of an image
Exemple #20
0
    # no need to pad bottom part of dft_A with zeros because of
    # use nonzero_rows parameter in cv.FT() call below

    cv.DFT( dft_A, dft_A, cv.CV_DXT_FORWARD, complexInput.height )

    cv.NamedWindow("win", 0)
    cv.NamedWindow("magnitude", 0)
    cv.ShowImage("win", im)

    # Split Fourier in real and imaginary parts
    cv.Split( dft_A, image_Re, image_Im, None, None )

    # Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
    cv.Pow( image_Re, image_Re, 2.0)
    cv.Pow( image_Im, image_Im, 2.0)
    cv.Add( image_Re, image_Im, image_Re, None)
    cv.Pow( image_Re, image_Re, 0.5 )

    # Compute log(1 + Mag)
    cv.AddS( image_Re, cv.ScalarAll(1.0), image_Re, None ) # 1 + Mag
    cv.Log( image_Re, image_Re ) # log(1 + Mag)


    # Rearrange the quadrants of Fourier image so that the origin is at
    # the image center
    cvShiftDFT( image_Re, image_Re )

    min, max, pt1, pt2 = cv.MinMaxLoc(image_Re)
    cv.Scale(image_Re, image_Re, 1.0/(max-min), 1.0*(-min)/(max-min))
    cv.ShowImage("magnitude", image_Re)
Exemple #21
0
import cv2.cv as cv

im = cv.LoadImage('meinv.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE)

sobx = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1)
cv.Sobel(im, sobx, 1, 0, 3)  #Sobel with x-order=1

soby = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1)
cv.Sobel(im, soby, 0, 1, 3)  #Sobel withy-oder=1

cv.Abs(sobx, sobx)
cv.Abs(soby, soby)

result = cv.CloneImage(im)
cv.Add(sobx, soby, result)  #Add the two results together.

cv.Threshold(result, result, 100, 255, cv.CV_THRESH_BINARY_INV)

cv.ShowImage('Image', im)
cv.ShowImage('Result', result)

cv.WaitKey(0)
Exemple #22
0
    #there can be noise in the video so ignore objects with small areas
    if (area > 1000):
        #determine the x and y coordinates of the center of the object
        #we are tracking by dividing the 1, 0 and 0, 1 moments by the area
        x = cv.GetSpatialMoment(moments, 1, 0) / area
        y = cv.GetSpatialMoment(moments, 0, 1) / area

        ##        print 'x: ' + str(x) + ' y: ' + str(y) + ' area: ' + str(area)

        #create an overlay to mark the center of the tracked object
        overlay = cv.CreateImage(cv.GetSize(F), 8, 3)

        cv.Circle(overlay, (int(x), int(y)), 2, (255, 255, 255), 20)
        cv.Circle(tr, (int(x), int(y)), 10, (255, 255, 255), -20)
        cv.Add(F, overlay, F)
        #add the thresholded image back to the img so we can see what was
        #left after it was applied
        cv.Merge(tr, None, None, None, F)

    threshold = thresh.copy()
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(thresh, contours, -1, (255, 255, 255), 5)

    img = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
    blur = cv2.medianBlur(f, 5)
    circles = cv2.HoughCircles(img,
                               cv2.cv.CV_HOUGH_GRADIENT,
                               1,
                               10,
    imgYellowTresh = getThresholdImage(frame)  #Apply the threshold function

    moments = cv.Moments(cv.GetMat(imgYellowTresh), 1)
    moment10 = cv.GetSpatialMoment(moments, 1, 0)
    moment01 = cv.GetSpatialMoment(moments, 0, 1)
    area = cv.GetCentralMoment(moments, 0, 0)  #Get the center

    lastx = posx
    lasty = posy
    if area == 0:
        posx = 0
        posy = 0
    else:
        posx = moment10 / area
        posy = moment01 / area

    if lastx > 0 and lasty > 0 and posx > 0 and posy > 0:  #Mean we have received coordinates to print
        #Draw the line
        cv.Line(imgScribble, (int(posx), int(posy)), (int(lastx), int(lasty)),
                cv.Scalar(0, 255, 255), 3, 1)

    #Add the frame and the line image to see lines on the webcam frame
    cv.Add(frame, imgScribble, frame)

    cv.ShowImage("video", frame)
    cv.ShowImage("thresh", imgYellowTresh)
    c = cv.WaitKey(1)
    if c == 27 or c == 1048603:  #Break if user enters 'Esc'.
        break
    elif c == 1048690:  # 'r' for reset
        cv.Zero(imgScribble)
    def run(self):
        logging.debug(' starting run ')
        global samecolorclient
        global capture
        global centroidList  #abh
        global lock  #abh
        global lock2  #abh
        global lock3  #abh
        global lock4  #abh
        mydata = threading.local()
        #window1=" Color Detection"
        mydata.window2 = str(self.name) + " Threshold"
        #cv.NamedWindow(window1,0)
        lock4.acquire()  #abh
        cv.NamedWindow(mydata.window2, 0)
        lock4.release()  #abh
        mydata.centroidold = [0, 0]
        mydata.flag = 0
        mydata.roi = [100, 22, 390, 390]
        #mydata.roi=[95,40,380,350]
        while True:
            lock2.acquire()  #abh
            lock4.acquire()  #abh
            mydata.color_image = cv.QueryFrame(capture)
            lock4.release()  #abh
            lock2.release()  #abh
            if (mydata.flag == 0):
                lock4.acquire  #abh lock4.release        #abh
                mydata.color_image = cv.GetSubRect(mydata.color_image,
                                                   (100, 22, 390, 390))
                lock4.release  #abh
            else:
                lock4.acquire  #abh lock4.release        #abh
                mydata.color_image = cv.GetSubRect(
                    mydata.color_image,
                    (int(mydata.roi[0]), int(mydata.roi[1]), int(
                        mydata.roi[2]), int(mydata.roi[3])))
                lock4.release  #abh
            lock4.acquire  #abh lock4.release        #abh
            cv.Flip(mydata.color_image, mydata.color_image, 1)
            cv.Smooth(mydata.color_image, mydata.color_image, cv.CV_MEDIAN, 3,
                      0)
            #logging.debug(' Starting getthresholdedimg ')
            mydata.imghsv = cv.CreateImage(cv.GetSize(mydata.color_image), 8,
                                           3)
            cv.CvtColor(mydata.color_image, mydata.imghsv,
                        cv.CV_BGR2YCrCb)  # Convert image from RGB to HSV
            mydata.imgnew = cv.CreateImage(cv.GetSize(mydata.color_image),
                                           cv.IPL_DEPTH_8U, 1)
            mydata.imgthreshold = cv.CreateImage(
                cv.GetSize(mydata.color_image), 8, 1)
            lock4.release  #abh
            mydata.c = self.color[0]
            mydata.minc = (float(mydata.c[0]), float(mydata.c[1]),
                           float(mydata.c[2]))
            mydata.c = self.color[1]
            mydata.maxc = (float(mydata.c[0]), float(mydata.c[1]),
                           float(mydata.c[2]))
            lock4.acquire  #abh lock4.release        #abh
            cv.InRangeS(mydata.imghsv, cv.Scalar(*(mydata.minc)),
                        cv.Scalar(*(mydata.maxc)), mydata.imgnew)
            cv.Add(mydata.imgnew, mydata.imgthreshold, mydata.imgthreshold)
            #logging.debug(' Exiting getthreasholdedimg')
            #logging.debug('function returned from thresholdedimg')
            cv.Erode(mydata.imgthreshold, mydata.imgthreshold, None, 1)
            cv.Dilate(mydata.imgthreshold, mydata.imgthreshold, None, 4)
            mydata.img2 = cv.CloneImage(mydata.imgthreshold)
            mydata.storage = cv.CreateMemStorage(0)
            mydata.contour = cv.FindContours(mydata.imgthreshold,
                                             mydata.storage,
                                             cv.CV_RETR_EXTERNAL,
                                             cv.CV_CHAIN_APPROX_SIMPLE)
            lock4.release  #abh
            mydata.points = []
            #logging.debug('Starting while contour')
            while mydata.contour:
                # Draw bounding rectangles
                lock4.acquire  #abh lock4.release        #abh
                mydata.bound_rect = cv.BoundingRect(list(mydata.contour))
                lock4.release  #abh
                mydata.contour = mydata.contour.h_next()
                mydata.pt1 = (mydata.bound_rect[0], mydata.bound_rect[1])
                mydata.pt2 = (mydata.bound_rect[0] + mydata.bound_rect[2],
                              mydata.bound_rect[1] + mydata.bound_rect[3])
                mydata.points.append(mydata.pt1)
                mydata.points.append(mydata.pt2)
                lock4.acquire  #abh lock4.release        #abh
                cv.Rectangle(
                    mydata.color_image, mydata.pt1, mydata.pt2,
                    cv.CV_RGB(mydata.maxc[0], mydata.maxc[1], mydata.maxc[2]),
                    1)
                lock4.release  #abh
                # Calculating centroids
                if (((mydata.bound_rect[2]) * (mydata.bound_rect[3])) < 3500):
                    #logging.debug('Inside iffffffffffffffffffffffff')
                    lock4.acquire  #abh lock4.release        #abh
                    mydata.centroidx = cv.Round(
                        (mydata.pt1[0] + mydata.pt2[0]) / 2)
                    mydata.centroidy = cv.Round(
                        (mydata.pt1[1] + mydata.pt2[1]) / 2)
                    lock4.release  #abh
                    if (mydata.flag == 1):
                        #logging.debug("inside flag1")
                        mydata.centroidx = mydata.roi[0] + mydata.centroidx
                        mydata.centroidy = mydata.roi[1] + mydata.centroidy
                    mydata.centroidnew = [mydata.centroidx, mydata.centroidy]
                    #logging.debug('mydataroi[0] '+str(mydata.roi[0]) + ';centroidx ' + str(mydata.centroidx))
                    #logging.debug('mydataroi[1] '+str(mydata.roi[1]) + ';centroidy ' + str(mydata.centroidy))
                    #print mydata.centroidx                                 #abh
                    #print mydata.centroidy                                 #abh
                    mydata.tmpclient = []
                    lock3.acquire()  #abh
                    mydata.tmpclient = samecolorclient[self.i]
                    lock3.release()  #abh
                    mydata.distance = math.sqrt(
                        math.pow((mydata.centroidnew[0] -
                                  mydata.centroidold[0]), 2) +
                        math.pow((mydata.centroidnew[1] -
                                  mydata.centroidold[1]), 2))
                    #lock.acquire()                                         #abh                                                            #abh commented
                    for mydata.j in range(len(mydata.tmpclient)):
                        mydata.client_socket = mydata.tmpclient[mydata.j]
                        #logging.debug('before centroid send...')
                        if (mydata.distance >= 1.50):
                            print 'inside 1.50 '

                            #self.server_socket.sendto(str(mydata.centroidnew),mydata.client_socket) #abh
                            lock.acquire()  #abh
                            centroidList[colorlist.index(
                                self.color)] = mydata.centroidnew  #abh
                            del mydata.centroidold[:]
                            #logging.debug(str(centroidList))
                            self.server_socket.sendto(
                                str(centroidList), mydata.client_socket)  #abh
                            lock.release()  #abh
                            #logging.debug ('updating done.')                                                 #abh
                            #print centroidList                                                       #abh
                            mydata.centroidold = mydata.centroidnew[:]
                        else:
                            #self.server_socket.sendto(str(mydata.centroidold),mydata.client_socket) #abh
                            lock.acquire()  #abh
                            centroidList[colorlist.index(
                                self.color)] = mydata.centroidold  #abh
                            #logging.debug(str(centroidList))
                            self.server_socket.sendto(
                                str(centroidList), mydata.client_socket)  #abh
                            lock.release()  #abh
                            #logging.debug ('updating done2.')                                                  #abh
                            #print centroidList                                                       #abh
                    #    logging.debug('byte sent to client')
                    #lock.release()                                         #abh
                    mydata.roi[0] = mydata.centroidx - 50
                    mydata.roi[1] = mydata.centroidy - 50
                    if (mydata.roi[0] < 95):
                        mydata.roi[0] = 95
                    if (mydata.roi[1] < 40):
                        mydata.roi[1] = 40
                    mydata.roi[2] = 100
                    mydata.roi[3] = 100
                    if ((mydata.roi[0] + mydata.roi[2]) > 475):
                        mydata.roi[0] = mydata.roi[0] - (
                            (mydata.roi[0] + mydata.roi[2]) - 475)
                    if ((mydata.roi[1] + mydata.roi[3]) > 390):
                        mydata.roi[1] = mydata.roi[1] - (
                            (mydata.roi[1] + mydata.roi[3]) - 390)
                    #del mydata.centroidnew[:]
                    mydata.flag = 1
            if mydata.contour is None:
                mydata.flag = 0
            #cv.ShowImage(window1,mydata.color_image)
            lock4.acquire  #abh lock4.release        #abh
            cv.ShowImage(mydata.window2, mydata.img2)
            lock4.release  #abh

            if cv.WaitKey(33) == 27:  #here it was 33 instead of 10
                #cv.DestroyWindow(mydata.window1)
                #cv.DestroyWindow(mydata.window2)
                break