How do you use Planet Imagery in MATLAB?

Comments

5 comments

  • Lukas Müller

    Hi Michael,

    you can use the geotiffread function to read your images into the MATLAB workspace.

        Example: [A,R] = geotiffread('2141729_1156105_2019-02-22_1027_BGRN_Analytic.tif');

    A is the image as an M-by-N-by-4 array. The 4 layers are the different bands of the satellite image, i.e. blue, green, red and near-infrared. Sometimes the fourth layer is a data mask. This depends on the type of asset you downloaded.

     

    R is a MATLAB spatial referencing object. R can be used to display the image georeferenced to map coordinates with the mapshow function.

        Example: mapshow(A(:,:,1),R)

    This displays the first layer (blue band) of A georeferenced to the map coordinates stored in R.

     

    Alternatively, you can use imread and imshow to read and display the image file. However, if you use these functions, the spatial information is lost.

     

    You can now access the image data as with any numeric MATLAB array. 

        Example: Red = A(:,:,3);

    This extracts the third layer (red band) and assings it to the new variable Red.

     

    Combine different bands to an RGB image using the cat function.

        Example: RGB = cat(3,Red,Green,Blue);

     

    To read the JSON files, use the fileread function and the jsondecode function.

    fileread returns the contents of a file as a character array. jsondecode converts the character array to a MATLAB structure array.

        Example: metadata = jsondecode(fileread('2141729_1156105_2019-02-22_1027_metadata.json'));

    0
    Comment actions Permalink
  • Eli Perez

    Hi, I have been trying to use geotiffread to read planet images but I get the error "Error using geotiffinfo>readinfo (line 278)
    Multiple images exist in the file and their sizes are different". I have used imread and it works, however I do not know why neither geotiffread or geotiffinfo work. Do you know what can I do?

    1
    Comment actions Permalink
  • Lukas Müller

    Hi Eli,

    Yeah, I had that problem before. May I ask which Matlab version you're using? I got this error with version R2018b, but they fixed it in R2019a, I think. So I'd suggest you upgrade to R2019a and see if it works. EDIT: That doesn't work. See the comments below.

    If this doesn't work or you're already using version R2019a, there is a workaround where you modify the geotiffinfo function by adding two lines of code. You can read this blog post by Yu Jiang for detailed instructions.

    I hope this helps. Let me know if it worked.

    0
    Comment actions Permalink
  • Eli Perez

    Hi Lukas, thanks for your answer. I tried with Matlab R2018a and R2018b. Unfortunately we still do not have R2019a license. When I use imread, I have to specify the image index, for example, imread('Name.tif',1) or imread('Name.tif',2) and in each options it reads a NxNx4 matrix with different NxN resolution for each index. Basically each TIFF image has 16 images grouped by 4. What I did is to batch clip in ArcMap all the images to the desired location and then read the TIFF again in Matlab. It worked. geotiffread loaded correctly the clipped TIFF images. Cheers.

    1
    Comment actions Permalink
  • Lukas Müller

    Sorry, I think I got something mixed up.

    As you said, the error occurs because the scene is stored as 4 different images with different resolutions in one tif file. I've just tried to load one of these files and got the same error even with MATLAB version R2019a.

    Seems like you get these GeoTIFFs with multiple images when you download scenes as a whole. The reason I don't get this error anymore is not because of MATLAB version R2019a, but because I use Planet's server-side clipping service. It's a tool of the new Orders v2 API that clips the scene to a dersired extent before downloading it. The output of this clipping operation is only one image in one tif file. You can load these tif files with geotiffread without any problems. I guess the result is similar to your solution with ArcMap.

    I also tried Yu Jiang's workaround where you force MATLAB to read only the first image of a multi-image GeoTIFF, and it works perfectly. I think that's the best solution to your problem :) Cheers, Lukas

    0
    Comment actions Permalink

Please sign in to leave a comment.