Skip to content

Instantly share code, notes, and snippets.

@zarelit
Created December 7, 2015 19:02
Show Gist options
  • Save zarelit/b430748614d813e77614 to your computer and use it in GitHub Desktop.
Save zarelit/b430748614d813e77614 to your computer and use it in GitHub Desktop.
Haskell snippet to convert depth maps took from kinect to meshlab compatible point clouds
-- takes a PGM file and outputs an ASCII list of triplets (coordinates)
-- in order to import a kinect depth map in meshlab as a point cloud
import Data.Char(toLower)
main = interact pgm2triplets
pgm2triplets :: String -> String
pgm2triplets whole = unlines $ map showTriplet noHoles
where tokens=words whole
height=read$tokens!!2 :: Int
width=read$tokens!!1 :: Int
depth=read$tokens!!3 :: Int
points=drop 4 tokens
pointsInt = map (read) points :: [Int]
numOfPoints=width*height
pointsWithIndex = zip [0 .. numOfPoints] pointsInt
-- Pay attention to the Axes system. Depth and Height are inverted
triplets=map (\(i,v)->(i `mod` width,height - i `div` height,depth - v)) pointsWithIndex
noHoles = filter (\(_,_,x)->x/=0) triplets
showTriplet (x,y,z) = show x ++ " " ++ show y ++" " ++show (z `div` 8) ++ "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment