예제 #1
0
def repeat():
    global capture  #declare as globals since we are assigning to them now
    global camera_index
    global done

    frame = cv.QueryFrame(capture)
    cv.Smooth(frame, frame, cv.CV_GAUSSIAN, 3, 3)

    imgHsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
    cv.CvtColor(frame, imgHsv, cv.CV_BGR2HSV)
    #imgHsv2 = GetThresholdedImage(imgHsv)
    #print(numpy.asarray(cv.GetMat(imgHsv)))

    imgRGBA = cv.CreateImage(cv.GetSize(frame), 8, 4)
    cv.CvtColor(frame, imgRGBA, cv.CV_BGR2RGBA)

    cv.Smooth(imgRGBA, imgRGBA, cv.CV_GAUSSIAN, 3, 3)
    (filteredImg, offsetX, offsetY) = parallelSumRed(imgRGBA, 640,
                                                     480)  #3D array

    d = numpy.sqrt(offsetX * offsetX + offsetY * offsetY)

    if d != 0:
        print("Distance = " + str(c1 / d + c2) + "cm")
        print("OffsetX = " + str(offsetX) + "; OffsetY = " + str(offsetY))
        print("")

    imgRGB = cv.CreateImage(cv.GetSize(frame), 8, 3)
    #cv.CvtColor(Image.fromarray(filteredImg), imgRGB, cv.CV_RGBA2RGB)

    imgRGBA = cv.fromarray(numpy.reshape(filteredImg, (480, 640, 4)))
    if offsetX != 0 or offsetY != 0:
        cv.Rectangle(imgRGBA, (320 + offsetX - 6, 240 + offsetY - 6),
                     (320 + offsetX + 6, 240 + offsetY + 6),
                     (255, 0, 255, 255), 1, 8)
        cv.Line(imgRGBA, (0, 240 + offsetY), (639, 240 + offsetY),
                (255, 0, 255, 255), 1, 8)
        cv.Line(imgRGBA, (320 + offsetX, 0), (320 + offsetX, 479),
                (255, 0, 255, 255), 1, 8)

    cv.ShowImage(HSVWindow, imgRGBA)
    cv.ShowImage(original, frame)

    cv.SetMouseCallback(original, onMouseMove, [
        cv.CV_EVENT_MOUSEMOVE,
        numpy.asarray(cv.GetMat(imgHsv)),
        numpy.asarray(cv.GetMat(frame))
    ])
    #cv.SetMouseCallback(HSVWindow, onMouseMove, [cv.CV_EVENT_MOUSEMOVE, numpy.asarray(cv.GetMat(imgHsv)), numpy.asarray(cv.GetMat(frame))])

    #cv.ShowImage(filtered, imgHsv2)
    c = cv.WaitKey(10)

    if (str(c) == "27"):  #if ESC is pressed
        print("Thank You!")
        done = True
    if (str(c) == "99"):  #'c' for calibration
        calibration(int(input("How many data points: ")))
예제 #2
0
def get_image(camera, filename=None):
    im = cv.QueryFrame(camera)

    # take greyscale and compute RMS value
    im2 = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_32F, 3)
    cv.Convert(im, im2)
    gray = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_32F, 1)
    cv.CvtColor(im2, gray, cv.CV_RGB2GRAY)
    gray_mat = cv.GetMat(gray)
    img = numpy.asarray(gray_mat)

    power = numpy.sqrt(numpy.mean(img**2))

    #save file
    if filename:
        font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.8, 0.8, 0, 2,
                           cv.CV_AA)
        cv.PutText(im, filename, (DATE_X, DATE_Y), font, cv.RGB(255, 255, 0))
        filename = os.path.join(DIR_PREFIX, filename + '.jpg')
        print filename
        cv.SaveImage(filename, im)
        del font
    else:
        filename = ''

    #del(camera)
    del im, im2, gray, img, gray_mat

    return (power, filename)
def genFeatres(img_list):
    network = slminit()

    filenames = img_list

    index = 0
    faceVectors = []
    for img in filenames:
        entry1 = img
        src1 = cv.LoadImageM(entry1)
        gray_full1 = cv.CreateImage(cv.GetSize(src1), 8, 1)
        grayim1 = cv.CreateImage((200, 200), 8, 1)
        cv.CvtColor(src1, gray_full1, cv.CV_BGR2GRAY)
        cv.Resize(gray_full1, grayim1, interpolation=cv.CV_INTER_CUBIC)
        gray1 = cv.GetMat(grayim1)
        im_array1 = np.asarray(gray1).astype('f')
        # -- compute feature map, shape [height, width, depth]
        f_map1 = slmprop(im_array1, network)
        f_map_dims1 = f_map1.shape
        image_vector = []
        for j in range(f_map_dims1[0]):
            for k in range(f_map_dims1[1]):
                for l in range(f_map_dims1[2]):
                    image_vector.append(f_map1[j][k][l])
        print index
        index = index + 1
        faceVectors.append(np.asarray(image_vector))
    return faceVectors
예제 #4
0
    def getMat(self):
        hdc = win32gui.GetWindowDC(self.hwnd)
        dc_obj = win32ui.CreateDCFromHandle(hdc)
        memorydc = dc_obj.CreateCompatibleDC()

        data_bitmap = win32ui.CreateBitmap()
        data_bitmap.CreateCompatibleBitmap(dc_obj, self.width, self.height)

        memorydc.SelectObject(data_bitmap)
        memorydc.BitBlt((0, 0), (self.width, self.height), dc_obj, (self.dx, self.dy), win32con.SRCCOPY)

        bmpheader = struct.pack("LHHHH", struct.calcsize("LHHHH"),
                                self.width, self.height, 1, 24)
        c_bmpheader = ctypes.create_string_buffer(bmpheader)

        # padded_length = (string_length + 3) & -3 for 4-byte aligned.
        c_bits = ctypes.create_string_buffer(" " * (self.width * ((self.height * 3 + 3) & -3)))

        res = ctypes.windll.gdi32.GetDIBits(memorydc.GetSafeHdc(), data_bitmap.GetHandle(),
                                            0, self.height, c_bits, c_bmpheader, win32con.DIB_RGB_COLORS)

        win32gui.DeleteDC(hdc)
        win32gui.ReleaseDC(self.hwnd, hdc)
        memorydc.DeleteDC()
        win32gui.DeleteObject(data_bitmap.GetHandle())

        cv_im = cv.CreateImageHeader((self.width, self.height), cv.IPL_DEPTH_8U, 3)
        cv.SetData(cv_im, c_bits.raw)
        # flip around x-axis
        cv.Flip(cv_im, None, 0)

        mat = cv.GetMat(cv_im)
        return numpy.asarray(mat)
예제 #5
0
 def resize(self, frame):
     img = cv.GetMat(frame)
     for _ in range(self.resize_num):
         w, h = cv.GetSize(img)
         small = cv.CreateMat((h + 1) / 2, (w + 1) / 2, img.type)
         cv.PyrDown(img, small)
         img = cv.CreateMat((h + 1) / 2, (w + 1) / 2, img.type)
         cv.Copy(small, img)
     return small
예제 #6
0
def test():
    #start = time.time()
    src1 = cv.GetMat(cv.LoadImage("d1.png", 0))
    src2 = cv.GetMat(cv.LoadImage("d2.png", 0))

    pt1 = (100, 100)
    pt2 = (200, 200)

    crop1 = src1[pt1[1]:pt2[1], pt1[0]:pt2[0]]
    crop2 = src2[pt1[1]:pt2[1], pt1[0]:pt2[0]]

    #cv2.GetMat

    hist1 = compute_histogram(crop1)

    hist2 = compute_histogram(crop2)

    sc = cv.CompareHist(hist1, hist2, cv.CV_COMP_CHISQR)

    print sc
예제 #7
0
파일: lrf.py 프로젝트: Mnemonic7/lrf
def calculate_laser_position(img):
    print "calculating moments"
    moments = cv.Moments(cv.GetMat(img))
    posX = -1
    posY = -1
    area = cv.GetCentralMoment(moments, 0, 0)
    if area > 0:
        moment10 = cv.GetSpatialMoment(moments, 1, 0)
        moment01 = cv.GetSpatialMoment(moments, 0, 1)
        posX = int(moment10 / area)
        posY = int(moment01 / area)

    return posX, posY, area
예제 #8
0
    def getAverageOpticalFlow(self, image, binary_mask):

        avgLength = self.distance(self.avgX, self.avgY)    
        print "avg length " + str(avgLength)   
        step = 5
        
        (w, h, channels) = np.shape(image)
        motionCanvas = np.zeros((w, h, channels), np.uint8)
        
#         motionCanvas2 = np.ones((w, h, channels), np.uint8)
        
#         cv2.polylines(motionCanvas, self.lines, 0, (0, 255, 0))

        for (x1, y1), (x2, y2) in self.lines:
            length = self.euclideanDistance(x1, y1, x2, y2)
#             print "length " + str(length)
            if length > 0.8:  # avgLength * 40:
                cv2.line(motionCanvas, (x1, y1), (x2, y2), (255, 255, 255))
#             cv2.circle(motionCanvas, (x1, y1), 1, (0, 255, 0), -1)

#         cv2.line(motionCanvas, (0, 0), (100, 100), (0, 255, 0), 5)
        
        cv2.imshow("motion", motionCanvas)
        
        
        
        print type(binary_mask)
#         (w, h) = cv.GetSize(binary_mask)
#         zerosArray = np.zeros((h, w), np.uint8, 3)

        if binary_mask != None:
         
            maskMat = cv.GetMat(binary_mask)
    #          
    #         (w, h) = cv.GetSize(binary_mask)
    #         maskMat2 = cv.CreateMat(w, h, cv.CV_8UC3)
    #         cv.CvtColor(maskMat, maskMat2, cv.CV_GRAY2BGR)
            maskArray = np.asarray(maskMat, np.uint8, 3)
    #         
            motionCanvas2 = np.copy(motionCanvas)
            
            print "size of input image " + str(np.shape(maskArray)) + " " + str(np.shape(motionCanvas2))
    #         
    #         print type(motionCanvas) , np.shape(motionCanvas)
    #         print type(maskArray) , np.shape(maskArray)
            motionCanvas2 = cv2.bitwise_and(motionCanvas2, motionCanvas2, mask=maskArray)
    #         motionCanvas2 = cv2.add(motionCanvas, zerosArray, motionCanvas2, maskArray)
            
            cv2.imshow("motion 2 ", motionCanvas2)
            
            self.opticalPath.findPath(motionCanvas2)
