예제 #1
0
                    conn.send('BUFFER OK')

                    data = conn.recv(buffer)
                    print data
                    flux = data.split(' ')

                    while True:
                        image = cv.QueryFrame(video)
                        faces = detect.FaceDetection.findFrame(image, flux[0], float(flux[1]), int(flux[2]))

                        if (faces):
                            finds = '{"faces": ['
                            index = 0
                            for ((x, y, w, h), n) in faces:
                                index += 1
                                finds += '{"x": "%s", "y": "%s", "w": "%s", "h": "%s"}' % (x, y, w, h)
                                if index < len(faces):
                                    finds += ','
                            finds += ']}'
                            conn.sendall('%s' % finds)
                        if cv.WaitKey(10) >= 0:
                                break
            except:
                message = '%s' % sys.exc_value
                message = message.replace('\\', '/').replace('\n', '').replace('\r', '')
                conn.send('{"error": "%s"}' % message)
                conn.close()
                s.close()
                video.release()
                cv.destroyAllWindows()
예제 #2
0
'''
Referred to example 2.4 in the book "Learning OpenCV: Computer Vision with the OpenCV Library"

Example 2-4. Loading and then smoothing an image before it is displayed on the screen

Brought to you by Abid.K	--mail me at [email protected]
'''
########################################################################################
import cv
image=cv.LoadImage("test.jpg")
#	create windows to show input and output images
cv.NamedWindow("Example4-in")
cv.NamedWindow("Example4-out")
cv.ShowImage("Example4-in",image)
#	create an image to hold smoothed output
out=cv.CreateImage(cv.GetSize(image),cv.IPL_DEPTH_8U,3)
#	smoothing--> cv.Smooth(input,output,smooth_type,parameter1,parameter2)
cv.Smooth(image,out,cv.CV_GAUSSIAN,3,3)
cv.ShowImage("Example4-out",out)
cv.WaitKey(0)
# 	be clean
cv.DestroyWindow("Example4-in")
cv.DestroyWindow("Example4-out")
#########################################################################################
예제 #3
0
def loopcv():
    cv.NamedWindow("Depth_Map", cv.CV_WINDOW_AUTOSIZE)
    cv.SetMouseCallback("Depth_Map", on_mouse, None)
    print "Execute loopcv: click 3 pts on mirror, click 4 pts at screen corner"
    global mr3
    global sn4
    global sn4_ref

    while 1:
        (depth, _) = freenect.sync_get_depth()
        im = array2cv(depth.astype(np.uint8))

        #[warp]add rgb as img

        if pt is not None:
            print "=================="
            (x_d, y_d) = pt
            print "x=", x_d, " ,y=", y_d
            #print depth.shape
            #Watch out the indexing for depth col,row = 480,640
            d_raw = np.array([depth[y_d, x_d]])
            u_d = np.array([x_d])
            v_d = np.array([y_d])

            print "d_raw= ", d_raw
            print "u_d= ", u_d
            print "v_d= ", v_d
            xyz, uv = calibkinect.depth2xyzuv(d_raw, u_d, v_d)
            print "XYZ=", xyz
            print "XYZonRGBplane=", uv
            cv.WaitKey(100)
            cv.Circle(im, (x_d, y_d), 4, (0, 0, 255, 0), -1, 8, 0)
            cv.Circle(im, (int(uv[0, 0]), int(uv[0, 1])), 2,
                      (255, 255, 255, 0), -1, 8, 0)

            if (mr3 is None):
                mr3 = xyz
                #print mr3

            elif (mr3.shape[0] <= 2):  #append "2"+1= 3pts
                mr3 = np.append(mr3, xyz, axis=0)
                #print "append mr3=",mr3
                if (mr3.shape[0] == 3):
                    print "enough for mirror, click on screen"
                    #print "mr3.shape= ",mr3.shape

            elif (mr3.shape[0] == 3):
                if (sn4 is None):
                    sn4 = xyz
                    sn4_ref = MirrorReflection(mr3, xyz[0])

                elif (sn4.shape[0] <= 3):  #append "3"+1= 4pts
                    sn4 = np.append(sn4, xyz, axis=0)
                    sn4_ref = np.append(sn4_ref,
                                        MirrorReflection(mr3, xyz[0]),
                                        axis=0)

                    if (sn4.shape[0] == 4):  #sn4 have 4 pts actually
                        print "Total screen pts before reflection=", sn4
                        #print "sn4 shape= ",sn4.shape
                        print "Total screen pts after reflection=", sn4_ref
                        #print "sn4_ref shape= ",sn4_ref.shape
                        if (mr3.shape[0] == 3 and sn4.shape[0] == 4):
                            print "go into Real Game: Virtual Mirror mode"
                            #print "mr3= ", mr3
                            #print "sn4_ref[0:3,:]= ", sn4_ref[0:3,:]
            else:
                print "..."

        #for (x,y) in feat:
        #print x, y, velx[y,x], vely[y,x]
        cv.ShowImage("Depth_Map", im)
        if cv.WaitKey(10) == 27:
            print "screen size after calibration: "
            print "Corner 1-2 = ", np.linalg.norm(sn4_ref[0] - sn4_ref[1])
            print "Corner 2-3 = ", np.linalg.norm(sn4_ref[1] - sn4_ref[2])
            print "Corner 3-4 = ", np.linalg.norm(sn4_ref[2] - sn4_ref[3])
            print "Corner 4-1 = ", np.linalg.norm(sn4_ref[3] - sn4_ref[0])
            break
    #update()
    if (sn4_ref is not None):
        cv.DestroyWindow("Depth_Map")
        VirtualMirror()
cv.Canny(image_threshed, image_threshed, 10, 50, 3)

current_contour = cv.FindContours(cv.CloneImage(image_threshed),
                                  cv.CreateMemStorage(), cv.CV_RETR_CCOMP,
                                  cv.CV_CHAIN_APPROX_SIMPLE)
largest_contour = current_contour
while True:
    current_contour = current_contour.h_next()
    if (not current_contour):
        break
    if (cv.ContourArea(current_contour) > cv.ContourArea(largest_contour)):
        largest_contour = current_contour

for c in largest_contour:
    cv.Circle(image, c, 1, (0, 255, 0), 1)

#cv.DrawContours(image, contours, (0,255,0), (255,0,0), 1)

moments = cv.Moments(largest_contour, 1)
center = (cv.GetSpatialMoment(moments, 1, 0) /
          cv.GetSpatialMoment(moments, 0, 0),
          cv.GetSpatialMoment(moments, 0, 1) /
          cv.GetSpatialMoment(moments, 0, 0))
cv.Circle(image, (int(center[0]), int(center[1])), 2, (0, 0, 255), 2)

# show the images
print "displaying images [press any key to continue]"
cv.ShowImage('original image', image)
cv.ShowImage('threshed image', image_threshed)
cv.WaitKey(10000)
예제 #5
0
#!/usr/bin/env python
import opennpy
import cv
import frame_convert
opennpy.align_depth_to_rgb()
cv.NamedWindow('Depth')
cv.NamedWindow('Video')
print('Press ESC in window to stop')


def get_depth():
    return frame_convert.pretty_depth_cv(opennpy.sync_get_depth()[0])


def get_video():
    return frame_convert.video_cv(opennpy.sync_get_video()[0])


while 1:
    cv.ShowImage('Depth', get_depth())
    cv.ShowImage('Video', get_video())
    if cv.WaitKey(10) == 27:
        break
예제 #6
0
        print frame_no

        # capture the current frame
        frame = cv.QueryFrame(capture)
        image_size = cv.GetSize(frame)
        if frame is None:
            break

        is_x, is_y = detect(frame)

        if len(is_x) > 0:
            print "is_x[5]=" + str(is_x[5])
            print "is_y[5]=" + str(is_y[5])
            kalman.update(array([is_x[5], frame.height - is_y[5], 1.]))
            proj_board(frame, kalman.mu_hat[0], kalman.mu_hat[1],
                       kalman.mu_hat[2])

            show_data(frame, kalman.mu_hat)

        # display webcam image
        cv.ShowImage('Camera', frame)

        # handle events
        k = cv.WaitKey(40)
        if k == "t":
            cvSaveImage('cb-kf-' + str(snap_no) + '.jpg', frame)
            snap_no += 1
        if k == 27:  # ESC
            print 'ESC pressed. Exiting ...'
            break
예제 #7
0
	x_ini=msg.pose.position.x
	y_ini=msg.pose.position.y
	z_ini=msg.pose.position.z

img=Imagen()

def camera_callback(data, camera_name):
        # Convert image from a ROS image message to a CV image
	global img
        try:
            img.setImg(cv_bridge.CvBridge().imgmsg_to_cv2(data))
        except cv_bridge.CvBridgeError, e:
            print e

        # 3ms wait
        cv.WaitKey(3)

# left camera call back function
def left_camera_callback(data):
        camera_callback(data, "Left Hand Camera")

# right camera call back function
def right_camera_callback(data):
        camera_callback(data, "Right Hand Camera")

# head camera call back function
def head_camera_callback(data):
        camera_callback(data, "Head Camera")

def subscribe_to_camera(camera):
        if camera == "left":
cv.NamedWindow("Mascara", 0)
cv.NamedWindow("Binario", 0)
cv.NamedWindow("Regiao de Interesse", 1)
cv.MoveWindow("Regiao de Interesse", 1000, 480)
cv.MoveWindow("Mascara", 0, 500)
cv.MoveWindow("Binario", 400, 500)

arquivo = open('Treino.txt', 'a')

mascara = cv.CreateImage((resolucao_largura, resolucao_altura), 8, 3)
cinza = cv.CreateImage((resolucao_largura, resolucao_altura), 8, 1)

while True:
    print("Por Favor tire uma foto do fundo estatico do seu video.")
    print("Aperte a tecla espaco.")
    if cv.WaitKey(0) % 0x100 == 32:
        primeiraImagem = cv.QueryFrame(captura)
        fundo = cv.CloneImage(primeiraImagem)
        cv.Smooth(fundo, fundo, cv.CV_GAUSSIAN, filtro_de_gauss)
        print("Tirou uma Foto !")
        break
    else:
        print "Uma foto do fundo nao foi tirada"
        break

while True:

    imagem = cv.QueryFrame(captura)
    cv.Smooth(imagem, imagem, cv.CV_GAUSSIAN, filtro_de_gauss)
    maiorArea = 0
    listaContornos = []
예제 #9
0
        sys.exit(1)

    input_name = args[0]
    if input_name.isdigit():
        capture = cv.CreateCameraCapture(int(input_name))
    else:
        capture = None

    cv.NamedWindow("result", 1)

    if capture:
        frame_copy = None
        while True:
            frame = cv.QueryFrame(capture)
            if not frame:
                cv.WaitKey(0)
                break
            if not frame_copy:
                frame_copy = cv.CreateImage((frame.width, frame.height),
                                            cv.IPL_DEPTH_8U, frame.nChannels)
            if frame.origin == cv.IPL_ORIGIN_TL:
                cv.Copy(frame, frame_copy)
            else:
                cv.Flip(frame, frame_copy, 0)

            detect_and_draw(frame_copy, cascade)

            if cv.WaitKey(10) >= 0:
                break
    else:
        image = cv.LoadImage(input_name, 1)
예제 #10
0
def main(progname, *args):

    parser = OptionParser()
    parser.add_option("-f",
                      "--file",
                      dest="filename",
                      default=None,
                      help="analyze a given FILE ending in .jpg or .jpeg",
                      metavar="FILE")
    parser.add_option(
        "-i",
        "--imageset",
        dest="imageset",
        default=None,
        help="Runs on a predefined set of algorithms (li,chow,china,custom)")
    parser.add_option("-d",
                      "--debug",
                      dest="debug",
                      action="store_true",
                      default=False,
                      help="Enable visual debugging.")
    parser.add_option(
        "-t",
        "--type",
        dest="type",
        default="all",
        help="Specifies the type of feature to debug. Defaults to all.")

    (options, args) = parser.parse_args(list(args))

    if options.imageset:
        if options.imageset == 'li':
            process(li_dir)
            return 0
        elif options.imageset == 'chow':
            process(chow_dir)
            return 0
        elif options.imageset == 'china':
            process(china_dir)
            return 0
        elif options.imageset == 'custom':
            process()
            return 0

    if not options.filename:
        print "Please specify a file (--file) or image set (--imageset)."
        return 1

    if not options.debug:
        process([load_image(options.filename)])
        return 0

    if options.filename.startswith('data/'):
        options.filename = options.filename[len('data/'):]

    tdata = load_image(options.filename)
    kind = options.type.lower()

    size = None  #(320,240,'crop') # (0.5, 0.5, 'resize-p')
    if size is None:
        im = tdata.load()
    elif size[-1] == 'crop':
        im = image.random_cropped_region(tdata.load(), size[:2])
    elif size[-1] == 'resize':
        im = tdata.load(size[:2])
    elif size[-1] == 'resize-p':
        im = image.resize(tdata.load(), by_percent=size[:2])
    else:
        raise TypeError, "Invalid image sizing type."

    image.show(im, "Image")
    #l,u,v = image.split(image.rgb2luv(im))
    ##cv.Set(l, 128)
    ##cv.EqualizeHist(l, l)
    ##cv.EqualizeHist(u, u)
    ##image.show(image.luv2rgb(image.merge(l,u,v)), "test")
    #s = cv.GetSize(im)
    #t = image.absDiff(u,v)
    #image.show(t, "test")
    #print "Test Score:", cv.CountNonZero(t) / float(s[0] * s[1])
    ##image.show(image.threshold(image.And(u,v), threshold=1), "LUV")

    # noise
    if kind in ('all', 'noise'):
        noise_img, score = noise.measure(im, debug=True)
        #image.show(noise_img, "Noise Result")
        print 'Noise Score:', score, noise.boolean(score)

    # contrast
    if kind in ('all', 'contrast'):
        contrast_img, score = contrast.measure(im, debug=True)
        #image.show(contrast_img, "Contrast Result")
        print 'Contrast Score:', score, contrast.boolean(score)

    # blur
    if kind in ('all', 'blur', 'composition'):
        focused, score = blur.measure(im, debug=kind in ('all', 'blur'))
        #image.show(focused,  "Blur Result")
        print 'Blur Score:', score, blur.boolean(score)

    # composition
    if kind in ('all', 'composition'):
        composition_img, score = composition.measure(
            im, (focused, score, blur.boolean(score)), debug=True)
        print 'Composition Score:', score, composition.boolean(score)

    if kind in ('faces', ):
        result, score = faces.measure(im, debug=True)
        print "Face Score:", score, faces.boolean(faces)

    #win = CornerTweaker(im)
    #win.show()

    #_, sat, _ = image.split(image.rgb2hsv(im))
    #arr = image.cv2array(sat)
    #print arr.mean(), arr.std()

    # faces
    #im, score = faces.measure(im, debug=True)
    #print score, faces.boolean(score)

    # composition
    #noise_img, score = noise.measure(im, debug=False)
    ##n = (noise_img, score, noise.boolean(score))
    #hulls, score = blur.measure(im, debug=False)
    #b = (hulls, score, blur.boolean(score))
    #cimg, score = composition.measure(im, b, debug=True)
    #print score, composition.boolean(score)

    # BLUR
    #from time import time
    #start = time()
    ##im2 = image.threshold(image.laplace(im), threshold=75, type=cv.CV_THRESH_TOZERO)
    #hulls, score = blur.measure(im, debug=True)
    ##blur_img, score = blur.measure(im, debug=True)
    #end = time()
    #print "Time:", (end - start), "seconds"
    #image.show(im,  "image")
    ##image.show(noise_img, "Noise Image")
    #print score, blur.boolean(score)

    #CONTRAST

    #_, score = contrast.measure(im, debug=True)
    #image.show(im, "Image")
    #print score, contrast.boolean(score)
    """
    
    #BLUR
    
    #im2 = image.threshold(image.laplace(im), threshold=75, type=cv.CV_THRESH_TOZERO)
    im3, score = blur.measure(im, debug=True)
    image.show(im,  "image")
    image.show(im3, "Focus Mask")
    print score, blur.boolean(score)
    #plt.show()
    """

    #NOISE

    #noise_img, score = noise.measure(im, debug=True)
    #image.show(noise_img, "Noise")
    #print score, noise.boolean(score)
    """
    #hwin = ColorHistograms(im)
    #hwin.show()
    hwin = HistogramWindow(image.rgb2gray(im))
    hwin.show()
    
    print cv.GetSize(im), cv.GetSize(im2)
    print 'blur', papers.blurry_histogram(im)
    #print papers.blurry_histogram(im2)
    
    wind = DerivativeTweaker(im, title="image derivative")
    wind.show()
    
    win = EdgeThresholdTweaker(im, title="image edges")
    win.show(50)#edge_threshold(im))
    
    #win2 = EdgeThresholdTweaker(im2, title="image resized edges")
    #win2.show(edge_threshold(im2))
    """
    cv.WaitKey()
    cv.DestroyAllWindows()
    return 0
