How to output a mat object as an XYZ file with Python

asked 2017-07-28 17:42:55 -0500

I am writing a program that will do 3D sensing and I am currently at the last step. I have a grayscale 1280x720 image, and in order to create and visualize a point cloud, I need to convert it to an .XYZ format. I am doing this in Python, all I need is to create 3 arrays, one from 0-1279 (X), array 2 from 0-720 (Y), and array 3 with the respective grayscale value for that pixel at point (X,Y). Then I need to print these arrays into a text file in the following format:



X Y Z ...

In C I would just to for loops to extract the grayscale values of this object into a new 1-D array and just start writing this into the text file using variables to count up to 1280 and 720. This is trivial and very easy for me to do. However I just recently started using python and need to use python.if I do my approach it takes minutes to go through a big array like this in python. I understand that using the numpy library makes everything very efficient, so my question is how can I achieve this efficiently with python?

I have spent a good amount of time looking for examples or functions but have't found anything helpful and I don't have a lot of time left to finish this project.

Thank you in advanced,


edit retag flag offensive close merge delete


can you show, what you're doing now ?

berak gravatar imageberak ( 2017-07-30 09:16:39 -0500 )edit