예제 #9
0
def main():

    # captured image size, change to whatever you want
    width = 320
    height = 240

    capture = cv.CreateCameraCapture(0)

    # Over-write default captured image size
    cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width)
    cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height)

    cv.NamedWindow( "output", 1 )
    cv.NamedWindow( "processed", 1 )

    while True:

        frame = cv.QueryFrame(capture)
        cv.Smooth(frame, frame, cv.CV_BLUR, 3)

        imgColorProcessed = ColorProcess(frame)
        mat = cv.GetMat(imgColorProcessed)

        # Calculating the moments
        moments = cv.Moments(mat, 0)
        area = cv.GetCentralMoment(moments, 0, 0)
        moment10 = cv.GetSpatialMoment(moments, 1, 0)
        moment01 = cv.GetSpatialMoment(moments, 0,1)

        # Finding a big enough blob
        if(area > 60000):

            # Calculating the center postition of the blob
            posX = int(moment10 / area)
            posY = int(moment01 / area)

            # check slave status and send coordinates
            state = readData()
            if state == 1:
                sendData(posX)
                sendData(posY)
                print 'x: ' + str(posX) + ' y: ' + str(posY)

		# update video windows
        cv.ShowImage("processed", imgColorProcessed)
        cv.ShowImage("output", frame)

        if cv.WaitKey(10) >= 0:
            break

    return;
예제 #10
0
 def defineRegionOfInterest(self, image, pt1, pt2):
     x0 = pt1[0]
     y0 = pt1[1]
     x1 = pt2[0]
     y1 = pt2[1]
     self.selection = (x0, y0, x1, y1)
     
     image_mat = cv.GetMat(image)
     image_array = np.asarray(image_mat, np.uint8, 3)
     self.processObjectOfInterest(image_array)
     
     
     if self.returnToDetection:
         return
예제 #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(cv.GetMat(thresholded_img), 0)
            area = cv.GetCentralMoment(moments, 0, 0)

            #there can be noise in the video so ignore objects with small areas
            if (area > 10000):
                #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, (int(x), int(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
예제 #12
0
    def DoBinarizeRegion(self, brightness, contrast):
        #print brightness, contrast
        self.UpdateLabelOfInterest()

        PilImage2 = self.OriginalScreenshot.copy()

        x = self.AlexaAppImages[self.AppObjectFeedbackIndex].RectX + self.AlexaAppImages[self.AppObjectFeedbackIndex].CropRegionX
        y = self.AlexaAppImages[self.AppObjectFeedbackIndex].RectY + self.AlexaAppImages[self.AppObjectFeedbackIndex].CropRegionY
        w = self.AlexaAppImages[self.AppObjectFeedbackIndex].RectX + self.AlexaAppImages[self.AppObjectFeedbackIndex].CropRegionX + self.AlexaAppImages[self.AppObjectFeedbackIndex].CropRegionWidth
        h = self.AlexaAppImages[self.AppObjectFeedbackIndex].RectY + self.AlexaAppImages[self.AppObjectFeedbackIndex].CropRegionY + self.AlexaAppImages[self.AppObjectFeedbackIndex].CropRegionHeight
        box = (x, y, w, h)

        region = PilImage2.crop(box)

        #region.save("c:\\region.png")

        enhancer = ImageEnhance.Brightness(region)
        region = enhancer.enhance(brightness)
        enhancer = ImageEnhance.Contrast(region)
        region = enhancer.enhance(contrast)

        #PilImage2.paste(region, box)

        cv_im = cv.CreateImageHeader(region.size, cv.IPL_DEPTH_8U, 3)
        cv.SetData(cv_im, region.tostring())
        mat = cv.GetMat(cv_im)
        img = numpy.asarray(mat)
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        thresh = 100
        im_bw = cv2.threshold(gray_image, thresh, 255, cv2.THRESH_BINARY)[1]

        im = Image.fromarray(im_bw)
        PilImage2.paste(im, box)

        self.QtImage1 = ImageQt.ImageQt(PilImage2)
        self.QtImage2 = QImage(self.QtImage1)
        self.pixmap = QPixmap.fromImage(self.QtImage2)

        #self.pixmap.load("im_bw.png")
        self.update()
예제 #13
0
def align(path, landmarks):
    x0, y0, x1, y1, x2, y2, x3, y3 = landmarks
    src = cv2.imread(path, 1)
    src, pl = pad_image(src)
    e0 = np.array([x0 + pl, y0 + pl])
    e1 = np.array([x1 + pl, y1 + pl])
    m0 = np.array([x2 + pl, y2 + pl])
    m1 = np.array([x3 + pl, y3 + pl])

    xx = e1 - e0
    yy = (e0 + e1) / 2. - (m0 + m1) / 2.
    c = (e0 + e1) / 2. - 0.1 * yy
    s = np.max((4. * LA.norm(xx), 3.6 * LA.norm(yy)))
    s = int(s)
    yy90 = np.array([yy[1], -yy[0]])
    x = xx - yy90
    angle = np.arctan(x[1] / x[0])

    res = subimage(src, c, angle, s, s)
    mat = cv.GetMat(res)
    res = np.asarray(mat)
    return res
예제 #14
0
파일: process.py 프로젝트: rymo4/ColorLapse
                    self.color_and_expand_pixel(row, col, expansion_width)
                else:
                    self.img[row][col] = np.array([255, 255, 255])

    def color_and_expand_pixel(self, row, col, n=1):
        width, height, t = self.color_img.shape
        if not (row <= n or row >= (width - n) or col <= n or col >=
                (height - n)):
            for i in range(-n, n + 1):
                for j in range(-n, n + 1):
                    self.img[row + i][col + j] = np.array(self.pen_color)


if __name__ == "__main__":
    vid = cv.CaptureFromFile('vid2.mov')
    img = cv.QueryFrame(vid)
    while img:
        print 'loopin!'
        tmp = cv.CreateImage(cv.GetSize(img), 8, 3)
        cv.CvtColor(img, tmp, cv.CV_BGR2RGB)
        img = np.asarray(cv.GetMat(tmp))
        print 'got the image as a numpy array'

        cl = ColorLapse(img, [50, 200, 50])
        cv2.namedWindow(cl.window)
        cv2.imshow(cl.window, cl.color_img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

        img = cv.QueryFrame(vid)
예제 #15
0
    def run(self):
        # Initialize
        log_file_name = "tracker_output.log"
        log_file = open( log_file_name, 'a' )
            #fps = 25

        #cap = cv2.VideoCapture( '../000104-.avi'

        frame = cv.QueryFrame( self.capture )
        frame_size = cv.GetSize( frame )
        foreground = cv.CreateImage(cv.GetSize(frame),8,1)
        foremat = cv.GetMat(foreground)
        Nforemat = numpy.array(foremat, dtype=numpy.float32)
	gfilter=sys.argv[2]
	gfilter_string=gfilter
	gfilter=float(gfilter)
	print "Processing Tracker with filter: " + str(gfilter)

        # Capture the first frame from webcam for image properties
        display_image = cv.QueryFrame( self.capture )

	# Create Background Subtractor
        fgbg = cv2.BackgroundSubtractorMOG()

        # Greyscale image, thresholded to create the motion mask:
        grey_image = cv.CreateImage( cv.GetSize(frame), cv.IPL_DEPTH_8U, 1 )


        # The RunningAvg() function requires a 32-bit or 64-bit image...
        running_average_image = cv.CreateImage( cv.GetSize(frame), cv.IPL_DEPTH_32F, 3 )
        # ...but the AbsDiff() function requires matching image depths:
        running_average_in_display_color_depth = cv.CloneImage( display_image )


        # RAM used by FindContours():
        mem_storage = cv.CreateMemStorage(0)


        # The difference between the running average and the current frame:
        difference = cv.CloneImage( display_image )


        target_count = 1
        last_target_count = 1
        last_target_change_t = 0.0
        k_or_guess = 1
        codebook=[]
        frame_count=0
        last_frame_entity_list = []
        fps = 25


        t0 = 165947


        # For toggling display:
        image_list = [ "camera", "shadow", "white", "threshold", "display", "yellow" ]
        image_index = 0   # Index into image_list




        # Prep for text drawing:
        text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1, cv.CV_AA )
        text_coord = ( 5, 15 )
        text_color = cv.CV_RGB(255,255,255)
        text_coord2 = ( 5, 30 )
        text_coord3 = ( 5, 45 )

        ###############################
        ### Face detection stuff
        #haar_cascade = cv.Load( 'haarcascades/haarcascade_frontalface_default.xml' )
        #haar_cascade = cv.Load( 'C:/OpenCV2.2/data/haarcascades/haarcascade_frontalface_alt.xml' )
        #haar_cascade = cv.Load( 'haarcascades/haarcascade_frontalface_alt2.xml' )
        #haar_cascade = cv.Load( 'haarcascades/haarcascade_mcs_mouth.xml' )
        #haar_cascade = cv.Load( 'haarcascades/haarcascade_eye.xml' )
        #haar_cascade = cv.Load( 'haarcascades/haarcascade_frontalface_alt_tree.xml' )
        #haar_cascade = cv.Load( 'haarcascades/haarcascade_upperbody.xml' )
        #haar_cascade = cv.Load( 'haarcascades/haarcascade_profileface.xml' )


        # Set this to the max number of targets to look for (passed to k-means):
        max_targets = 20


        while True:


            # Capture frame from webcam
            camera_image = cv.QueryFrame( self.capture )
            #ret, frame = cap.read()

            frame_count += 1
            frame_t0 = time.time()
            mat = cv.GetMat(camera_image)
            Nmat = numpy.array(mat, dtype=numpy.uint8)


            # Create an image with interactive feedback:
            display_image = cv.CloneImage( camera_image )

            # NEW INSERT - FGMASK
            fgmask = fgbg.apply(Nmat,Nforemat,-1)
            fgmask = cv.fromarray(fgmask)

            # Create a working "color image" to modify / blur
            color_image = cv.CloneImage( display_image )


            # Smooth to get rid of false positives
            cv.Smooth( color_image, color_image, cv.CV_GAUSSIAN, 19, 0 ) #Changed from 19 AND MADE MEDIAN FILTER


            # Smooth to get rid of false positives

#            color_image = numpy.asarray( cv.GetMat( color_image ) )
#            (mu, sigma) = cv2.meanStdDev(color_image)
#            edges = cv2.Canny(color_image, mu - sigma, mu + sigma)
#            lines = cv2.HoughLines(edges, 1, pi / 180, 70)


            # Use the Running Average as the static background
            # a = 0.020 leaves artifacts lingering way too long.
            # a = 0.320 works well at 320x240, 15fps.  (1/a is roughly num frames.)
            cv.RunningAvg( color_image, running_average_image, gfilter, None )


            # Convert the scale of the moving average.
            cv.ConvertScale( running_average_image, running_average_in_display_color_depth, 1.0, 0.0 )


            # Subtract the current frame from the moving average.
            cv.AbsDiff( color_image, running_average_in_display_color_depth, difference )


            # Convert the image to greyscale.
            cv.CvtColor( difference, grey_image, cv.CV_RGB2GRAY )
            # Smooth Before thresholding
            cv.Smooth( grey_image, grey_image, cv.CV_GAUSSIAN, 19, 19 )
            # Threshold the image to a black and white motion mask:
            cv.Threshold( grey_image, grey_image, 2, 255, cv.CV_THRESH_BINARY )
            # Smooth and threshold again to eliminate "sparkles"
            #cv.Smooth( grey_image, grey_image, cv.CV_GAUSSIAN, 19, 0 )  #changed from 19 - AND put smooth before threshold
            cv.Threshold( grey_image, grey_image, 240, 255, cv.CV_THRESH_BINARY)


            grey_image_as_array = numpy.asarray( cv.GetMat( grey_image ) )
            non_black_coords_array = numpy.where( grey_image_as_array > 3 )
            # Convert from numpy.where()'s two separate lists to one list of (x, y) tuples:
            non_black_coords_array = zip( non_black_coords_array[1], non_black_coords_array[0] )

            frame_hsv = cv.CreateImage(cv.GetSize(color_image),8,3)
            cv.CvtColor(color_image,frame_hsv,cv.CV_BGR2HSV)
            imgthreshold_yellow=cv.CreateImage(cv.GetSize(color_image),8,1)
            imgthreshold_white=cv.CreateImage(cv.GetSize(color_image),8,1)
            imgthreshold_white2=cv.CreateImage(cv.GetSize(color_image),8,1)
            cv.InRangeS(frame_hsv,cv.Scalar(0,0,196),cv.Scalar(255,255,255),imgthreshold_white)  # changed scalar from 255,15,255 to 255,255,255
            cv.InRangeS(frame_hsv,cv.Scalar(41,43,224),cv.Scalar(255,255,255),imgthreshold_white2)
            cv.InRangeS(frame_hsv,cv.Scalar(20,100,100),cv.Scalar(30,255,255),imgthreshold_yellow)
            #cvCvtColor(color_image, yellowHSV, CV_BGR2HSV)
            #lower_yellow = np.array([10, 100, 100], dtype=np.uint8)
            #upper_yellow = np.array([30, 255, 255], dtype=np.uint8)
            #mask_yellow = cv2.inRange(yellowHSV, lower_yellow, upper_yellow)
            #res_yellow = cv2.bitwise_and(color_image, color_image, mask_yellow = mask_yellow)


            points = []   # Was using this to hold either pixel coords or polygon coords.
            bounding_box_list = []


            # Now calculate movements using the white pixels as "motion" data
            contour = cv.FindContours( grey_image, mem_storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE )

            i=0
            while contour:

#                c = contour[i]
#                m = cv2.moments(c)
#                Area  = m['m00']
#                print( Area )

                bounding_rect = cv.BoundingRect( list(contour) )
                point1 = ( bounding_rect[0], bounding_rect[1] )
                point2 = ( bounding_rect[0] + bounding_rect[2], bounding_rect[1] + bounding_rect[3] )


                bounding_box_list.append( ( point1, point2 ) )
                polygon_points = cv.ApproxPoly( list(contour), mem_storage, cv.CV_POLY_APPROX_DP )


                # To track polygon points only (instead of every pixel):
                #points += list(polygon_points)


                # Draw the contours:
                ###cv.DrawContours(color_image, contour, cv.CV_RGB(255,0,0), cv.CV_RGB(0,255,0), levels, 3, 0, (0,0) )
                cv.FillPoly( grey_image, [ list(polygon_points), ], cv.CV_RGB(255,255,255), 0, 0 )
                cv.PolyLine( display_image, [ polygon_points, ], 0, cv.CV_RGB(255,255,255), 1, 0, 0 )
                #cv.Rectangle( display_image, point1, point2, cv.CV_RGB(120,120,120), 1)