예제 #11
0
    image = cv.CreateImage((temp.width, temp.height), cv.IPL_DEPTH_8U, 1)

    # Convert original image to B/W - Only because calculations are apparently better
    cv.CvtColor(temp, image, cv.CV_BGR2GRAY)

    # Use Haar Classifier for face detection (Magic Black Box)
    storage = cv.CreateMemStorage()
    haar = cv.Load('./haarcascade_frontalface_default.xml')
    detected = cv.HaarDetectObjects(image, haar, storage, 1.2, 2,
                                    cv.CV_HAAR_DO_CANNY_PRUNING, (100, 100))

    # Returns a list, the first item of which is a tuple
    # First element of tuple is x, second is y, third and fourth are width and height
    # We pass these params to create a rectangle. If we had not converted a b/w image
    # we can have a red border around instead
    if detected:
        cv.Rectangle(image, (detected[0][0][0], detected[0][0][1]),
                     (detected[0][0][0] + detected[0][0][2],
                      detected[0][0][1] + detected[0][0][3]),
                     cv.RGB(255, 0, 0), 3)
        ser.write('Just a really really long string to see something')

    # Create a named window and display image in it. Key press kills window and exits
    cv.NamedWindow('Nik')
    cv.ShowImage('Nik', image)

    char = cv.WaitKey(33)
    if (char != -1):
        if (ord(char) == 27):
            loop = False
예제 #12
0
    def run(self):
        # Initialize
        #log_file_name = "tracker_output.log"
        #log_file = file( log_file_name, 'a' )

        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 = 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)

        ###############################
        ### Face detection stuff
        #haar_cascade = cv.Load( 'haarcascades/haarcascade_frontalface_default.xml' )
        haar_cascade = cv.Load('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 = 3

        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)

            # 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)

            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)

            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)
예제 #13
0
imageWidth = 1024
imageHeight = 480

cv.NamedWindow("window1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0

capture = cv.CaptureFromCAM(camera_index)

cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, imageWidth)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, imageHeight)

font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 0.5, 0.5, 0, 1, cv.CV_AA)

while True:
    frame = cv.QueryFrame(capture)
    cv.Rectangle(frame, (0, 0), (imageWidth, 15), (255, 255, 255),
                 cv.CV_FILLED, 8, 0)
    timeStampString = datetime.datetime.now().strftime("%A %Y-%m-%d %I:%M %p")
    cv.PutText(frame, timeStampString, (10, 10), font, (0, 0, 0))
    cv.ShowImage("window1", frame)

    command = cv.WaitKey(10)

    if command == ord('q'):
        print("Ending program")
        break
    elif command == ord('s'):
        print("Saving image")
        cv.SaveImage("img00.jpg", frame)
예제 #14
0
    def run(self):
        frame = cv.QueryFrame(self.capture)
        frame_size = cv.GetSize(frame)
        grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)
        run_mean = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_32F, 3)
        diff = None
        movement = []

        while True:
            # Capture frame from webcam
            color_image = cv.QueryFrame(self.capture)
            cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0)

            if not diff:
                # Initialize
                diff = cv.CloneImage(color_image)
                temp = cv.CloneImage(color_image)
                cv.ConvertScale(color_image, run_mean, 1.0, 0.0)
            else:
                cv.RunningAvg(color_image, run_mean, 0.020, None)

            # Convert the scale of the moving average.
            cv.ConvertScale(run_mean, temp, 1.0, 0.0)

            # Minus the current frame from the moving average.
            cv.AbsDiff(color_image, temp, diff)

            # Convert the image to grayscale.
            cv.CvtColor(diff, grey_image, cv.CV_RGB2GRAY)

            # Convert the image to black and white.
            cv.Threshold(grey_image, grey_image, 70, 255, cv.CV_THRESH_BINARY)

            # Dilate and erode to get object blobs
            cv.Dilate(grey_image, grey_image, None, 18)
            cv.Erode(grey_image, grey_image, None, 10)

            # Calculate movements
            store_movement = cv.CreateMemStorage(0)
            contour = cv.FindContours(grey_image, store_movement, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
            points = []

            while contour:
                # Draw rectangles
                bound_rect = cv.BoundingRect(list(contour))
                contour = contour.h_next()

                pt1 = (bound_rect[0], bound_rect[1])
                pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
                points.append(pt1)
                points.append(pt2)
                cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1)

            num_points = len(points)

            if num_points:
                x = 0
                for point in points:
                    x += point[0]
                x /= num_points

                movement.append(x)

            if len(movement) > 0 and numpy.average(numpy.diff(movement[-30:-1])) > 0:
              print 'Left'
            else:
              print 'Right'

            # Display frame to user
            cv.ShowImage("Object Direction of Motion", color_image)

            # Listen for ESC or ENTER key
            c = cv.WaitKey(7) % 0x100
            if (0xFF & c == 27):
               break
예제 #15
0
파일: corner.py 프로젝트: mroja/eyetracker
    img = cv.CreateImage((200, 200), 8, 1)

    col = (128, 128, 128)
    p3 = [(0, 0), (65, 65), (200, 0)]
    p1 = [(0, 0), (120, 65), (200, 0)]
    p2 = [(0, 200), (120, 100), (200, 0)]
    c1 = Corner(p1, 1)
    #print det((0,0),(0,1) , (1,0)), det((0,0),(1,0),(0,1))
    c1.measure()
    c2 = Corner(p2, 1, imgtime=2)
    c2.measure()
    c3 = Corner(p3, 1, imgtime=2)
    c3.measure()
    c1.computeChange(c3, 1)

    c2.computeChange(c1, 1)
    c3.draw(img)
    c1.draw(img)
    c2.draw(img)
    points = c2.get_bounding_points(1)
    for p in points:
        cv.Circle(img, p, 2, (255, 255, 255))
    cv.Line(img, c2.p, points[0], (255, 255, 255))
    rect = c2.get_rectangle(1)
    #print rect
    cv.Rectangle(img, (rect[0], rect[1]),
                 (rect[0] + rect[2], rect[1] + rect[3]), (64, 64, 64))
    cv.NamedWindow('a')
    cv.ShowImage('a', img)
    cv.WaitKey(120000)
예제 #16
0
파일: test04_surf.py 프로젝트: wklharry/hrl
import roslib

roslib.load_manifest('hai_sandbox')
import cv
import sys
import hai_sandbox.features as fea

if __name__ == '__main__':
    fname = sys.argv[1]
    image = cv.LoadImage(fname)
    image_gray = cv.CreateImage((640, 480), cv.IPL_DEPTH_8U, 1)
    cv.CvtColor(image, image_gray, cv.CV_BGR2GRAY)

    star_keypoints = fea.star(image)
    surf_keypoints, surf_descriptors = fea.surf(image_gray)
    harris_keypoints = fea.harris(image_gray)

    cv.NamedWindow('surf', 1)
    cv.NamedWindow('harris', 1)
    cv.NamedWindow('star', 1)
    while True:
        cv.ShowImage('surf', fea.draw_surf(image, surf_keypoints, (255, 0, 0)))
        cv.ShowImage('harris',
                     fea.draw_harris(image, harris_keypoints, (0, 255, 0)))
        cv.ShowImage('star', fea.draw_star(image, star_keypoints, (0, 0, 255)))
        k = cv.WaitKey(33)
        if k == 27:
            break

    #Canny(image, edges, threshold1, threshold2, aperture_size=3) => None
예제 #17
0
def wait():
    while True:
        k = cv.WaitKey(0) % 0x100
        if k == 27:
            break
예제 #18
0
class video_processor:
    def __init__(self):
        self.sub = rospy.Subscriber('usb_cam/image_raw', Image, self.callback)
        self.pub = rospy.Publisher('heading', Twist)
        self.speed = float(1)
        self.bridge = CvBridge()
        cv.NamedWindow("Input Video")
        #cv.NamedWindow("Blur Video")
        #cv.NamedWindow("HSV Video")
        #cv.NamedWindow("Hue Video")
        #cv.NamedWindow("Saturation Video")
        #cv.NamedWindow("Value Video")
        cv.NamedWindow("Red-Orange Video")
        cv.NamedWindow("White Video")
        cv.NamedWindow("Red-Orange and White Video")
        #cv.WaitKey(0)

    def callback(self, image_in):
        try:
            input_image = self.bridge.imgmsg_to_cv(image_in,"bgr8")
        except CvBridgeError, e:
            print e
        cv.ShowImage("Input Video", input_image)

        blur_image = cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC3)
        cv.Smooth(input_image,blur_image,cv.CV_BLUR, 10, 10)
        #cv.ShowImage("Blur Video", proc_image)
        proc_image = cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC3)
        cv.CvtColor(blur_image, proc_image, cv.CV_BGR2HSV)
        #cv.ShowImage("HSV Video", proc_image)
        split_image = [cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC1),cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC1),cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC1)]
        cv.Split(proc_image, split_image[0],split_image[1],split_image[2], None )
        #hue = cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC1)
        #sat = cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC1)
        #val = cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC1)
        #cv.Split(proc_image, hue,sat,val, None )
        #cv.ShowImage("Hue Video", hue)
        #cv.ShowImage("Saturation Video", sat)
        #cv.ShowImage("Value Video", val)

        thresh_0 = cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC1)
        thresh_1 = cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC1)
        thresh_2 = cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC1)
        red_orange = cv.CreateMat(input_image.rows,input_image.cols,cv.CV_8UC1)
        cv.Threshold(split_image[1],thresh_0, 128,255,cv.CV_THRESH_BINARY) # > 50% saturation
        cv.Threshold(split_image[0],thresh_1, 220,255,cv.CV_THRESH_BINARY) # > Purple
        cv.Threshold(split_image[0],thresh_2, 10, 255,cv.CV_THRESH_BINARY_INV) # < Yellow-Orange
        cv.Add(thresh_1,thresh_2,red_orange)
        cv.And(red_orange,thresh_0,red_orange)
        cv.ShowImage("Red-Orange Video",red_orange)

        cv.CvtColor(blur_image, proc_image, cv.CV_BGR2HLS)
        cv.Split(proc_image, split_image[0], split_image[1],split_image[2], None )
        cv.Threshold(split_image[1],thresh_0, 204,255,cv.CV_THRESH_BINARY) # > 80% Lum
        cv.ShowImage("White Video",thresh_0)
        cv.Or(red_orange, thresh_0, thresh_0)
        cv.ShowImage("Red-Orange and White Video",thresh_0)
        cv.WaitKey(30)

        ang_z = 0
        x = 0
        for i in range(input_image.rows):
            y = -(input_image.cols / 2)
            row = cv.GetRow(thresh_0,i)
            for j in row.tostring():
                ang_z = ang_z + (x * y *ord(j))
                y = y + 1
            x = x + 1
        ang_z = (ang_z * pi * 2 * 2 * 4 / 255 / input_image.rows / input_image.rows / input_image.cols / input_image.cols)
        p = Twist()
        p.linear.x = self.speed
        p.angular.z = ang_z
        self.pub.publish(p)
예제 #19
0
def main():
    if len(sys.argv) == 1:
        print 'Usage: %s [inputfile]' % sys.argv[0]
        sys.exit(1)

    # initialize window
    cv.NamedWindow('video', cv.CV_WINDOW_AUTOSIZE)
    cv.MoveWindow('video', 10, 10)

    cv.NamedWindow('threshold', cv.CV_WINDOW_AUTOSIZE)
    cv.MoveWindow('threshold', 10, 500)

    cv.NamedWindow('flow', cv.CV_WINDOW_AUTOSIZE)
    cv.MoveWindow('flow', 500, 10)

    cv.NamedWindow('edges', cv.CV_WINDOW_AUTOSIZE)
    cv.MoveWindow('edges', 500, 500)

    cv.NamedWindow('combined', cv.CV_WINDOW_AUTOSIZE)
    cv.MoveWindow('combined', 1000, 10)

    capture = cv.CreateFileCapture(sys.argv[1])
    if not capture:
        print 'Error opening capture'
        sys.exit(1)

    # Load bg image
    bg = cv.LoadImage('bg.png')

    # Discard some frames
    for i in xrange(2300):
        cv.GrabFrame(capture)

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

    # vars for playback
    fps = 25
    play = True
    velx = cv.CreateImage(frame_size, cv.IPL_DEPTH_32F, 1)
    vely = cv.CreateImage(frame_size, cv.IPL_DEPTH_32F, 1)
    combined = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1)
    prev = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1)
    curr = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1)
    frame_sub = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 3)

    edges = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1)
    prev_edges = None
    storage = cv.CreateMemStorage(0)

    blob_mask = cv0.cvCreateImage(frame_size, cv.IPL_DEPTH_8U, 1)
    cv0.cvSet(blob_mask, 1)

    hough_in = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1)
    hough_storage = cv.CreateMat(100, 1, cv.CV_32FC3)
    '''
    cv.CvtColor(frame, prev, cv.CV_BGR2GRAY)
    frame = cv.QueryFrame(capture)
    cv.CvtColor(frame, curr, cv.CV_BGR2GRAY)

    # winSize can't have even numbers
    cv.CalcOpticalFlowLK(prev, curr, (3,3), velx, vely)
    cv.ShowImage('video', frame)
    cv.ShowImage('flow', velx)
    cv.WaitKey(0)
    '''

    while True:

        if play:
            frame = cv.QueryFrame(capture)
            cv.Sub(frame, bg, frame_sub)
            '''#detect people
            found = list(cv.HOGDetectMultiScale(frame, storage, win_stride=(8,8),
                padding=(32,32), scale=1.05, group_threshold=2))
            for r in found:
                (rx, ry), (rw, rh) = r
                tl = (rx + int(rw*0.1), ry + int(rh*0.07))
                br = (rx + int(rw*0.9), ry + int(rh*0.87))
                cv.Rectangle(frame, tl, br, (0, 255, 0), 3)
            '''

            #color thresholding
            hsv = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 3)
            cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
            mask = cv.CreateMat(frame_size[1], frame_size[0], cv.CV_8UC1)
            cv.InRangeS(hsv, (0.06 * 256, 0.2 * 256, 0.6 * 256, 0),
                        (0.16 * 256, 1.0 * 256, 1.0 * 256, 0), mask)
            cv.ShowImage('threshold', mask)

            #optical flow method
            # store previous frame
            prev, curr = curr, prev
            # convert next frame to single channel grayscale
            cv.CvtColor(frame_sub, curr, cv.CV_BGR2GRAY)
            #cv.CalcOpticalFlowLK(prev, curr, (3,3), velx, vely)
            #cv.Threshold(velx, velx, 8.0, 0, cv.CV_THRESH_TOZERO)
            cv.CalcOpticalFlowHS(prev, curr, 1, velx, vely, 0.5,
                                 (cv.CV_TERMCRIT_ITER, 10, 0))
            cv.Threshold(velx, velx, 0.5, 0, cv.CV_THRESH_TOZERO)
            cv.Threshold(vely, vely, 0.5, 0, cv.CV_THRESH_TOZERO)
            cv.Erode(
                vely, vely,
                cv.CreateStructuringElementEx(2, 2, 0, 0, cv.CV_SHAPE_ELLIPSE))
            cv.Add(vely, velx, vely)
            cv.ShowImage('flow', vely)

            #edge detection
            cv.Canny(curr, edges, 50, 100)
            cv.Dilate(
                edges, edges,
                cv.CreateStructuringElementEx(7, 7, 0, 0, cv.CV_SHAPE_ELLIPSE))
            cv.ShowImage('edges', edges)

            if prev_edges:
                cv.CalcOpticalFlowHS(prev_edges, edges, 1, velx, vely, 0.5,
                                     (cv.CV_TERMCRIT_ITER, 10, 0))
                cv.Threshold(velx, velx, 0.5, 0, cv.CV_THRESH_TOZERO)
                cv.Threshold(vely, vely, 0.5, 0, cv.CV_THRESH_TOZERO)
                cv.ShowImage('flow', vely)
            prev_edges = edges

            cv.Threshold(vely, combined, 0.5, 255, cv.CV_THRESH_BINARY)
            cv.Min(combined, edges, combined)
            cv.ShowImage('combined', combined)

            # blobs
            myblobs = CBlobResult(edges, blob_mask, 100, False)
            myblobs.filter_blobs(10, 10000)
            blob_count = myblobs.GetNumBlobs()

            for i in range(blob_count):

                my_enumerated_blob = myblobs.GetBlob(i)
                #               print "%d: Area = %d" % (i, my_enumerated_blob.Area())
                my_enumerated_blob.FillBlob(frame,
                                            hsv2rgb(i * 180.0 / blob_count), 0,
                                            0)

            cv.ShowImage('video', frame)
            ''' crashes
            #hough transform on dilated image
            #http://wiki.elphel.com/index.php?
            # title=OpenCV_Tennis_balls_recognizing_tutorial&redirect=no
            cv.Copy(edges, hough_in)
            cv.Smooth(hough_in, hough_in, cv.CV_GAUSSIAN, 15, 15, 0, 0)
            cv.HoughCircles(hough_in, hough_storage, cv.CV_HOUGH_GRADIENT,
                            4, frame_size[1]/10, 100, 40, 0, 0)
            print hough_storage
            '''

        k = cv.WaitKey(1000 / fps)
        if k == 27:  # ESC key
            break
        elif k == 'p':  # play/pause
            play = not play
예제 #20
0
    def getFrameData(self, files):
        depthFilename = []
        rgbFilename = []
        skelFilename = []

        # pdb.set_trace()

        # Get filenames
        if len(files) > 0:
            for i in files:
                if i[-5:] == 'depth':
                    depthFilename = i
                    self.depthFilename = depthFilename
                if i[-3:] == 'rgb':
                    rgbFilename = i
                    self.rgbFilename = rgbFilename
                if self.skelsEnabled:
                    if i[-4:] == 'skel':
                        skelFilename = i
                        self.skelFilename = skelFilename

            displayUsers = []
            skels = []
            if len(skelFilename) > 0:
                users = SR.readUserData(skelFilename)
                skels = SR.readSkeletonData(skelFilename)
                for i in users:
                    displayUsers.append(users[i]['Img'])

                #eliminate bad skeleton data (joint data is linked to old data)
                # pdb.set_trace()
                deleteInd = []
                for i in users.keys():
                    # print users[i]['Img']
                    if users[i]['Img'][2] == 0:
                        deleteInd.append(i)
                deleteInd.sort(reverse=1)
                for i in deleteInd:
                    # print "Del", i
                    del users[i]
                    del skels[i]
                    del displayUsers[i - 1]

            if len(depthFilename) > 0:
                ## 1
                # depthRaw = open(depthFilename, 'rb').read().split()
                # depthRaw = np.fromfile(depthFilename, dtype=np.uint16, sep=" ")
                # self.depthData = np.array(depthRaw, dtype=int).reshape([480,640])[:,::-1]
                # self.depthData = self.depthData[:,::-1]
                ## 2
                # self.depthData = np.fromfile(depthFilename, dtype=np.uint16, sep=" ").reshape([480, 640])[:,::-1]
                # self.depthDataRaw = self.depthData
                ## 3
                x = open(depthFilename).read()
                self.depthDataRaw = np.fromstring(x, dtype=np.uint16,
                                                  sep=" ").reshape([480, 640
                                                                    ])[:, ::-1]
                self.depthIm = self.depthDataRaw
                if self.constrain != []:
                    self.depthIm8 = constrain(self.depthIm, self.constrain[0],
                                              self.constrain[1])

                # print "User count: ", len(displayUsers)
                # print displayUsers
                if self.viz:
                    self.depthData = constrain(self.depthDataRaw)
                    try:
                        for i in displayUsers:
                            if i[0] != 0:
                                for x in xrange(-10, 10):
                                    for j in xrange(-1, 1):
                                        self.depthData[480 - i[1] + j,
                                                       640 - i[0] + x] = 30
                                for y in xrange(-10, 10):
                                    for j in xrange(-1, 1):
                                        self.depthData[480 - i[1] + y,
                                                       640 - i[0] + j] = 30
                    except:
                        print "Error adding cross at", i

                # self.depthData = cv.fromarray(np.array(self.depthData, dtype=np.uint8))
                # print "Skels: ", len(skels)
                # print skels
                if skels != [] and self.vizSkel:
                    self.depthData = cv.fromarray(
                        np.array(self.depthData, dtype=np.uint8))
                    self.depthData = SR.displaySkeleton_CV(
                        self.depthData, skels)
                if self.viz:
                    self.depthData = cv.fromarray(
                        np.array(self.depthData, dtype=np.uint8))
                    cv2.imshow(self.windowName,
                               np.array(self.depthData, dtype=np.uint8))

            if len(rgbFilename) > 0:
                imgData = np.fromfile(rgbFilename, dtype=np.uint8)
                imgData = imgData.reshape([480, 640, 3])
                if self.viz:
                    cv2.imshow("RGB", imgData)

            cv.WaitKey(1)
예제 #21
0
#!/usr/bin/env python2

import cv

# ESC = 1048603
ESC = 27


def WaitKey(delay=0):
    c = cv.WaitKey(delay)
    if c == -1:
        ret = -1
    else:
        ret = c & ~0b100000000000000000000

    return ret


if __name__ == '__main__':
    cv.NamedWindow("janela", cv.CV_WINDOW_AUTOSIZE)

    cv.ShowImage("janela", None)
    c = cv.WaitKey()
    print "%d - %d" % (c & ~0b100000000000000000000, c)
예제 #22
0
 
    # If faces are found
    if faces:
        for ((x, y, w, h), n) in faces:
            # the input to cv.HaarDetectObjects was resized, so scale the
            # bounding box of each face and convert it to two CvPoints
            pt1 = (int(x * image_scale), int(y * image_scale))
            pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
            cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)
 
    return image
 
#----------
# M A I N
#----------
 
capture = cv.CaptureFromCAM(0)
#capture = cv.CaptureFromFile("test.avi")
 
#faceCascade = cv.Load("haarcascades/haarcascade_frontalface_default.xml")
#faceCascade = cv.Load("haarcascades/haarcascade_frontalface_alt2.xml")
faceCascade = cv.Load("haarcascades/haarcascade_frontalface_alt.xml")
#faceCascade = cv.Load("haarcascades/haarcascade_frontalface_alt_tree.xml")
 
while (cv.WaitKey(15)==-1):
    img = cv.QueryFrame(capture)
    image = DetectFace(img, faceCascade)
    cv.ShowImage("face detection test", image)
 
