Export PImage file to JSON

Hi! I have a short sketch that reads the input from a kinect file, calculates colors, and prints out the resulting array on screen. All information is saved on a PImage file type. It’s pretty small as the resolution of the kinect is only 320x240 pixels
I would like to save this PImage to a file and maybe json is a nice file format, but I can’t find the code for this. I’ve seen code for saving a JsonObject but my PImage is not that file type.
Any ideas on how can I do it?
Thanks!

Maybe I’m misunderstanding your intentions here, but why not just save the PImage as an image?

What information are you after (and how are you intending to further use it)? The pixels that make up an image? Or some quantitative information extracted from all the color values?

It’s not your fault, I failed to provide some information.
Each pixel on this image actually stores a z-axis depth, the Kinect sensor gives out distance for each X-Y pixel and it is stored in the Image array. I updatePixels with a color that depends on the depth of the pixel, so it’s like a colored depth map.
The goal of the file is to keep a record of the measured image, but not as a displayable image, but as a file that holds each height for every pixel. Im not sure yet how I will process the file afterwards, and we may not get involved in that stage, as this is an R&D project. For now I need to export the heights file to some format I create.
I thought of a JSON file as it is an open file type that can be processed by all programming languages directly
Thanks

1 Like

Ah, yes, the hint was in “Kinect”. That’s all about depth information.
I’m not very knowledgeable on JsonObjects, but I believe it indeed reasonable to use those.

1 Like

Hello @edugimeno,

You can certainly save as a PNG (not a lossy algorithm) and extract the data.
Don’t save as a JPG which is a lossy algorithm!

Example:

/*
 Project:   Save data in a PNG
 Author:    GLV
 Date:      2024-11-12
 Version:   1.0.0
*/

// Reference:
// https://stackoverflow.com/questions/13162839/kinect-depth-image

PImage kin = createImage(320, 240, RGB);

int cols = 320;
int rows = 240;

size(400, 400, P3D);
background(255);

float angleY = TAU/90;
float angleX = TAU/90;

kin.loadPixels();

for (int y = 0; y< rows; y++)
  {
  for (int x = 0; x < cols; x++)
    {
    int loc = x + y*cols;
    float z1 = sin(x*angleX) + sin(y*angleY);  // Range is -2, 2
    int data = (int) map(z1, -2, 2, 0, 2047);
    kin.pixels[loc] = data;
    }
  }

kin.save("data.png");

PImage data = loadImage("data.png");

//image(data, 0, 0);  // If you really want to "see" it!

lights();
translate(width/2, height/2, -150);
rotateX(TAU/8);

for (int y = 0; y< data.height; y+=10)
  {
  for (int x = 0; x < data.width; x+=10)
    {
    int loc = x + y*data.width;
    
    //stroke(255, 0, 0);
    //strokeWeight(5);
    //point (x-160, y-100, kin.pixels[loc]/20+20);

    pushMatrix();
    translate(x-160, y-100+40, (kin.pixels[loc]/20)/2);
    //noStroke();
    fill(128, 255, 0);
    box(10, 10, kin.pixels[loc]/20);
    popMatrix();
    }
  }

Or save and load as a data file with:

I used this for the data:
3D Mesh of Sine Waves from Coding Challenge

:)

Awesome! So the first example kind of renders the depth array as a 3D image?? That’s great!!
Ok I will explore all the suggested options!
Thanks!!

That it does!

I stored the data in a PNG and retrieved it, processed it (with Processing!) and displayed it!

Once you get the data to Processing you can visualize any way you can imagine:
Processing + Arduino Force Sensor Visualizing - #3 by glv

:)

1 Like