#        if Area > 3000:
#            cv2.drawContours(imgrgb,[cnt],0,(255,255,255),2)
#            print(Area)

                i=i+1
                contour = contour.h_next()




            # Find the average size of the bbox (targets), then
            # remove any tiny bboxes (which are prolly just noise).
            # "Tiny" is defined as any box with 1/10th the area of the average box.
            # This reduces false positives on tiny "sparkles" noise.
            box_areas = []
            for box in bounding_box_list:
                box_width = box[right][0] - box[left][0]
                box_height = box[bottom][0] - box[top][0]
                box_areas.append( box_width * box_height )


                #cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(255,0,0), 1)


            average_box_area = 0.0
            if len(box_areas): average_box_area = float( sum(box_areas) ) / len(box_areas)


            trimmed_box_list = []
            for box in bounding_box_list:
                box_width = box[right][0] - box[left][0]
                box_height = box[bottom][0] - box[top][0]


                # Only keep the box if it's not a tiny noise box:
                if (box_width * box_height) > average_box_area*0.1: trimmed_box_list.append( box )


            # Draw the trimmed box list:
            #for box in trimmed_box_list:
            #    cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(0,255,0), 2 )


            bounding_box_list = merge_collided_bboxes( trimmed_box_list )


            # Draw the merged box list:
            for box in bounding_box_list:
                cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(0,255,0), 1 )


            # Here are our estimate points to track, based on merged & trimmed boxes:
            estimated_target_count = len( bounding_box_list )


            # Don't allow target "jumps" from few to many or many to few.
            # Only change the number of targets up to one target per n seconds.
            # This fixes the "exploding number of targets" when something stops moving
            # and the motion erodes to disparate little puddles all over the place.


            if frame_t0 - last_target_change_t < .35:  # 1 change per 0.35 secs
                estimated_target_count = last_target_count
            else:
                if last_target_count - estimated_target_count > 1: estimated_target_count = last_target_count - 1
                if estimated_target_count - last_target_count > 1: estimated_target_count = last_target_count + 1
                last_target_change_t = frame_t0


            # Clip to the user-supplied maximum:
            estimated_target_count = min( estimated_target_count, max_targets )


            # The estimated_target_count at this point is the maximum number of targets
            # we want to look for.  If kmeans decides that one of our candidate
            # bboxes is not actually a target, we remove it from the target list below.


            # Using the numpy values directly (treating all pixels as points):
            points = non_black_coords_array
            center_points = []


            if len(points):


                # If we have all the "target_count" targets from last frame,
                # use the previously known targets (for greater accuracy).
                k_or_guess = max( estimated_target_count, 1 )  # Need at least one target to look for.
                if len(codebook) == estimated_target_count:
                    k_or_guess = codebook


                #points = vq.whiten(array( points ))  # Don't do this!  Ruins everything.
                codebook, distortion = vq.kmeans( array( points ), k_or_guess )


                # Convert to tuples (and draw it to screen)
                for center_point in codebook:
                    center_point = ( int(center_point[0]), int(center_point[1]) )
                    center_points.append( center_point )
                    #cv.Circle(display_image, center_point, 10, cv.CV_RGB(255, 0, 0), 2)
                    #cv.Circle(display_image, center_point, 5, cv.CV_RGB(255, 0, 0), 3)


            # Now we have targets that are NOT computed from bboxes -- just
            # movement weights (according to kmeans).  If any two targets are
            # within the same "bbox count", average them into a single target.
            #
            # (Any kmeans targets not within a bbox are also kept.)
            trimmed_center_points = []
            removed_center_points = []


            for box in bounding_box_list:
                # Find the centers within this box:
                center_points_in_box = []


                for center_point in center_points:
                    if    center_point[0] < box[right][0] and center_point[0] > box[left][0] and \
                        center_point[1] < box[bottom][1] and center_point[1] > box[top][1] :


                        # This point is within the box.
                        center_points_in_box.append( center_point )


                # Now see if there are more than one.  If so, merge them.
                if len( center_points_in_box ) > 1:
                    # Merge them:
                    x_list = y_list = []
                    for point in center_points_in_box:
                        x_list.append(point[0])
                        y_list.append(point[1])


                    average_x = int( float(sum( x_list )) / len( x_list ) )
                    average_y = int( float(sum( y_list )) / len( y_list ) )


                    trimmed_center_points.append( (average_x, average_y) )


                    # Record that they were removed:
                    removed_center_points += center_points_in_box


                if len( center_points_in_box ) == 1:
                    trimmed_center_points.append( center_points_in_box[0] ) # Just use it.


            # If there are any center_points not within a bbox, just use them.
            # (It's probably a cluster comprised of a bunch of small bboxes.)
            for center_point in center_points:
                if (not center_point in trimmed_center_points) and (not center_point in removed_center_points):
                    trimmed_center_points.append( center_point )


            # Draw what we found:
            #for center_point in trimmed_center_points:
            #    center_point = ( int(center_point[0]), int(center_point[1]) )
            #    cv.Circle(display_image, center_point, 20, cv.CV_RGB(255, 255,255), 1)
            #    cv.Circle(display_image, center_point, 15, cv.CV_RGB(100, 255, 255), 1)
            #    cv.Circle(display_image, center_point, 10, cv.CV_RGB(255, 255, 255), 2)
            #    cv.Circle(display_image, center_point, 5, cv.CV_RGB(100, 255, 255), 3)


            # Determine if there are any new (or lost) targets:
            actual_target_count = len( trimmed_center_points )
            last_target_count = actual_target_count


            # Now build the list of physical entities (objects)
            this_frame_entity_list = []


            # An entity is list: [ name, color, last_time_seen, last_known_coords ]


            for target in trimmed_center_points:


                # Is this a target near a prior entity (same physical entity)?
                entity_found = False
                entity_distance_dict = {}


                for entity in last_frame_entity_list:


                    entity_coords= entity[3]
                    delta_x = entity_coords[0] - target[0]
                    delta_y = entity_coords[1] - target[1]


                    distance = sqrt( pow(delta_x,2) + pow( delta_y,2) )
                    entity_distance_dict[ distance ] = entity


                # Did we find any non-claimed entities (nearest to furthest):
                distance_list = entity_distance_dict.keys()
                distance_list.sort()


                for distance in distance_list:


                    # Yes; see if we can claim the nearest one:
                    nearest_possible_entity = entity_distance_dict[ distance ]


                    # Don't consider entities that are already claimed:
                    if nearest_possible_entity in this_frame_entity_list:
                        #print "Target %s: Skipping the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3], nearest_possible_entity[1] ) #Commented Out 3/20/2016
                        continue


                    #print "Target %s pixel(b,g,r) : USING the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3] , nearest_possible_entity[1]) # Commented Out 3/20/2016
                    # Found the nearest entity to claim:
                    entity_found = True
                    nearest_possible_entity[2] = frame_t0  # Update last_time_seen
                    nearest_possible_entity[3] = target  # Update the new location
                    this_frame_entity_list.append( nearest_possible_entity )
                    #log_file.write( "%.3f MOVED %s %d %d\n" % ( frame_count, nearest_possible_entity[0], nearest_possible_entity[3][0], nearest_possible_entity[3][1]  ) )
                    break


                if entity_found == False:
                    # It's a new entity.
                    color = ( random.randint(0,255), random.randint(0,255), random.randint(0,255) )
                    name = hashlib.md5( str(frame_t0) + str(color) ).hexdigest()[:6]
                    last_time_seen = frame_t0
                    if imgthreshold_white[target[1],target[0]] == 0.0:
                        # It's a real detect (not a line marker)

                        new_entity = [ name, color, last_time_seen, target ]
                        this_frame_entity_list.append( new_entity )
                        log_file.write( "%.3f %.3f FOUND %s %d %d\n" % ( frame_count/fps, frame_count, new_entity[0], new_entity[3][0], new_entity[3][1]  ) )
                        filedrive = "C:/Users/525494/New_folder/000216/run_096/"
                        filename = "img"+str(name)
			#print "gfilter is: %.2f" + gfilter
                        cv.SaveImage("image-test%s-%3f.png"%(new_entity[0],gfilter), display_image)
                    elif imgthreshold_white[target[1],target[0]] == 255.0:
                        # It's a white line detect

                        new_entity = [ name, color, last_time_seen, target ]
                        this_frame_entity_list.append( new_entity )
                        log_file.write( "%.3f %.3f FOUND %s %d %d\n" % ( frame_count/fps, frame_count, new_entity[0], new_entity[3][0], new_entity[3][1]  ) )
                        filedrive = "C:/Users/525494/New_folder/000216/run_096/"
                        filename = "img"+str(name)
			#print "gfilter is: %.2f" + gfilter
                        cv.SaveImage("white-line-image-test%s-%3f.png"%(new_entity[0],gfilter), display_image)
                    elif imgthreshold_yellow[target[1],target[0]] == 255.0:
                        # It's a yellow line detect

                        new_entity = [ name, color, last_time_seen, target ]
                        this_frame_entity_list.append( new_entity )
                        log_file.write( "%.3f %.3f FOUND %s %d %d\n" % ( frame_count/fps, frame_count, new_entity[0], new_entity[3][0], new_entity[3][1]  ) )
                        filedrive = "C:/Users/525494/New_folder/000216/run_096/"
                        filename = "img"+str(name)
                        cv.SaveImage("yellow-line-image-test%s.png"%(new_entity[0]), camera_image)

            # Now "delete" any not-found entities which have expired:
            entity_ttl = 1.0  # 1 sec.


            for entity in last_frame_entity_list:
                last_time_seen = entity[2]
                if frame_t0 - last_time_seen > entity_ttl:
                    # It's gone.
                    #log_file.write( "%.3f STOPD %s %d %d\n" % ( frame_count, entity[0], entity[3][0], entity[3][1]  ) )
                    pass
                else:
                    # Save it for next time... not expired yet:
                    this_frame_entity_list.append( entity )


            # For next frame:
            last_frame_entity_list = this_frame_entity_list


            # Draw the found entities to screen:
            for entity in this_frame_entity_list:
                center_point = entity[3]
                c = entity[1]  # RGB color tuple
                cv.Circle(display_image, center_point, 20, cv.CV_RGB(c[0], c[1], c[2]), 1)
                cv.Circle(display_image, center_point, 15, cv.CV_RGB(c[0], c[1], c[2]), 1)
                cv.Circle(display_image, center_point, 10, cv.CV_RGB(c[0], c[1], c[2]), 2)
                cv.Circle(display_image, center_point,  5, cv.CV_RGB(c[0], c[1], c[2]), 3)




            #print "min_size is: " + str(min_size)
            # Listen for ESC or ENTER key
            c = cv.WaitKey(7) % 0x100
            if c == 27 or c == 10:
                break


            # Toggle which image to show
            if chr(c) == 'd':
                image_index = ( image_index + 1 ) % len( image_list )


            image_name = image_list[ image_index ]


            # Display frame to user
            if image_name == "camera":
                image = camera_image
                cv.PutText( image, "Camera (Normal)", text_coord, text_font, text_color )
            elif image_name == "shadow":
                image = fgmask
                cv.PutText( image, "No Shadow", text_coord, text_font, text_color )
            elif image_name == "white":
                #image = difference
                image = imgthreshold_white
                cv.PutText( image, "White Threshold", text_coord, text_font, text_color )
            elif image_name == "display":
                #image = display_image
                image = display_image
                cv.PutText( image, "Targets (w/AABBs and contours)", text_coord, text_font, text_color )
                cv.PutText( image, str(frame_t0), text_coord2, text_font, text_color )
                cv.PutText( image, str(frame_count), text_coord3, text_font, text_color )
            elif image_name == "threshold":
                # Convert the image to color.
                cv.CvtColor( grey_image, display_image, cv.CV_GRAY2RGB )
                image = display_image  # Re-use display image here
                cv.PutText( image, "Motion Mask", text_coord, text_font, text_color )
            elif image_name == "yellow":
                # Do face detection
                #detect_faces( camera_image, haar_cascade, mem_storage )
                image = imgthreshold_yellow  # Re-use camera image here
                cv.PutText( image, "Yellow Threshold", text_coord, text_font, text_color )


            #cv.ShowImage( "Target", image )		Commented out 3/19

#            self.writer.write( image )
#            out.write( image );
#            cv.WriteFrame( self.writer, image );
#            if self.writer:
#                cv.WriteFrame( self.writer, image );
#                video.write( image );

            log_file.flush()

            # If only using a camera, then there is no time.sleep() needed,
            # because the camera clips us to 15 fps.  But if reading from a file,
            # we need this to keep the time-based target clipping correct:
            frame_t1 = time.time()

            # If reading from a file, put in a forced delay:
#            if not self.writer:
#                delta_t = frame_t1 - frame_t0
#                if delta_t < ( 1.0 / 15.0 ): time.sleep( ( 1.0 / 15.0 ) - delta_t ):

            if frame_count == 155740:
                cv2.destroyWindow("Target")
#                    cv.ReleaseVideoWriter()
#                    self.writer.release()
#                    log_file.flush()
                break

        t1 = time.time()
        time_delta = t1 - t0
        processed_fps = float( frame_count ) / time_delta
        print "Got %d frames. %.1f s. %f fps." % ( frame_count, time_delta, processed_fps )
예제 #16
0
def getImageCol(file):
    img = cv.LoadImage(file, cv.CV_LOAD_IMAGE_GRAYSCALE)
    npArr = np.array(np.asarray(cv.GetMat(img)))
    npArr = np.ndarray.flatten(npArr)
    return np.reshape(npArr, (npArr.shape[0], 1))
예제 #17
0
            cv.WaitKey(0)
            break

        cv.Smooth(frame, frame, cv.CV_BLUR, 3)

        hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)

        cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)

        thr = cv.CreateImage(cv.GetSize(frame), 8, 1)

        #Change threshold value

        cv.InRangeS(hsv, (h1, s1, v1), (h2, s2, v2), thr)

        moments = cv.Moments(cv.GetMat(thr, 1), 0)

        area = cv.GetCentralMoment(moments, 0, 0)

        cv.Line(frame, (160, 0), (160, 240), (0, 0, 255), 3, 8, 0)

        if (area > 10000):

            x = cv.GetSpatialMoment(moments, 1, 0) / area
            y = cv.GetSpatialMoment(moments, 0, 1) / area

            #			overlay = cv.CreateImage(cv.GetSize(frame),8,3)

            cv.Circle(frame, (int(x), int(y)), 2, (255, 255, 255), 20)

            #			cv.Add(frame,overlay,frame)