cv.ReleaseCapture(capture)
예제 #23
0
class image_converter:
    def __init__(self):
        self.image_pub = rospy.Publisher("image_converter1", Image)

        cv.NamedWindow("Image window", 1)
        self.bridge = CvBridge()
        self.image_sub = rospy.Subscriber("ardrone/image_raw", Image,
                                          self.callback)

    def callback(self, data):
        try:
            cv_image = self.bridge.imgmsg_to_cv(data, "bgr8")
        except CvBridgeError, e:
            print e

        #(cols,rows) = cv.GetSize(cv_image)
        #if cols > 60 and rows > 60 :
        #  cv.Circle(cv_image, (50,50), 10, 255)

        #######################################
        #img1=cv.CreateImage((150,150),8,3)
        #cv.Rectangle(cv_image, (60,60),(70,90), (255,0,255))
        #sub1=cv.GetSubRect(cv_image,(60,60,150,150))
        #save1=cv.CloneMat(sub1)
        #sub2=cv.GetSubRect(img1,(0,0,150,150))
        #cv.Copy(save1,sub2)
        storage = cv.CreateMemStorage(0)
        img = cv.CreateImage(
            (cv.GetSize(cv_image)[1], cv.GetSize(cv_image)[1]), 8, 3)
        img1 = cv.CreateImage(
            (cv.GetSize(cv_image)[1], cv.GetSize(cv_image)[1]), 8, 3)
        #img=cv.CreateImage(cv.GetSize(cv_image),8,3)
        img_r = cv.CreateImage(cv.GetSize(img), 8, 1)
        img_g = cv.CreateImage(cv.GetSize(img), 8, 1)
        img_b = cv.CreateImage(cv.GetSize(img), 8, 1)
        img_g1 = cv.CreateImage(cv.GetSize(img), 8, 1)
        img_g2 = cv.CreateImage(cv.GetSize(img), 8, 1)

        img2 = cv.LoadImage("/home/petin/catkin_ws/src/vp_ardrone2/ris1.jpg",
                            cv.CV_LOAD_IMAGE_GRAYSCALE)

        sub1 = cv.GetSubRect(
            cv_image, (0, 0, cv.GetSize(cv_image)[1], cv.GetSize(cv_image)[1]))
        save1 = cv.CloneMat(sub1)
        sub2 = cv.GetSubRect(
            img, (0, 0, cv.GetSize(cv_image)[1], cv.GetSize(cv_image)[1]))
        cv.Copy(save1, sub2)

        #cv.CvtColor(img, img1, cv.CV_BGR2HSV)
        #cv.CvtPixToPlane(img1,img_h,img_s,img_v,None)
        #cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
        #cv.Smooth(gray,gray,cv.CV_GAUSSIAN,5,5)
        cv.Split(img, img_b, img_g, img_r, None)
        #
        #cv.ShowImage("Image window1", img)
        #cv.ShowImage("Image windowb", img_b)
        #cv.ShowImage("Image windowg", img_g)
        #cv.ShowImage("Image windowr", img_r)
        #
        cv.InRangeS(img_g, cv.Scalar(180), cv.Scalar(255), img_g1)
        #cv.InRangeS(img_s, cv.Scalar(135), cv.Scalar(255), img_s1);
        #cv.InRangeS(img_b, cv.Scalar(0), cv.Scalar(61), img_b1);
        #cv.Invert(img_g1,img_g2,cv.CV_SVD)
        cv.Smooth(img2, img2, cv.CV_GAUSSIAN, 9, 9)
        #
        cv.ShowImage("Image windowh1", img_g1)
        #cv.ShowImage("Image windowg1", img_h1)
        #cv.ShowImage("Image windowr1", img_r1)
        #cv.ShowImage("Image gray", gray)
        # search circle
        storage = cv.CreateMat(img2.width, 1, cv.CV_32FC3)
        cv.ShowImage("Image window1", img2)
        cv.HoughCircles(img2, storage, cv.CV_HOUGH_GRADIENT, 2, 100, 100, 50,
                        10, 400)
        #rospy.loginfo(storage.width)
        for i in xrange(storage.width - 1):
            radius = storage[i, 2]
            center = (storage[i, 0], storage[i, 1])
            rospy.loginfo('444')
            cv.Circle(cv_image, center, radius, (0, 0, 255), 3, 10, 200)
        #search_circle=cv.HoughCircles(img_g1,np.asarray(storage),3,10,150)
        #for res in search_circles:
        #   p = float (cv.GetSeqElem(res))
        #   pt = cv.Point( cv.Round( p[0] ), cv.Round( p[1] ) );
        #   cv.Circle( cv_image, pt, cv.Round( p[2] ), 255 );
        #
        #cv.And(img_g,img_r,img_a)
        #cv.And(img_a,img_b,img_a)
        #
        cv.ShowImage("Image window", cv_image)
        cv.WaitKey(3)

        try:
            self.image_pub.publish(self.bridge.cv_to_imgmsg(cv_image, "bgr8"))
        except CvBridgeError, e:
            print e
예제 #24
0
def mainLoop():
  input_video_fn = get_input_video_filename()
  print 'input video filename:', input_video_fn
  # Setting up the window objects and environment
  proc_win_name = "Processing window"
  cam_win_name = "Capture from camera"
  proc_win = cv.NamedWindow(proc_win_name, 1)
  cam_win = cv.NamedWindow(cam_win_name, 1)
  if input_video_fn:
    cam = cv.CaptureFromFile(input_video_fn)
  else:
    cam = cv.CaptureFromCAM(0)
  cv.SetMouseCallback(proc_win_name, handle_mouse)
  cv.SetMouseCallback(cam_win_name, handle_mouse)
  msdelay = 3
  initHueThreshold = 42
  initIntensityThreshold = 191
  skin_detector = skin.SkinDetector()
  skin_detector.setHueThreshold(initHueThreshold)
  skin_detector.setIntensityThreshold(initIntensityThreshold)
  cv.CreateTrackbar('hueThreshold',
                    proc_win_name,
                    initHueThreshold,
                    255,
                    skin_detector.setHueThreshold)
  cv.CreateTrackbar('intensityThreshold',
                    proc_win_name,
                    initIntensityThreshold,
                    255,
                    skin_detector.setIntensityThreshold)

  session = ImageProcessSession(skin_detector)
  ga = gesture.GestureAnalyzer()
  grammar = Grammar()
  gfn = get_grammar_filename()
  if not gfn:
      print 'usage: python GestureLock.py -g grammar_file.gmr'
      exit(0)
  answer_grammer = read_grammar(gfn)
  im_orig_writer = ImageWriter(output_folder=get_output_folder())
  im_contour_writer = ImageWriter(output_folder='out2')
  prev = []
  while True:
    k = cv.WaitKey(msdelay)
    k = chr(k) if k > 0 else 0
    if handle_keyboard(k) < 0:
        break
    bgrimg = cv.QueryFrame(cam)
    if not bgrimg:
        break
    im_orig_writer.write(bgrimg)
    cv.Flip(bgrimg, None, 1)
    contours = session.process(bgrimg)

    img = cv.CreateImage((bgrimg.width, bgrimg.height), 8, 3)
    if contours:
        ges, area, depth = ga.recognize(contours)
        x, y, r, b = im.find_max_rectangle(contours)
        cv.Rectangle(img, (x,y), (r, b), im.color.RED)
        cv.DrawContours(img, contours, im.color.RED, im.color.GREEN, 1,
            thickness=3)
        print ges
        currentInput = grammar.instantGes(ges)
        print currentInput
        
        if len(prev)>=2:
          for i,g in enumerate(currentInput):
              im.puttext(prev[0], str(g), 30, 70+40*i)
          im_contour_writer.write(prev[0])
          prev.append( img )
          prev.pop(0)
        else:
          prev.append( img )
    if grammar == answer_grammer:
        for i,g in enumerate(currentInput):
          im.puttext(prev[0], str(g), 30, 70+40*i)
        im_contour_writer.write(prev[0])
        im.puttext(prev[0], 'AUTHENTICATED!', 30, 70+40*len(currentInput))
        im_contour_writer.write(prev[0])
        print 'AUTHENTICATED!!!!'
        break
    cv.ShowImage(proc_win_name, img)
