question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Working with multiband hyperspectral images in ENVI BIL format (raw, hdr). Saving bands to 2D image.

See original GitHub issue

Hi,

I am beginning to learn more about net-vips and starting to use it more for various projects. So far mainley worked with regular 2d images and I find the performance and mamory usage amazing.

I have recently started to learn about hyperspectral images (HSI) but due to a lack of information having a hard time figuring out how to get started with net-vips.

I’m fairly new to net-vips and HSI so please go easy on me if get the terminology wrong.

I have a sample HSI image in ENVI BIL (Band-interleaved-by-line) format (.raw and .hdr - header file). I am trying to learn how to do some basic operations on it e.g.: extracting bands to 2d images, joining bands and saving them to 2d images.

My header file gives me information about the file/format:

ENVI
samples = 2883
lines = 4094
bands = 306
header offset = 0
file type = ENVI Standard
data type = 4
interleave = bil
wavelength units = nm
binning = {4,4}
wavelength = {"list of wavelengths here"}

I did try to use the Rawload() and started reading the documentation but I’m stuck. Should I be looking at Enums.Interpretation and Enumbs.BandFormat?

  static void Main(string[] args)
        {

            string selectedFile = @"C:\hsi.raw";
            int datatype = 4;    // Floating - point: 32 - bit single - precision
            int samples = 2883;  // width
            int lines = 4094;    // height
            int bands = 306;     // number of bands (channels)


            using (var image = Image.Rawload(selectedFile, samples, lines, bands, access: Enums.Access.Sequential))
            {
                var splited = image.Bandsplit();
                int counter = 0;
                foreach (var im in splited)
                {
                    im.WriteToFile("img" + counter + ".jpg");
                    counter++;
                }
            }

        }

 static void Main(string[] args)
       {
           string selectedFile = @"C:\hsi.raw";
           int datatype = 4;    // Floating - point: 32 - bit single - precision
           int samples = 2883;  // width
           int lines = 4094;    // height
           int bands = 306;     // bands (channels)

           using (var image = Image.Rawload(selectedFile, samples, lines, bands, access: Enums.Access.Sequential))
           {
               image[0].WriteToFile("test.jpg");
           }
       }

test

I have also tried to do this manually with FileStream, calculating positions, using Buffer.BlockCopy and attempting to get an image from a byte array.

I was wondering if someone with more experience in the subject could help me get started/give some examples if possible?

Many thanks.

EDIT: I have managed to get some results to extract one band manually: using FileStream, setting img dimensions and positions of bands then looping while doing BlockCopy to float[]. Eventually converting float[] to byte[] then using Net-Vips Image.FromMemory to save as jpg.

Still trying to figure out if this could be more straightforward with net-vips? (like using the Rawload and then working on bands).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
jcupittcommented, Nov 25, 2021

It looks like the sample image uses 0 - 1 for black to white, so I think adding a *256 will fix it.

I had a go in python:

#!/usr/bin/python3

import sys
import pyvips

# read a band-interleaved-by-line image

samples = 460
lines = 285
bands = 306

# load as a huge one-band image and chop into bands in the loop below
# "scrgb" uses 0-1 for black to white, which is convenient for us
raw = pyvips.Image.rawload("bil/stitchedRefl_CROPPED4.raw",
                           samples, lines * bands, 1, 
                           format="float")

bands = [raw.crop(0, b, raw.width, raw.height - b).subsample(1, bands)
         for b in range(bands)]

# join the bands up
combined = bands[0].bandjoin(bands[1:])

# the iamge uses 0 - 1 for black to white ... convert to 0 - 255 uint8 for
# convenience
combined = (combined * 256).cast("uchar")

print("writing hyperspectral x.tif ...")
combined.write_to_file("x.tif")

# also save an RGB image as a sample ... these are the 450, 550 and 
# 750nm bands
print("writing RGB x.jpg ...")
combined[28].bandjoin([combined[80], combined[181]]).write_to_file("x.jpg")

It makes this:

x

I suppose we’d need to add something about the SPD to fix the blueness.

(saving as .v and then using vipsdisp or nip2 to examine the file is an easy way to find things like this)

0reactions
kleisaukecommented, Jul 25, 2022

I hope this information helped. Please feel free to re-open if questions remain.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading a RAW file from a hyperspectral camera using ...
I have captured and stored data that comes from a hyperspectral camera. This results in 2 files: ''example_file.raw''. ''example_file.hdr''.
Read more >
Combining hyperspectral bands to form a multispectral ...
What I would like to do is to convert the hyperspectral data to a multi one. For example, given 200 images(bands) from visible...
Read more >
ENVI Tutorials
NCSA Hierarchical Data Format (HDF) Software Library and Utilities ... you to work with entire image files, individual bands, or both. When an...
Read more >
Getting Started in ENVI
The ENVI National Imagery Transmission Format (NITF) and NATO Secondary. Image Format (NSIF) Module supports reading and writing image files in the NITF....
Read more >
Read ENVI hyperspectral images #441
We will add a new method for reading images files to support ENVI-type hyperspectral data. Our plan is to use the BIL filename...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found