예제 #18
0
    def run(self):
        # Initialize
        # log_file_name = "tracker_output.log"
        # log_file = file( log_file_name, 'a' )

        print "hello"

        frame = cv.QueryFrame(self.capture)
        frame_size = cv.GetSize(frame)

        # Capture the first frame from webcam for image properties
        display_image = cv.QueryFrame(self.capture)

        # Greyscale image, thresholded to create the motion mask:
        grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)

        # The RunningAvg() function requires a 32-bit or 64-bit image...
        running_average_image = cv.CreateImage(cv.GetSize(frame),
                                               cv.IPL_DEPTH_32F, 3)

        # ...but the AbsDiff() function requires matching image depths:
        running_average_in_display_color_depth = cv.CloneImage(display_image)

        # RAM used by FindContours():
        mem_storage = cv.CreateMemStorage(0)

        # The difference between the running average and the current frame:
        difference = cv.CloneImage(display_image)

        target_count = 1
        last_target_count = 1
        last_target_change_t = 0.0
        k_or_guess = 1
        codebook = []
        frame_count = 0
        last_frame_entity_list = []

        t0 = time.time()

        # For toggling display:
        image_list = ["camera", "difference", "threshold", "display", "faces"]
        image_index = 3  # Index into image_list

        # Prep for text drawing:
        text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1,
                                cv.CV_AA)
        text_coord = (5, 15)
        text_color = cv.CV_RGB(255, 255, 255)

        # Set this to the max number of targets to look for (passed to k-means):
        max_targets = 5

        while True:

            # Capture frame from webcam
            camera_image = cv.QueryFrame(self.capture)

            frame_count += 1
            frame_t0 = time.time()

            # Create an image with interactive feedback:
            display_image = cv.CloneImage(camera_image)

            # Create a working "color image" to modify / blur
            color_image = cv.CloneImage(display_image)

            # Smooth to get rid of false positives
            cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 19, 0)

            # Use the Running Average as the static background
            # a = 0.020 leaves artifacts lingering way too long.
            # a = 0.320 works well at 320x240, 15fps.  (1/a is roughly num frames.)
            cv.RunningAvg(color_image, running_average_image, 0.320, None)

            # Convert the scale of the moving average.
            cv.ConvertScale(running_average_image,
                            running_average_in_display_color_depth, 1.0, 0.0)

            # Subtract the current frame from the moving average.
            cv.AbsDiff(color_image, running_average_in_display_color_depth,
                       difference)

            cv.ShowImage("difference ", difference)

            # Convert the image to greyscale.
            cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY)

            # Threshold the image to a black and white motion mask:
            cv.Threshold(grey_image, grey_image, 2, 255, cv.CV_THRESH_BINARY)
            # Smooth and threshold again to eliminate "sparkles"
            cv.Smooth(grey_image, grey_image, cv.CV_GAUSSIAN, 19, 0)
            cv.Threshold(grey_image, grey_image, 240, 255, cv.CV_THRESH_BINARY)

            grey_image_as_array = numpy.asarray(cv.GetMat(grey_image))
            non_black_coords_array = numpy.where(grey_image_as_array > 3)
            # Convert from numpy.where()'s two separate lists to one list of (x, y) tuples:
            non_black_coords_array = zip(non_black_coords_array[1],
                                         non_black_coords_array[0])

            #             cv.SegmentMotion(non_black_coords_array, None, storage, timestamp, seg_thresh)

            # print "min_size is: " + str(min_size)
            # Listen for ESC or ENTER key
            c = cv.WaitKey(7) % 0x100
            if c == 27 or c == 10:
                break

            # Toggle which image to show


#             if chr(c) == 'd':
#                 image_index = ( image_index + 1 ) % len( image_list )
#
#             image_name = image_list[ image_index ]
#
#             # Display frame to user
#             if image_name == "camera":
#                 image = camera_image
#                 cv.PutText( image, "Camera (Normal)", text_coord, text_font, text_color )
#             elif image_name == "difference":
#                 image = difference
#                 cv.PutText( image, "Difference Image", text_coord, text_font, text_color )
#             elif image_name == "display":
#                 image = display_image
#                 cv.PutText( image, "Targets (w/AABBs and contours)", text_coord, text_font, text_color )
#             elif image_name == "threshold":
#                 # Convert the image to color.
#                 cv.CvtColor( grey_image, display_image, cv.CV_GRAY2RGB )
#                 image = display_image  # Re-use display image here
#                 cv.PutText( image, "Motion Mask", text_coord, text_font, text_color )
#             elif image_name == "faces":
#                 # Do face detection
#                 detect_faces( camera_image, haar_cascade, mem_storage )
#                 image = camera_image  # Re-use camera image here
#                 cv.PutText( image, "Face Detection", text_coord, text_font, text_color )
#             cv.ShowImage( "Target", image )

            image1 = display_image

            cv.ShowImage("Target 1", image1)

            #             if self.writer:
            #                 cv.WriteFrame( self.writer, image );

            # log_file.flush()

            # If only using a camera, then there is no time.sleep() needed,
            # because the camera clips us to 15 fps.  But if reading from a file,
            # we need this to keep the time-based target clipping correct:
            frame_t1 = time.time()

            # If reading from a file, put in a forced delay:
            if not self.writer:
                delta_t = frame_t1 - frame_t0
                if delta_t < (1.0 / 15.0): time.sleep((1.0 / 15.0) - delta_t)

        t1 = time.time()
        time_delta = t1 - t0
        processed_fps = float(frame_count) / time_delta
        print "Got %d frames. %.1f s. %f fps." % (frame_count, time_delta,
                                                  processed_fps)