예제 #25
0
    def run(self):
        # Capture first frame to get size
        frame = cv.QueryFrame(self.capture)
        frame_size = cv.GetSize(frame)
        color_image = cv.CreateImage(cv.GetSize(frame), 8, 3)
        grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)
        moving_average = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_32F, 3)

        first = True

        while True:
            closest_to_left = cv.GetSize(frame)[0]
            closest_to_right = cv.GetSize(frame)[1]

            color_image = cv.QueryFrame(self.capture)

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

            if first:
                difference = cv.CloneImage(color_image)
                temp = cv.CloneImage(color_image)
                cv.ConvertScale(color_image, moving_average, 1.0, 0.0)
                first = False
            else:
                cv.RunningAvg(color_image, moving_average, 0.020, None)

            # Convert the scale of the moving average.
            cv.ConvertScale(moving_average, temp, 1.0, 0.0)

            # Minus the current frame from the moving average.
            cv.AbsDiff(color_image, temp, difference)

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

            # Convert the image to black and white.
            cv.Threshold(grey_image, grey_image, 70, 255, cv.CV_THRESH_BINARY)

            # Dilate and erode to get people blobs
            cv.Dilate(grey_image, grey_image, None, 18)
            cv.Erode(grey_image, grey_image, None, 10)

            storage = cv.CreateMemStorage(0)
            contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP,
                                      cv.CV_CHAIN_APPROX_SIMPLE)
            points = []

            while contour:
                bound_rect = cv.BoundingRect(list(contour))
                contour = contour.h_next()

                pt1 = (bound_rect[0], bound_rect[1])
                pt2 = (bound_rect[0] + bound_rect[2],
                       bound_rect[1] + bound_rect[3])
                points.append(pt1)
                points.append(pt2)
                cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255, 0, 0), 1)

            if len(points):
                center_point = reduce(
                    lambda a, b: ((a[0] + b[0]) / 2, (a[1] + b[1]) / 2),
                    points)
                cv.Circle(color_image, center_point, 40,
                          cv.CV_RGB(255, 255, 255), 1)
                cv.Circle(color_image, center_point, 30,
                          cv.CV_RGB(255, 100, 0), 1)
                cv.Circle(color_image, center_point, 20,
                          cv.CV_RGB(255, 255, 255), 1)
                cv.Circle(color_image, center_point, 10,
                          cv.CV_RGB(255, 100, 0), 1)

            cv.ShowImage("Target", color_image)

            # Listen for ESC key
            c = cv.WaitKey(7) % 0x100
            if c == 27:
                break
예제 #26
0
            + struct.pack('b', bearingToGoal) \
            + struct.pack('B', 0)
        cyril.write(output)

        #Data Logging
        if (cyril.inWaiting() > 0):
            logdata = cyril.read(cyril.inWaiting())
            a = 0
            b = 0
            for c in logdata:
                if c == '\n':
                    datalog.write(
                        str(datetime.now().time()) + "," + logdata[a:b] + "\n")
                    a = b + 1
                b = b + 1

        cv.ShowImage('cam', img)
        cv.ShowImage('unwrapped', cropped)
        cv.ShowImage('target', cones)

        key = cv.WaitKey(10)  # THIS REQUIRES AT LEAST ONE WINDOW
        #print "key ",key
        if key > 0:
            break

    cv.DestroyAllWindows()
    cyril.close()
    datalog.write("\n~~~=== Rambler Data Log Closed, " + str(datetime.now()) +
                  " ===~~~\n")
    datalog.close()
예제 #27
0
import cv
import numpy as np
cv.NamedWindow('Leak')
while 1:
    leak = np.random.random((480, 640)) * 255
    cv.ShowImage('Leak', leak.astype(np.uint8))
    cv.WaitKey(10)
예제 #28
0
def rangeGUI():
    """ creates and displays a GUI for the range finder data
        Ranges window: shows range finder values as red lines
        coming from the center of the range finder
        HoughLines window: shows the result of using a Hough
        transformation on image containing the range values as points.
    """
    global D

    init_GUI()  # initialize images and windows

    # initialize the lists and variables
    D.dangerList = []  # Set up the danger point list
    D.ahead = []
    D.behind = []
    D.left = []
    D.right = []
    D.broadcast = []
    D.send_counter = 0  # counter to turn on the 'send' switch
    D.corner_type = 0  # counter to send the corner type
    D.send = True  # at first the send switch turns on
    D.text = "Wait for information"  # Text on the display window
    D.waiting = 0  # Counter for waiting map direction
    D.next_dir = ""
    D.default_dir = "Right"

    # main loop
    while rospy.is_shutdown() == False:

        # loop through every angle
        for angle in range(REV):
            magnitude = MAG_SCALE * D.ranges[angle]
            x = int(CENTER + magnitude * cos(pi / 180 * (angle + ANGLE_OFFSET))
                    )  # find x and y coordinates based on the angle
            y = int(CENTER - magnitude *
                    sin(pi / 180 *
                        (angle + ANGLE_OFFSET)))  # and the length of the line

            # put the danger points into the list
            if x > CENTER - 30 and x < CENTER + 30 and y < CENTER - 30 and y > CENTER - 90:
                D.dangerList.append((x, y))

            # check the points in the cross zone and put them in four lists
            if x < CENTER - 20 and y > CENTER - 20 and y < CENTER + 10:
                D.left.append((x, y))
            elif x > CENTER + 20 and y > CENTER - 20 and y < CENTER + 10:
                D.right.append((x, y))
            elif y < CENTER - 30 and y > 60 and x < CENTER + 10 and x > CENTER - 10:
                D.ahead.append((x, y))
            elif y > CENTER + 10 and x < CENTER + 10 and x > CENTER - 10:
                D.behind.append((x, y))

            if 0 < magnitude < MAX_MAG:  # if data is within "good data" range
                # add line to the ranges image
                cv.Line(D.image, (CENTER, CENTER), (x, y), cv.RGB(255, 0, 0))
                # add dot to image being used in Hough transformation
                cv.Line(D.hough, (x, y), (x, y), cv.RGB(255, 0, 0))

        # wait and check for quit request
        key_code = cv.WaitKey(1) & 255
        key_press = chr(key_code)
        if key_code == 27 or key_press == 'q':  # if ESC or 'q' was pressed
            rospy.signal_shutdown("Quitting...")

        # find walls and add to image using Hough transformation
        findHoughLines()

        # call wall following algorithm
        wallFollow()

        # show image with range finder data and calculated walls
        cv.ShowImage("Ranges", D.image)

        # show image used in Hough transformation if option is selected
        if SHOW_HOUGH:
            cv.ShowImage("HoughLines", D.color_dst)

        # clear the images for next loop
        cv.Set(D.image, cv.RGB(0, 0, 0))
        cv.Set(D.hough, cv.RGB(0, 0, 0))

        # clear all the lists for next loop
        D.dangerList = []
        D.ahead = []
        D.behind = []
        D.left = []
        D.right = []

    D.tank(0, 0)  # stops the robot
    print "Quitting..."
예제 #29
0
def VirtualMirror():
    cv.NamedWindow("RGB_remap", cv.CV_WINDOW_NORMAL)
    cv.NamedWindow("Depth_remap", cv.CV_WINDOW_AUTOSIZE)
    cv.NamedWindow('dst', cv.CV_WINDOW_NORMAL)
    cv.SetMouseCallback("Depth_remap", on_mouse, None)
    print "Virtual Mirror"
    print "Calibrated 4 Screen corner= ", sn4_ref
    print "Corner 1-2 = ", np.linalg.norm(sn4_ref[0] - sn4_ref[1])
    print "Corner 2-3 = ", np.linalg.norm(sn4_ref[1] - sn4_ref[2])
    print "Corner 3-4 = ", np.linalg.norm(sn4_ref[2] - sn4_ref[3])
    print "Corner 4-1 = ", np.linalg.norm(sn4_ref[3] - sn4_ref[0])
    global head_pos
    global head_virtual
    global scene4_cross
    head_pos = np.array([-0.2, -0.2, 1.0])  #Head_detect()

    while 1:
        (depth, _) = freenect.sync_get_depth()
        (rgb, _) = freenect.sync_get_video()
        #print type(depth)
        img = array2cv(rgb[:, :, ::-1])
        im = array2cv(depth.astype(np.uint8))
        #modulize this part for update_on() and loopcv()
        #q = depth
        X, Y = np.meshgrid(range(640), range(480))
        d = 2  #downsampling if need
        projpts = calibkinect.depth2xyzuv(depth[::d, ::d], X[::d, ::d],
                                          Y[::d, ::d])
        xyz, uv = projpts

        if tracking == 0:
            #*********************************
            if pt is not None:
                print "=================="
                (x_d, y_d) = pt
                print "x=", x_d, " ,y=", y_d
                #print depth.shape
                #Watch out the indexing for depth col,row = 480,640
                d_raw = np.array([depth[y_d, x_d]])
                u_d = np.array([x_d])
                v_d = np.array([y_d])

                print "d_raw= ", d_raw
                print "u_d= ", u_d
                print "v_d= ", v_d
                head3D, head2D = calibkinect.depth2xyzuv(d_raw, u_d, v_d)
                print "XYZ=", head3D
                print "XYZonRGBplane=", head2D

                head_pos = head3D[0]
                #print "head_pos.shape",head_pos.shape
                print "head_pos= ", head_pos
                cv.WaitKey(100)
                cv.Circle(im, (x_d, y_d), 4, (0, 0, 255, 0), -1, 8, 0)
                cv.Circle(im, (int(head2D[0, 0]), int(head2D[0, 1])), 2,
                          (255, 255, 255, 0), -1, 8, 0)

            #*********************************
        elif tracking == 1:
            #find the nearest point (nose) as reference for right eye position
            print "nose"
            inds = np.nonzero(xyz[:, 2] > 0.5)
            #print xyz.shape
            new_xyz = xyz[inds]
            #print new_xyz.shape
            close_ind = np.argmin(new_xyz[:, 2])
            head_pos = new_xyz[close_ind, :] + (0.03, 0.04, 0.01)
            #print head_pos.shape
            #print head_pos

        elif tracking == 2:
            #find the closest point as eye posiiton
            print "camera"
            inds = np.nonzero(xyz[:, 2] > 0.5)
            #print xyz.shape
            new_xyz = xyz[inds]
            #print new_xyz.shape
            close_ind = np.argmin(new_xyz[:, 2])
            head_pos = new_xyz[close_ind, :]
            #print head_pos.shape
            #print head_pos

        else:
            print "please select a tracking mode"

        head_virtual = MirrorReflection(sn4_ref[0:3, :], head_pos)
        print "head_virtual= ", head_virtual

        rgbK = np.array([[520.97092069697146, 0.0, 318.40565581396697],
                         [0.0, 517.85544366622719, 263.46756370601804],
                         [0.0, 0.0, 1.0]])
        rgbD = np.array([[0.22464481251757576], [-0.47968370787671893], [0.0],
                         [0.0]])
        irK = np.array([[588.51686020601733, 0.0, 320.22664144213843],
                        [0.0, 584.73028132692866, 241.98395817513071],
                        [0.0, 0.0, 1.0]])
        irD = np.array([[-0.1273506872313161], [0.36672476189160591], [0.0],
                        [0.0]])

        mapu = cv.CreateImage((640, 480), cv.IPL_DEPTH_32F, 1)
        mapv = cv.CreateImage((640, 480), cv.IPL_DEPTH_32F, 1)
        mapx = cv.CreateImage((640, 480), cv.IPL_DEPTH_32F, 1)
        mapy = cv.CreateImage((640, 480), cv.IPL_DEPTH_32F, 1)

        cv.InitUndistortMap(rgbK, rgbD, mapu, mapv)
        cv.InitUndistortMap(irK, irD, mapx, mapy)

        if 1:
            rgb_remap = cv.CloneImage(img)
            cv.Remap(img, rgb_remap, mapu, mapv)

            depth_remap = cv.CloneImage(im)
            cv.Remap(im, depth_remap, mapx, mapy)

        scene4_cross = Cross4Pts.CrossPts(xyz, uv, head_pos, head_virtual,
                                          sn4_ref)
        #[warp] Add whole warpping code here
        #[warp] points = Scene4Pts() as warpping 4 pts
        #Flip the dst image!!!!!!!!!
        #ShowImage("rgb_warp", dst)

        #Within/out of the rgb range
        #Mapping Destination (width, height)=(x,y)

        #Warning: the order of pts in clockwise: pt1(L-T),pt2(R-T),pt3(R-B),pt4(L-B)
        #points = [(test[0,0],test[0,1]), (630.,300.), (700.,500.), (400.,470.)]
        points = [(scene4_cross[0, 0], scene4_cross[0, 1]),
                  (scene4_cross[1, 0], scene4_cross[1, 1]),
                  (scene4_cross[2, 0], scene4_cross[2, 1]),
                  (scene4_cross[3, 0], scene4_cross[3, 1])]
        #Warping the image without flipping (camera image)
        #npoints  = [(0.,0.), (640.,0.), (640.,480.), (0.,480.)]
        #Warping the image with flipping (mirror flip image)
        npoints = [(640., 0.), (0., 0.), (0., 480.), (640., 480.)]
        mat = cv.CreateMat(3, 3, cv.CV_32FC1)
        cv.GetPerspectiveTransform(points, npoints, mat)

        #src = cv.CreateImage( cv.GetSize(img), cv.IPL_DEPTH_32F, 3 )
        src = cv.CreateImage(cv.GetSize(rgb_remap), cv.IPL_DEPTH_32F, 3)
        #cv.ConvertScale(img,src,(1/255.00))
        cv.ConvertScale(rgb_remap, src, (1 / 255.00))

        dst = cv.CloneImage(src)
        cv.Zero(dst)
        cv.WarpPerspective(src, dst, mat)
        #************************************************************************

        #Remap the rgb and depth image
        #Warping will use remap rgb image as src

        if 1:
            cv.ShowImage("RGB_remap", rgb_remap)  #rgb[200:440,300:600,::-1]
            cv.ShowImage("Depth_remap", depth_remap)
            cv.ShowImage("dst", dst)  #warp rgb image

        if cv.WaitKey(5) == 27:
            cv.DestroyWindow("RGB_remap")
            cv.DestroyWindow("Depth_remap")
            cv.DestroyWindow("dst")
            break
예제 #30
0
cv.DrawContours (orig, contours, cv.RGB(0,255,0), cv.RGB(255,0,0), 2, 3, cv.CV_AA, (0, 0)) 

def contour_iterator(contour):
  while contour:
    yield contour
    contour = contour.h_next()

for c in contour_iterator(contours):
  # Number of points must be more than or equal to 6 for cv.FitEllipse2
  if len(c) >= 6:
    # Copy the contour into an array of (x,y)s
    PointArray2D32f = cv.CreateMat(1, len(c), cv.CV_32FC2)

    for (i, (x, y)) in enumerate(c):
      PointArray2D32f[0, i] = (x, y)

    # Fits ellipse to current contour.
    (center, size, angle) = cv.FitEllipse2(PointArray2D32f)

    # Convert ellipse data from float to integer representation.
    center = (cv.Round(center[0]), cv.Round(center[1]))
    size = (cv.Round(size[0] * 0.5), cv.Round(size[1] * 0.5))

    # Draw ellipse
    cv.Ellipse(orig, center, size, angle, 0, 360, cv.RGB(255,0,0), 2,cv.CV_AA, 0)

# show images
cv.ShowImage("image - press 'q' to quit", orig)
#cv.ShowImage("post-process", processed)
cv.WaitKey(-1)