def distance_skeleton(self, img): # Ein Zielbild bereitstellen, um das Ergebnis der Distance # Transformation zu speichern dist_img = cv.CreateImage(cv.GetSize(img), 32, 1) # Distanztransformation cv.DistTransform(img, dist_img, distance_type=cv.CV_DIST_L2) # Konvertierung von CV-Bildobjekt zu Numpy-Array dist_img_mat = image_conversion.cv2array(dist_img) # Normalisierung der Distance Map max_of_dist = dist_img_mat.max() dist_img_mat = dist_img_mat / max_of_dist # Zurueck konvertieren dist_img = image_conversion.array2cv(dist_img_mat) # Erste Stufe fuer das Pruning: Gradientbild berechnen und # segmentieren des Gradientenbildes dist_gradient = self.pruning(dist_img, 1) dist_gradient_mat = image_conversion.cv2array(dist_gradient) dist_img_mat = image_conversion.cv2array(dist_img) # Zweite Stufe fuer das Pruning: Differenzbild aus Distanzbild und # segmentiertem Gradientenbild diff = dist_img_mat - dist_gradient_mat diff = diff * 1.0 diff_img = image_conversion.array2cv(diff) return diff_img, dist_img
def run(algorithm): # Schwellwerte fuer die Segmentierung des Spielers threshold_value = 40 depth_value = 170 skeleton = skeletonization.Skeleton() while True: # Tiefenbild erzeugen depthvalues = depth.get_better_depth() # Spielersegmentierung depth_seg, depth_image = player_segmentation.player_segmentation(depthvalues, threshold_value, depth_value) # Glaetten des segmentierten Bildes cv.Smooth(depth_seg, depth_seg, smoothtype=cv.CV_MEDIAN, param1=5, param2=5) # Distance Map berechnen und das Skelett daraus extrahieren if algorithm == "distancetransform": diff_img, dist_gradient = skeleton.distance_skeleton(depth_seg) diff = image_conversion.cv2array(diff_img) diff = 255.0 * numpy.logical_and(diff >= 0.2, diff <= 1) diff = diff.astype(numpy.uint8) diff_img = image_conversion.array2cv(diff) # Skelettierung mittels Thinning. Innerhalb von Python-Wrapper # wird ein C++-Programm aufgerufen, in dem das Thinning # implementiert ist. elif algorithm == "thinning": greyscale_array = image_conversion.cv2array(depth_seg) new_g = greyscale_array.reshape((480, 640)) new_g = pythonWrapper.reflectimage_band(new_g, 1) # Anzeige des Spielers (segmentiert) und des Distanz-Skeletts cv.ShowImage("Spieler", depth_seg) cv.ShowImage("Distanz-Skeltt", diff_img) if cv.WaitKey(10) == 27: break
def get_better_depth(): depth = get_depth() #Tiefenwert gleich 255. Dies ist der Fall, wenn die Tiefe von der #Kinect nicht gemessen werden kann. Diese Werte werden auf null gesetzt. depth = numpy.where(depth == 255, 0, depth) depth_img = image_conversion.array2cv(depth) #Glätten des Tiefenbildes cv.Smooth(depth_img, depth_img, smoothtype=cv.CV_MEDIAN, param1=5, param2=5) return depth_img
def test(image): grey_img = cv.CreateImage(cv.GetSize(image),8,1) dist_img = cv.CreateImage(cv.GetSize(image),32,1) cv.CvtColor(image,grey_img,cv.CV_BGR2GRAY) #Invertierung der Farbe, damit Objekt weiss und Hintergrund schwarz grey_img_mat = image_conversion.cv2array(grey_img) for i in xrange(len(grey_img_mat[:,1])): for j in xrange(len(grey_img_mat[1,:])): if grey_img_mat[i,j] == 255: grey_img_mat[i,j] = 0 else: grey_img_mat[i,j] = 1 grey_img = image_conversion.array2cv(grey_img_mat) #Distance Transform dist_img, dist_map = skeleton.distance_skeleton(grey_img) #Pruning/Segmentierung des Skeletts dist_gradient = skeleton.pruning(dist_img,1) dist_gradient_thresh_mat = image_conversion.cv2array(dist_gradient) #Differenz der Distance-Map und dem segmentierten Gradientbild dist_img_mat = image_conversion.cv2array(dist_img) diff = dist_img_mat - dist_gradient_thresh_mat #Schwellwertbasierte Segmentierung des Differenzbildes. Ergibt ein schoenes Skelett diff = 255.0 * numpy.logical_and(diff >= 0.2, diff <=1) #diff = diff.astype(numpy.float32) diff = diff.astype(numpy.uint8) diff_img = image_conversion.array2cv(diff) features = skeleton_improvement.calcGoodFeatures(diff_img) #return features, image, dist_img, diff_img, drawImage return diff_img, features, dist_gradient, dist_map
def pruning(self, skeleton_img, sigma): skeleton_img_mat = image_conversion.cv2array(skeleton_img) # Ausgabe-Array fuer das Ergebnis der Gradientberechnung gradient_output = numpy.empty_like(skeleton_img_mat) # Gradienten-Berechnung ndimage.gaussian_gradient_magnitude(skeleton_img_mat, sigma, gradient_output) # Normalisierung gradient_output /= gradient_output.max() # Array ins Bild umwandeln grad_img = image_conversion.array2cv(gradient_output) # Schwellwertbasierte Segmentierung des Gradientbildes dist_gradient_thresh = cv.CreateImage(cv.GetSize(grad_img), 8, 1) cv.InRangeS(grad_img, 0.6, 1, dist_gradient_thresh) return dist_gradient_thresh
img = cv.LoadImage(os.path.join(workdir,i)) grey_img = cv.CreateImage(cv.GetSize(img),8,1) bin_img = cv.CreateImage(cv.GetSize(img),8,1) dist_img = cv.CreateImage(cv.GetSize(img),32,1) cv.CvtColor(img,grey_img,cv.CV_BGR2GRAY) #Invertierung der Farbe, damit Objekt weiss und Hintergrund schwarz grey_img_mat = image_conversion.cv2array(grey_img) for i in xrange(len(grey_img_mat[:,1])): for j in xrange(len(grey_img_mat[1,:])): if grey_img_mat[i,j] == 255: grey_img_mat[i,j] = 0 else: grey_img_mat[i,j] = 1 grey_img = image_conversion.array2cv(grey_img_mat) #Distance Transform dist_img = skeleton.distance_skeleton(grey_img) #Pruning/Segmentierung des Skeletts dist_gradient = skeleton.pruning(dist_img,1) dist_gradient_thresh_mat = image_conversion.cv2array(dist_gradient) #Differenz der Distance-Map und dem segmentierten Gradientbild dist_img_mat = image_conversion.cv2array(dist_img) diff = dist_img_mat - dist_gradient_thresh_mat #Schwellwertbasierte Segmentierung des Differenzbildes. Ergibt ein schoenes Skelett #diff = 1.0 * numpy.logical_and(diff >= 0.2, diff <=1) diff_img = image_conversion.array2cv(diff) cv.ShowImage('Originalbild', img) cv.ShowImage('Distance Map',dist_img)
def multipleCalls(num,features,img): i = 0 while i < num: skeleton_improvement.startConnect(features,20,img) i = i + 1 img = cv.LoadImage("person.jpg") #img2 = cv.LoadImage("person.jpg") #corners1, image1, dist_img1, diff_img1, drawImage = test(img) #corners2, image2, dist_img2, diff_img2 = test(img2) diff_img, features, dist_gradient, dist_img = test(img) skeleton_improvement.drawFeatures(features,diff_img) #comparison.connectFeatures(diff_img,features,10) #neighbours = skeleton_improvement.startConnect(features,20,img) cProfile.run('skeleton_improvement.startConnect(features,20,img)') cProfile.run('test(img)') #skeleton_improvement.connectDFS(features) print img.depth diff_img_arr = image_conversion.cv2array(diff_img) diff_img = image_conversion.array2cv(diff_img_arr) cv.ShowImage("Diff-Image", diff_img) cv.ShowImage("Image",img) cv.SaveImage("hand-bfs.png",img) cv.ShowImage("Gradient", dist_gradient) cv.ShowImage("Distance", dist_img) cv.WaitKey()