예제 #19
0
    def run(self):
        # Initialize
        # log_file_name = "tracker_output.log"
        # log_file = file( log_file_name, 'a' )

        print "hello"

        frame = cv.QueryFrame(self.capture)
        frame_size = cv.GetSize(frame)

        # Capture the first frame from webcam for image properties
        display_image = cv.QueryFrame(self.capture)

        # Greyscale image, thresholded to create the motion mask:
        grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)

        # The RunningAvg() function requires a 32-bit or 64-bit image...
        running_average_image = cv.CreateImage(cv.GetSize(frame),
                                               cv.IPL_DEPTH_32F, 3)

        # ...but the AbsDiff() function requires matching image depths:
        running_average_in_display_color_depth = cv.CloneImage(display_image)

        # RAM used by FindContours():
        mem_storage = cv.CreateMemStorage(0)

        # The difference between the running average and the current frame:
        difference = cv.CloneImage(display_image)

        target_count = 1
        last_target_count = 1
        last_target_change_t = 0.0
        k_or_guess = 1
        codebook = []
        frame_count = 0
        last_frame_entity_list = []

        t0 = time.time()

        # For toggling display:
        image_list = ["camera", "difference", "threshold", "display", "faces"]
        image_index = 3  # Index into image_list

        # Prep for text drawing:
        text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1,
                                cv.CV_AA)
        text_coord = (5, 15)
        text_color = cv.CV_RGB(255, 255, 255)

        # Set this to the max number of targets to look for (passed to k-means):
        max_targets = 5

        while True:

            # Capture frame from webcam
            camera_image = cv.QueryFrame(self.capture)

            frame_count += 1
            frame_t0 = time.time()

            # Create an image with interactive feedback:
            display_image = cv.CloneImage(camera_image)

            # Create a working "color image" to modify / blur
            color_image = cv.CloneImage(display_image)

            # Smooth to get rid of false positives
            cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 19, 0)

            # Use the Running Average as the static background
            # a = 0.020 leaves artifacts lingering way too long.
            # a = 0.320 works well at 320x240, 15fps.  (1/a is roughly num frames.)
            cv.RunningAvg(color_image, running_average_image, 0.320, None)

            #             cv.ShowImage("background ", running_average_image)

            # Convert the scale of the moving average.
            cv.ConvertScale(running_average_image,
                            running_average_in_display_color_depth, 1.0, 0.0)

            # Subtract the current frame from the moving average.
            cv.AbsDiff(color_image, running_average_in_display_color_depth,
                       difference)

            cv.ShowImage("difference ", difference)

            # Convert the image to greyscale.
            cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY)

            # Threshold the image to a black and white motion mask:
            cv.Threshold(grey_image, grey_image, 2, 255, cv.CV_THRESH_BINARY)
            # Smooth and threshold again to eliminate "sparkles"
            cv.Smooth(grey_image, grey_image, cv.CV_GAUSSIAN, 19, 0)

            cv.Threshold(grey_image, grey_image, 240, 255, cv.CV_THRESH_BINARY)

            grey_image_as_array = numpy.asarray(cv.GetMat(grey_image))
            non_black_coords_array = numpy.where(grey_image_as_array > 3)
            # Convert from numpy.where()'s two separate lists to one list of (x, y) tuples:
            non_black_coords_array = zip(non_black_coords_array[1],
                                         non_black_coords_array[0])

            points = [
            ]  # Was using this to hold either pixel coords or polygon coords.
            bounding_box_list = []

            # Now calculate movements using the white pixels as "motion" data
            contour = cv.FindContours(grey_image, mem_storage,
                                      cv.CV_RETR_CCOMP,
                                      cv.CV_CHAIN_APPROX_SIMPLE)

            levels = 10
            while contour:

                bounding_rect = cv.BoundingRect(list(contour))
                point1 = (bounding_rect[0], bounding_rect[1])
                point2 = (bounding_rect[0] + bounding_rect[2],
                          bounding_rect[1] + bounding_rect[3])

                bounding_box_list.append((point1, point2))
                polygon_points = cv.ApproxPoly(list(contour), mem_storage,
                                               cv.CV_POLY_APPROX_DP)

                # To track polygon points only (instead of every pixel):
                # points += list(polygon_points)

                # Draw the contours:
                cv.DrawContours(color_image, contour, cv.CV_RGB(255, 0, 0),
                                cv.CV_RGB(0, 255, 0), levels, 3, 0, (0, 0))
                cv.FillPoly(grey_image, [
                    list(polygon_points),
                ], cv.CV_RGB(255, 255, 255), 0, 0)
                cv.PolyLine(display_image, [
                    polygon_points,
                ], 0, cv.CV_RGB(255, 255, 255), 1, 0, 0)
                # cv.Rectangle( display_image, point1, point2, cv.CV_RGB(120,120,120), 1)

                contour = contour.h_next()

            # Find the average size of the bbox (targets), then
            # remove any tiny bboxes (which are prolly just noise).
            # "Tiny" is defined as any box with 1/10th the area of the average box.
            # This reduces false positives on tiny "sparkles" noise.
            box_areas = []
            for box in bounding_box_list:
                box_width = box[right][0] - box[left][0]
                box_height = box[bottom][0] - box[top][0]
                box_areas.append(box_width * box_height)

                # cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(255,0,0), 1)

            average_box_area = 0.0
            if len(box_areas):
                average_box_area = float(sum(box_areas)) / len(box_areas)

            trimmed_box_list = []
            for box in bounding_box_list:
                box_width = box[right][0] - box[left][0]
                box_height = box[bottom][0] - box[top][0]

                # Only keep the box if it's not a tiny noise box:
                if (box_width * box_height) > average_box_area * 0.1:
                    trimmed_box_list.append(box)

            # Draw the trimmed box list:
            # for box in trimmed_box_list:
            #    cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(0,255,0), 2 )

            bounding_box_list = merge_collided_bboxes(trimmed_box_list)

            # Draw the merged box list:
            for box in bounding_box_list:
                cv.Rectangle(display_image, box[0], box[1],
                             cv.CV_RGB(0, 255, 0), 1)

            # Here are our estimate points to track, based on merged & trimmed boxes:
            estimated_target_count = len(bounding_box_list)

            # Don't allow target "jumps" from few to many or many to few.
            # Only change the number of targets up to one target per n seconds.
            # This fixes the "exploding number of targets" when something stops moving
            # and the motion erodes to disparate little puddles all over the place.

            if frame_t0 - last_target_change_t < .350:  # 1 change per 0.35 secs
                estimated_target_count = last_target_count
            else:
                if last_target_count - estimated_target_count > 1:
                    estimated_target_count = last_target_count - 1
                if estimated_target_count - last_target_count > 1:
                    estimated_target_count = last_target_count + 1
                last_target_change_t = frame_t0

            # Clip to the user-supplied maximum:
            estimated_target_count = min(estimated_target_count, max_targets)

            # The estimated_target_count at this point is the maximum number of targets
            # we want to look for.  If kmeans decides that one of our candidate
            # bboxes is not actually a target, we remove it from the target list below.

            # Using the numpy values directly (treating all pixels as points):
            points = non_black_coords_array
            center_points = []

            if len(points):

                # If we have all the "target_count" targets from last frame,
                # use the previously known targets (for greater accuracy).
                k_or_guess = max(estimated_target_count,
                                 1)  # Need at least one target to look for.
                if len(codebook) == estimated_target_count:
                    k_or_guess = codebook

                # points = vq.whiten(array( points ))  # Don't do this!  Ruins everything.
                codebook, distortion = vq.kmeans(array(points), k_or_guess)

                # Convert to tuples (and draw it to screen)
                for center_point in codebook:
                    center_point = (int(center_point[0]), int(center_point[1]))
                    center_points.append(center_point)
                    # cv.Circle(display_image, center_point, 10, cv.CV_RGB(255, 0, 0), 2)
                    # cv.Circle(display_image, center_point, 5, cv.CV_RGB(255, 0, 0), 3)

            # Now we have targets that are NOT computed from bboxes -- just
            # movement weights (according to kmeans).  If any two targets are
            # within the same "bbox count", average them into a single target.
            #
            # (Any kmeans targets not within a bbox are also kept.)
            trimmed_center_points = []
            removed_center_points = []

            for box in bounding_box_list:
                # Find the centers within this box:
                center_points_in_box = []

                for center_point in center_points:
                    if    center_point[0] < box[right][0] and center_point[0] > box[left][0] and \
                        center_point[1] < box[bottom][1] and center_point[1] > box[top][1] :

                        # This point is within the box.
                        center_points_in_box.append(center_point)

                # Now see if there are more than one.  If so, merge them.
                if len(center_points_in_box) > 1:
                    # Merge them:
                    x_list = y_list = []
                    for point in center_points_in_box:
                        x_list.append(point[0])
                        y_list.append(point[1])

                    average_x = int(float(sum(x_list)) / len(x_list))
                    average_y = int(float(sum(y_list)) / len(y_list))

                    trimmed_center_points.append((average_x, average_y))

                    # Record that they were removed:
                    removed_center_points += center_points_in_box

                if len(center_points_in_box) == 1:
                    trimmed_center_points.append(
                        center_points_in_box[0])  # Just use it.

            # If there are any center_points not within a bbox, just use them.
            # (It's probably a cluster comprised of a bunch of small bboxes.)
            for center_point in center_points:
                if (not center_point in trimmed_center_points) and (
                        not center_point in removed_center_points):
                    trimmed_center_points.append(center_point)

            # Draw what we found:
            # for center_point in trimmed_center_points:
            #    center_point = ( int(center_point[0]), int(center_point[1]) )
            #    cv.Circle(display_image, center_point, 20, cv.CV_RGB(255, 255,255), 1)
            #    cv.Circle(display_image, center_point, 15, cv.CV_RGB(100, 255, 255), 1)
            #    cv.Circle(display_image, center_point, 10, cv.CV_RGB(255, 255, 255), 2)
            #    cv.Circle(display_image, center_point, 5, cv.CV_RGB(100, 255, 255), 3)

            # Determine if there are any new (or lost) targets:
            actual_target_count = len(trimmed_center_points)
            last_target_count = actual_target_count

            # Now build the list of physical entities (objects)
            this_frame_entity_list = []

            # An entity is list: [ name, color, last_time_seen, last_known_coords ]

            for target in trimmed_center_points:

                # Is this a target near a prior entity (same physical entity)?
                entity_found = False
                entity_distance_dict = {}

                for entity in last_frame_entity_list:

                    entity_coords = entity[3]
                    delta_x = entity_coords[0] - target[0]
                    delta_y = entity_coords[1] - target[1]

                    distance = sqrt(pow(delta_x, 2) + pow(delta_y, 2))
                    entity_distance_dict[distance] = entity

                # Did we find any non-claimed entities (nearest to furthest):
                distance_list = entity_distance_dict.keys()
                distance_list.sort()

                for distance in distance_list:

                    # Yes; see if we can claim the nearest one:
                    nearest_possible_entity = entity_distance_dict[distance]

                    # Don't consider entities that are already claimed:
                    if nearest_possible_entity in this_frame_entity_list:
                        # print "Target %s: Skipping the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3], nearest_possible_entity[1] )
                        continue

                    # print "Target %s: USING the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3] , nearest_possible_entity[1])
                    # Found the nearest entity to claim:
                    entity_found = True
                    nearest_possible_entity[
                        2] = frame_t0  # Update last_time_seen
                    nearest_possible_entity[
                        3] = target  # Update the new location
                    this_frame_entity_list.append(nearest_possible_entity)
                    # log_file.write( "%.3f MOVED %s %d %d\n" % ( frame_t0, nearest_possible_entity[0], nearest_possible_entity[3][0], nearest_possible_entity[3][1]  ) )
                    break

                if entity_found == False:
                    # It's a new entity.
                    color = (random.randint(0, 255), random.randint(0, 255),
                             random.randint(0, 255))
                    name = hashlib.md5(str(frame_t0) +
                                       str(color)).hexdigest()[:6]
                    last_time_seen = frame_t0

                    new_entity = [name, color, last_time_seen, target]
                    this_frame_entity_list.append(new_entity)
                    # log_file.write( "%.3f FOUND %s %d %d\n" % ( frame_t0, new_entity[0], new_entity[3][0], new_entity[3][1]  ) )

            # Now "delete" any not-found entities which have expired:
            entity_ttl = 1.0  # 1 sec.

            for entity in last_frame_entity_list:
                last_time_seen = entity[2]
                if frame_t0 - last_time_seen > entity_ttl:
                    # It's gone.
                    # log_file.write( "%.3f STOPD %s %d %d\n" % ( frame_t0, entity[0], entity[3][0], entity[3][1]  ) )
                    pass
                else:
                    # Save it for next time... not expired yet:
                    this_frame_entity_list.append(entity)

            # For next frame:
            last_frame_entity_list = this_frame_entity_list

            # Draw the found entities to screen:
            for entity in this_frame_entity_list:
                center_point = entity[3]
                c = entity[1]  # RGB color tuple
                cv.Circle(display_image, center_point, 20,
                          cv.CV_RGB(c[0], c[1], c[2]), 1)
                cv.Circle(display_image, center_point, 15,
                          cv.CV_RGB(c[0], c[1], c[2]), 1)
                cv.Circle(display_image, center_point, 10,
                          cv.CV_RGB(c[0], c[1], c[2]), 2)
                cv.Circle(display_image, center_point, 5,
                          cv.CV_RGB(c[0], c[1], c[2]), 3)

            # print "min_size is: " + str(min_size)
            # Listen for ESC or ENTER key
            c = cv.WaitKey(7) % 0x100
            if c == 27 or c == 10:
                break

            # Toggle which image to show


