2

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

LA Tran
  • 25
  • 5
  • 1
    Aren't DEMs usually 16-bit integers? – fmw42 Apr 28 '19 at 20:51
  • 1
    From https://en.wikipedia.org/wiki/USGS_DEM, it says `The elevations are contiguous; breaks or other discontinuities are expressed using "void" elevations of value -32767. Each elevation is described as a six-character readable integer occupying a fixed location in a block.` So I don't think you should be specifying it as uint8 – fmw42 Apr 28 '19 at 21:02
  • @fmw42 I agree with both your thoughts, Fred, yet the filesize does correspond to 4096x4096 of 8-bit data, rather than the 16-bit or ASCII formats you sensibly suggest. Mmmm... OP needs to check his sources, I think. – Mark Setchell Apr 29 '19 at 09:34
  • I figured out something, thank you @Mark Setchell – LA Tran May 12 '19 at 06:09

1 Answers1

2

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

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432