0

I'm trying to output the ARVI band of an image to a JPEG from GRASS GIS. I need the image to be the same resolution, as I it will be used for creating label data for a ML model in LabelStudio.

I generated the image like this from NAIP data

i.vi viname=arvi output=m_3707612_sw_18_1_20140630.5 blue=m_3707612_sw_18_1_20140630.1 nir=m_3707612_sw_18_1_20140630.4 red=m_3707612_sw_18_1_20140630.3 --overwrite

I changed the colors slightly

r.colors map=m_3707612_sw_18_1_20140630.5@PERMANENT color=grey 

and this is what I am trying to export as a JPEG. I've tried using r.out.gdal, but I'm having a hard time, and the images are either only black/white (not greyscaled), or are a single value (128).

I think the problem has to do with the values that are being interpreted to grayscale in the range of values and translating that to an 8bit (255) image. Right now it is scaled from -1,0.25.. See this command:

r.univar -e m_3707612_sw_18_1_20140630.5@PERMANENT                              
total null and non-null cells: 47481490
total null cells: 0
Of the non-null cells:
----------------------
n: 47481490
minimum: -1
maximum: 0.25
range: 1.25
mean: -0.277477
mean of absolute values: 0.280471
standard deviation: 0.265399
variance: 0.0704366
variation coefficient: -95.6472 %
sum: -13175022.8683972
1st quartile: -0.527273
median (even number of cells): -0.13986
3rd quartile: -0.0487805
90th percentile: -0.00555556

I've tried to use r.rescale.eq, r.rescale to get the values scaled to 0-255 before export, but that doesn't seem to work. I was going to try r.mapcalc as well per this answer, but couldn't figure out how to make it work. I'm pretty stumped. It seems like the image I took a screenshot of should be able to be represented between 0 and 255 in a JPEG pretty well.

The image looks like this: enter image description here


Finally found something that works for my use case. Going to explore a little more to hopefully understand why an 8bit JPEG wasn't working.

r.out.png input=m_3707612_sw_18_1_20140630.5@PERMANENT output=/Users/nick/Desktop/arvi-m_3707612_sw_18_1_20140630.png compression=9

is pretty much what I want. This is likely my answer, though I was hoping for a JPEG.

1 Answer 1

0

You seem to be on the right track, but three comments along the way:

  1. All vegetation indicies are designed to have values between -1 and +1, so your result seems fine.

  2. If you still need your values from 0-255, then use r.mapcalc with an expression such as

    r.mapcalc expr="m_3707612_sw_18_1_20140630.rescale = ((m_3707612_sw_18_1_20140630.5 + 1) / 2) * 255"

  3. While jpeg does support single band greyscale, it's mostly used for three band color. PNG would work, or you can use the (lossless) tif format.

1
  • thank you! I appreciate the mapcalc example
    – Nick Brady
    Commented Jan 18, 2023 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.