image-processingcomputer-visionrawimage

How can display a Digital Elevation Model (DEM) (.raw) using Python?


I want to display a DEM file (.raw) using Python, but there may be something wrong with the result.

Below is my code:

img1 = open('DEM.raw', 'rb')
rows = 4096
cols = 4096

f1 = np.fromfile(img1, dtype = np.uint8, count = rows * cols)
image1 = f1.reshape((rows, cols)) #notice row, column format
img1.close()

image1 = cv2.resize(image1, (image1.shape[1]//4, image1.shape[0]//4))
cv2.imshow('', image1)
cv2.waitKey(0)
cv2.destroyAllWindows()

And I got this result: display result

The original DEM file is placed here: DEM.raw


Solution

  • There's nothing wrong with your code, that's what's in your file. You can convert it to a JPEG or PNG with ImageMagick at the command line like this:

    magick -size 4096x4096 -depth 8 GRAY:DEM.raw result.jpg
    

    And you'll get pretty much the same:

    enter image description here

    The problem is elsewhere.

    Taking the hint from Fred (@fmw42) and playing around, oops I mean "experimenting carefully and scientifically", I can get a more likely looking result if I treat your image as 4096x2048 pixels with 16 bpp and MSB first endianness:

    magick -size 4096x2048 -depth 16 -endian MSB gray:DEM.raw  -normalize result.jpg 
    

    enter image description here