#             if chr(c) == 'd':
#                 image_index = ( image_index + 1 ) % len( image_list )
#
#             image_name = image_list[ image_index ]
#
#             # Display frame to user
#             if image_name == "camera":
#                 image = camera_image
#                 cv.PutText( image, "Camera (Normal)", text_coord, text_font, text_color )
#             elif image_name == "difference":
#                 image = difference
#                 cv.PutText( image, "Difference Image", text_coord, text_font, text_color )
#             elif image_name == "display":
#                 image = display_image
#                 cv.PutText( image, "Targets (w/AABBs and contours)", text_coord, text_font, text_color )
#             elif image_name == "threshold":
#                 # Convert the image to color.
#                 cv.CvtColor( grey_image, display_image, cv.CV_GRAY2RGB )
#                 image = display_image  # Re-use display image here
#                 cv.PutText( image, "Motion Mask", text_coord, text_font, text_color )
#             elif image_name == "faces":
#                 # Do face detection
#                 detect_faces( camera_image, haar_cascade, mem_storage )
#                 image = camera_image  # Re-use camera image here
#                 cv.PutText( image, "Face Detection", text_coord, text_font, text_color )
#             cv.ShowImage( "Target", image )

            image1 = display_image

            cv.ShowImage("Target 1", image1)

            #             if self.writer:
            #                 cv.WriteFrame( self.writer, image );

            # log_file.flush()

            # If only using a camera, then there is no time.sleep() needed,
            # because the camera clips us to 15 fps.  But if reading from a file,
            # we need this to keep the time-based target clipping correct:
            frame_t1 = time.time()

            # If reading from a file, put in a forced delay:
            if not self.writer:
                delta_t = frame_t1 - frame_t0
                if delta_t < (1.0 / 15.0): time.sleep((1.0 / 15.0) - delta_t)

        t1 = time.time()
        time_delta = t1 - t0
        processed_fps = float(frame_count) / time_delta
        print "Got %d frames. %.1f s. %f fps." % (frame_count, time_delta,
                                                  processed_fps)
예제 #20
0
        cv.ShowImage("image", self.dst_image)

        cv.CalcArrHist([self.dst_image], self.hist)
        (min_value, max_value, _, _) = cv.GetMinMaxHistValue(self.hist)
        cv.Scale(self.hist.bins, self.hist.bins, float(self.hist_image.height) / max_value, 0)

        cv.Set(self.hist_image, cv.ScalarAll(255))
        bin_w = round(float(self.hist_image.width) / hist_size)

        for i in range(hist_size):
            cv.Rectangle(self.hist_image, (int(i * bin_w), self.hist_image.height),
                         (int((i + 1) * bin_w), self.hist_image.height - cv.Round(self.hist.bins[i])),
                         cv.ScalarAll(0), -1, 8, 0)
       
        cv.ShowImage("histogram", self.hist_image)

if __name__ == "__main__":
    # Load the source image.
    if len(sys.argv) > 1:
        src_image = cv.GetMat(cv.LoadImage(sys.argv[1], 0))
    else:
        url = 'https://code.ros.org/svn/opencv/trunk/opencv/samples/c/baboon.jpg'
        filedata = urllib2.urlopen(url).read()
        imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
        cv.SetData(imagefiledata, filedata, len(filedata))
        src_image = cv.DecodeImageM(imagefiledata, 0)

    dh = DemHist(src_image)

    cv.WaitKey(0)
예제 #21
0
    def findBrightObjects(self):
        
        cv.NamedWindow("camera")

        while True :
            frame = cv.QueryFrame(self.video1)
        #     print type(frame)
            [rows, cols] = cv.GetSize(frame)
            
#             image = cv.CreateMat(rows, cols, cv.CV_8UC3)
            image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, frame.nChannels)        
            cv.Copy(frame, image)
#             image = cv.GetMat(frame)
            cv.ShowImage("camera", image)
            
           
#             grayScaleFullImage = cv.CreateImage((image.width, image.height), 8, 1)
#             cv.CvtColor(image, grayScaleFullImage, cv.CV_BGR2GRAY)
            
#             convert to hsv
            hsvImage = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, frame.nChannels)  
            cv.CvtColor(image, hsvImage, cv.CV_BGR2HSV)
            cv.ShowImage("hsv", hsvImage)
           
#             hsvImage = cv2.cvtColor(imageArray, cv.CV_BGR2HSV)

#             h_plane = cv.CreateImage(cv.GetSize(image), 8, 1)
#             s_plane = cv.CreateImage(cv.GetSize(image), 8, 1)
#             v_plane = cv.CreateImage(cv.GetSize(image), 8, 1)
            
#           Split HSV into two of it's three channels. V channel is same as greyscale image so ignore.
#             cv.Split(hsvImage, h_plane, s_plane, v_plane, None)
#             http://robbierickman.blogspot.co.uk/2011/11/laserduckpy-coloured-object-tracking.html
            
            
#             planes = [h_plane, s_plane]
# 
#             h_bins = 30
#             s_bins = 32
#             hist_size = [h_bins, s_bins]
#             # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
#             h_ranges = [0, 180]
#             # saturation varies from 0 (black-gray-white) to
#             # 255 (pure spectrum color)
#             s_ranges = [0, 255]
#             ranges = [h_ranges, s_ranges]
#             scale = 10
#             hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
#             cv.CalcHist([cv.GetImage(i) for i in planes], hist)
#             (_, max_value, _, _) = cv.GetMinMaxHistValue(hist)
#         
#             hist_img = cv.CreateImage((h_bins*scale, s_bins*scale), 8, 3)
#         
#             for h in range(h_bins):
#                 for s in range(s_bins):
#                     bin_val = cv.QueryHistValue_2D(hist, h, s)
#                     intensity = cv.Round(bin_val * 255 / max_value)
#                     cv.Rectangle(hist_img,
#                                  (h*scale, s*scale),
#                                  ((h+1)*scale - 1, (s+1)*scale - 1),
#                                  cv.RGB(intensity, intensity, intensity),
#                                  cv.CV_FILLED)
            
            # http://uvhar.googlecode.com/hg/test/laser_tracker.py
            # Threshold ranges of HSV components.
            
#             cv.InRangeS(h_plane, hmin, hmax, h_plane)
# #             cv.InRangeS(s_plane, smin, smax, s_plane)
# #             cv.InRangeS(v_plane, vmin, vmax, v_plane)
#             
#             finalImage = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)
#     
#             # Perform an AND on HSV components to identify the laser!
#             cv.And(h_plane, s_plane, finalImage)
#             # This actually Worked OK for me without using Saturation.
#             # cv.cvAnd(laser_img, s_img,laser_img) 
#     
#             # Merge the HSV components back together.
#             cv.Merge(h_plane, s_plane, v_plane, None, hsvImage)
            
   
#             cv.ShowImage("hue", h_plane)
#             cv.ShowImage("saturation", s_plane)
#             cv.ShowImage("value", v_plane)
            
            thresholdImage = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)
            cv.InRangeS(hsvImage, cv.Scalar(0, 100, 100), cv.Scalar(50, 255, 255), thresholdImage)
            
#             thresholdImage = cv2.threshold(hsvImage, [0, 100, 100], [50, 255, 255], cv2.THRESH_BINARY)
            cv.ShowImage("threshold image", thresholdImage)
            
            # remove noise from threshold image
            kernel = cv.CreateStructuringElementEx(9, 9, 5, 5, cv.CV_SHAPE_CROSS) 
             
            # Dilate- replaces pixel value with highest value pixel in kernel
            cv.Dilate(thresholdImage, thresholdImage, kernel, 2)
             
            # Erode- replaces pixel value with lowest value pixel in kernel
            cv.Erode(thresholdImage, thresholdImage, kernel, 2)

#             element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
#             cv.Dilate(thresholdImage, element, thresholdImage)
#             cv2.erode(thresholdImage, element, thresholdImage)
            
            cv.ShowImage("cleaned image ", thresholdImage)
            
            # contour detection
            imageArray = np.asarray(cv.GetMat(thresholdImage), np.uint8, 1)
            print type(imageArray)
            imageArray = imageArray.T
            
            
            
#             contours, hier = cv2.findContours(imageArray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)   
#             print "TYPE " + str(type(contours)) + " " + str(len(contours))
#              
# #             for i in contours:
# #                 print i
#             maxArea = -1
#             contourIndex = -1
#             if contours: 
#                 for i in range(0, len(contours)):
#                     cnt = contours[i].astype('int')
#                     print type(cnt)
#                     area = cv2.contourArea(cnt)
#                     print area
#                     if area > maxArea:
#                         maxArea = area
#                         contourIndex = i
#                          
#             if contourIndex != -1:
#                 cv2.drawContours(imageArray, contours, contourIndex, (0, 0 , 255), 10)

            
            params = cv2.SimpleBlobDetector_Params()
#             params.minDistBetweenBlobs = 1.0  # minimum 10 pixels between blobs
#             params.filterByArea = True        # filter my blobs by area of blob
#             params.minArea = 5.0             # min 20 pixels squared
#             params.maxArea = 500.0            # max 500 pixels squared
            params.minDistBetweenBlobs = 50.0
            params.filterByInertia = False
            params.filterByConvexity = False
            params.filterByColor = False
            params.filterByCircularity = False
            params.filterByArea = True
            params.minArea = 20.0
            params.maxArea = 500.0
            
            myBlobDetector = cv2.SimpleBlobDetector(params)
            keypoints = myBlobDetector.detect(imageArray)
            print "blobs " + str(keypoints)
            
            # extract the x y coordinates of the keypoints: 

            for i in range(0, len(keypoints) - 1):
                print keypoints[i].pt
                pt1 = (int(keypoints[i].pt[0]), int(keypoints[i].pt[1]))
                pt2 = (int(keypoints[i + 1].pt[0]), int(keypoints[i + 1].pt[1]))
                cv2.line(imageArray, pt1, pt2, (255, 0, 0))
#                 float X=keypoints[i].pt.x; 
#                 float Y=keypoints[i].pt.y;
            
                     
            cv2.imshow("final detection ", imageArray)
#             
            
            if cv.WaitKey(10) == 27:
                break 
