convert_image_to_point_cloud.py
Go to the documentation of this file.
00001 #/usr/bin/env python
00002 
00003 import sys
00004 import Image
00005 
00006 # Settings:
00007 # Color of background pixels
00008 backgroundpixel = (255, 255, 255)
00009 # scale the point cloud to fit inside (-1,-1) - (1,1)
00010 scaling = True 
00011 
00012 im = Image.open (sys.argv[1])
00013 
00014 # resulting point cloud vector
00015 points = []
00016 
00017 # minimal/maximal pixel positions that contain non-background
00018 minimum = [d for d in im.size]
00019 maximum = [0, 0]
00020 
00021 for x in range (im.size[0]):
00022   for y in range (im.size[1]):
00023     px = im.getpixel((x,y))
00024     if (px != backgroundpixel):
00025       points.append ([x, y, 0, px[0], px[1], px[2]])
00026       if maximum[0] < x:
00027         maximum[0] = x
00028       if minimum[0] > x:
00029         minimum[0] = x
00030       if maximum[1] < y:
00031         maximum[1] = y
00032       if minimum[1] > y:
00033         minimum[1] = y
00034 
00035 size_x = maximum[0] - minimum[0]
00036 size_y = maximum[1] - minimum[1]
00037 
00038 if (size_x > size_y):
00039   scale = [1.0, float (size_y) / size_x]
00040 else:
00041   scale = [float (size_x) / size_y, 1.0]
00042 
00043 if scaling:
00044   #aspect_ratio = 
00045   for p in points:
00046     for d in range(2):
00047       p[d] = scale[d] * ((float (p[d]) - minimum[d]) / (maximum[d] - minimum[d]) * 2.0 - 1.0)
00048 
00049 print "VERSION .7"
00050 print "FIELDS x y z rgb "
00051 print "SIZE 4 4 4 4"
00052 print "TYPE F F F F"
00053 print "COUNT 1 1 1 1"
00054 print "WIDTH %i" % len (points)
00055 print "HEIGHT 1"
00056 print "VIEWPOINT 0 0 0 1 0 0 0"
00057 print "POINTS %i" % len (points)
00058 print "DATA ascii"
00059 
00060 for p in points:
00061   print p[0], p[1], p[2], p[3]<<16 | p[4]<<8 | p[5]


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:22:55