예제 #22
0
def transmit_thread():
    '''thread for image transmit to GCS'''
    state = mpstate.camera_state

    tx_count = 0
    skip_count = 0
    bsend = block_xmit.BlockSender(0, bandwidth=state.settings.bandwidth, debug=False)
    state.bsocket = MavSocket(mpstate.mav_master[0])
    state.bsend2 = block_xmit.BlockSender(mss=96, sock=state.bsocket, dest_ip='mavlink', dest_port=0, backlog=5, debug=False)
    state.bsend2.set_bandwidth(state.settings.bandwidth2)

    while not state.unload.wait(0.02):
        bsend.tick(packet_count=1000, max_queue=state.settings.maxqueue1)
        state.bsend2.tick(packet_count=1000, max_queue=state.settings.maxqueue2)
        check_commands()
        if state.transmit_queue.empty():
            continue

        (frame_time, regions, im_full, im_640) = state.transmit_queue.get()
        if state.settings.roll_stabilised:
            roll=0
        else:
            roll=None
        pos = get_plane_position(frame_time, roll=roll)

        # this adds the latlon field to the regions
        log_joe_position(pos, frame_time, regions)

        # filter out any regions outside the boundary
        if state.boundary_polygon:
            regions = cuav_region.filter_boundary(regions, state.boundary_polygon, pos)
            regions = cuav_region.filter_regions(im_full, regions, min_score=state.settings.minscore)

        state.xmit_queue = bsend.sendq_size()
        state.xmit_queue2 = state.bsend2.sendq_size()
        state.efficiency = bsend.get_efficiency()
        state.bandwidth_used = bsend.get_bandwidth_used()
        state.rtt_estimate = bsend.get_rtt_estimate()

        jpeg = None

        if len(regions) > 0:
            lowscore = 0
            highscore = 0
            for r in regions:
                lowscore = min(lowscore, r.score)
                highscore = max(highscore, r.score)
                
            if state.settings.transmit:
                # send a region message with thumbnails to the ground station
                thumb = None
                if state.settings.send1:
                    thumb_img = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)),
                                                               regions,
                                                               thumb_size=state.settings.thumbsize)
                    thumb = scanner.jpeg_compress(numpy.ascontiguousarray(cv.GetMat(thumb_img)), state.settings.quality)

                    pkt = ThumbPacket(frame_time, regions, thumb, state.frame_loss, state.xmit_queue, pos)

                    buf = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL)
                    bsend.set_bandwidth(state.settings.bandwidth)
                    bsend.set_packet_loss(state.settings.packet_loss)
                    bsend.send(buf,
                               dest=(state.settings.gcs_address, state.settings.gcs_view_port),
                               priority=1)
                # also send thumbnails via 900MHz telemetry
                if state.settings.send2 and highscore >= state.settings.minscore2:
                    if thumb is None or lowscore < state.settings.minscore2:
                        # remove some of the regions
                        regions = cuav_region.filter_regions(im_full, regions, min_score=state.settings.minscore2)
                        thumb_img = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)),
                                                                   regions,
                                                                   thumb_size=state.settings.thumbsize)
                        thumb = scanner.jpeg_compress(numpy.ascontiguousarray(cv.GetMat(thumb_img)), state.settings.quality)
                        pkt = ThumbPacket(frame_time, regions, thumb, state.frame_loss, state.xmit_queue, pos)

                        buf = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL)
                    state.bsend2.set_bandwidth(state.settings.bandwidth2)
                    state.bsend2.send(buf, priority=highscore)

        # Base how many images we send on the send queue size
        send_frequency = state.xmit_queue // 3
        if send_frequency == 0 or (tx_count+skip_count) % send_frequency == 0:
            jpeg = scanner.jpeg_compress(im_640, state.settings.quality)

        if jpeg is None:
            skip_count += 1
            continue

        # keep filtered image size
        state.jpeg_size = 0.95 * state.jpeg_size + 0.05 * len(jpeg)
        
        tx_count += 1

        if state.settings.gcs_address is None:
            continue
        bsend.set_packet_loss(state.settings.packet_loss)
        bsend.set_bandwidth(state.settings.bandwidth)
        pkt = ImagePacket(frame_time, jpeg, state.xmit_queue, pos)
        str = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL)
        bsend.send(str,
                   dest=(state.settings.gcs_address, state.settings.gcs_view_port))
예제 #23
0
    grayIPimage = cv.GetImage(cv.fromarray(gray))
    sobel  = cv.CreateImage((w, h),cv2.IPL_DEPTH_16S, 1)  #创建一张深度为16位有符号(-65536~65535)的的图像区域保持处理结果
    cv.Sobel(grayIPimage,sobel,2,0,7)         # 进行x方向的sobel检测
    temp  = cv.CreateImage(cv.GetSize(sobel),cv2.IPL_DEPTH_8U, 1)       #图像格式转换回8位深度已进行下一步处理
    cv.ConvertScale(sobel, temp,0.00390625, 0)
    cv.Threshold(temp, temp, 0, 255, cv2.THRESH_OTSU)
    kernal = cv.CreateStructuringElementEx(3,1, 1, 0, 0)
    cv.Dilate(temp, temp,kernal,2)
    cv.Erode(temp, temp,kernal,4)
    cv.Dilate(temp, temp,kernal,2)
#     cv.ShowImage('1', temp)
    kernal = cv.CreateStructuringElementEx(1,3, 0, 1, 0)
    cv.Erode(temp, temp,kernal,1)
    cv.Dilate(temp, temp,kernal,3)
#     cv.ShowImage('2', temp)
    temp = np.asarray(cv.GetMat(temp))
    contours, heirs  = cv2.findContours(temp,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    
    for tours in contours:
        rc = cv2.boundingRect(tours)
        if rc[2]/rc[3] >= 2:
            #rc[0] 表示图像左上角的纵坐标,rc[1] 表示图像左上角的横坐标,rc[2] 表示图像的宽度,rc[3] 表示图像的高度,
            cv2.rectangle(image, (rc[0],rc[1]),(rc[0]+rc[2],rc[1]+rc[3]),(255,0,255))
            imageIp = cv.GetImage(cv.fromarray(image))
            cv.SetImageROI(imageIp, rc)
            imageCopy = cv.CreateImage((rc[2], rc[3]),cv2.IPL_DEPTH_8U, 3)
            cv.Copy(imageIp, imageCopy)
            cv.ResetImageROI(imageIp)
            cv.SaveImage('D:/pic/result/' + str(i) + '.jpg',imageCopy)
            i = i +1
# cv2.imshow("黑底白字",image)      
    def run(self):
        initialTime = 0.  #sets the initial time
        num_Frames = int(
            cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_COUNT))
        fps = cv.GetCaptureProperty(self.capture, cv.CV_CAP_PROP_FPS)

        for ii in range(num_Frames - 8):

            print('Frame: ' + str(ii) + ' of ' + str(num_Frames))
            # read the ii-th frame
            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
            # It's 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 the	is 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)

            # uncomment below for tracking blue
            #             cv.InRangeS(hsv_img, (112, 50, 50), (118, 200, 200), thresholded_img)

            # tracking red
            cv.InRangeS(hsv_img, (160, 150, 100), (180, 255, 255),
                        thresholded_img)

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

            # there can be noise in the video so ignore objects with small areas
            if (area > 2500):
                #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

                elapsedTime = ii / fps

                f.write(
                    str(elapsedTime) + ',' + '%013.9f' % x + ',' +
                    '%013.9f' % y + "\n"
                )  #prints output to the specified output file for later use


#
#                 x = int(x)
#                 y = int(y)
#
#                 #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)

# close the data file
        f.close()
예제 #25
0
        if frame is None:
            break

        # mirror
        cv.Flip(frame, None, 1)

        originalImage = frame

        hsvImage = cv.CreateImage(cv.GetSize(originalImage), 8, 3)
        cv.CvtColor(originalImage, hsvImage, cv.CV_BGR2HSV)

        thresholdImage = cv.CreateImage(cv.GetSize(originalImage), 8, 1)
        cv.InRangeS(hsvImage, cv.Scalar(20.74, 75, 75),
                    cv.Scalar(30.74, 255, 255), thresholdImage)

        thresholdImageArray = np.asarray(cv.GetMat(thresholdImage))
        thresholdImageArray = cv2.GaussianBlur(thresholdImageArray, (0, 0), 2)

        thresholdImage = cv.fromarray(thresholdImageArray)

        circles = cv2.HoughCircles(thresholdImageArray,
                                   cv.CV_HOUGH_GRADIENT,
                                   2,
                                   10,
                                   param1=40,
                                   param2=80,
                                   minRadius=4,
                                   maxRadius=1500)

        if circles is not None:
            if circles.size > 0:
예제 #26
0
cv.NamedWindow("video")
cv.NamedWindow("thresh")

tmp = cv.QueryFrame(capture)
imgScribble = cv.CreateImage(cv.GetSize(tmp), 8,
                             3)  #Image that will contain lines

posx = 0
posy = 0

while True:
    frame = cv.QueryFrame(capture)

    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
예제 #27
0
import cv2.cv as cv
import numpy

if __name__ == "__main__":
    cv.NamedWindow("camera", 1)

    capture = cv.CaptureFromCAM(0)

    paste = cv.CreateMat(960, 1280, cv.CV_8UC3)
    topleft = numpy.asarray(cv.GetSubRect(paste, (0, 0, 640, 480)))
    topright = numpy.asarray(cv.GetSubRect(paste, (640, 0, 640, 480)))
    bottomleft = numpy.asarray(cv.GetSubRect(paste, (0, 480, 640, 480)))
    bottomright = numpy.asarray(cv.GetSubRect(paste, (640, 480, 640, 480)))

    while True:
        img = cv.GetMat(cv.QueryFrame(capture))

        n = (numpy.asarray(img)).astype(numpy.uint8)

        red = n[:,:,2]
        grn = n[:,:,1]
        blu = n[:,:,0]

        topleft[:,:,0] = blu
        topleft[:,:,1] = grn
        topleft[:,:,2] = red


        color = red
        topright[:,:,0] = blu - red
        topright[:,:,1] = grn