Recent

Author Topic: [SOLVED] Tiff 16 bit grayscale pixel access?  (Read 17869 times)

Frate

  • New Member
  • *
  • Posts: 30
[SOLVED] Tiff 16 bit grayscale pixel access?
« on: May 18, 2021, 12:21:44 am »
Hello,
I'm working on a project with DEM tiff data and i need to access pixel color to retrieve elevation value of mutiple points.
I was successfull in loading the tiff image onto bitmap using this example https://bugs.freepascal.org/file_download.php?file_id=28524&type=bug
 
The problem is that i'm not able to get the correct color value from bitmap pixel access.
The tiff image i'm trying to read is 16 bit grayscale, so i'm using Lsa color space.
The issue present itself as following:
using photoshop on a pixel i get a reading of L = 2, while using TColorToFPColor(newImage.Canvas.Pixels[x,y]).ToLabA.L I get L = 0.008 .

What is going on? Do I need to set the colorspace on the bitimap in advance?

Actually I dont need to display the image at all, i just need to do calculations, so skipping the bitmap step would be good. Is it possible to use FPReadTiff directly to get a buffer of the pixels? (Documentation on this?)

I hope I could exlain mysef clearly enough.

Any help/suggestion appreciated

Thanks

Frate
« Last Edit: May 21, 2021, 11:55:53 am by Frate »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Tiff 16 bit grayscale pixel access?
« Reply #1 on: May 18, 2021, 07:54:16 pm »
Notice that 2/256=0.0078125 ~0.008

According to the comments in FPReadTiff:
Quote
ToDo:
   Compression: FAX, Jpeg...
   Color format: YCbCr, Lab...
   PlanarConfiguration: 2 (one chunk for each channel)
   bigtiff 64bit offsets
   XMP tag 700
   ICC profile tag 34675

and

Code: Pascal  [Select][+][-]
  1.     case IFD.PhotoMetricInterpretation of
  2.     0,1: // 0:bilevel grayscale 0 is white; 1:0 is black
  3. ...
  4.     2: // RGB(A)
  5. ...
  6.     3: //3 Palette/color map indexed
  7. ...
  8.     //4 Mask/holdout mask (obsolete by TIFF 6.0 specification)
  9.  
  10.     5: // CMYK plus optional alpha
  11. ...
  12.  
  13.      //6: YCBCR: CCIR 601
  14.      //8: CIELAB: 1976 CIE L*a*b*
  15.      //9: ICCLAB: ICC L*a*b*. Introduced post TIFF rev 6.0 by Adobe TIFF Technote 4
  16.      //10: ITULAB: ITU L*a*b*
  17.      //32844: LOGL: CIE Log2(L)
  18.      //32845: LOGLUV: CIE Log2(L) (u',v')
  19.     else
  20.       TiffError('PhotometricInterpretation='+IntToStr(IFD.PhotoMetricInterpretation)+' not supported');
  21.     end;

You might want to give BGRABitmap library a try. Read this post for instance.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Tiff 16 bit grayscale pixel access?
« Reply #2 on: May 18, 2021, 10:39:36 pm »
Hi!

As far as I know there is no simple solution to read 16 bit grayscale GeoTiffs.

The Gimp moans something about unknown Tags, BGRAbitmap is not prepared for that, the LCL not. This is a special enhancement of arcGis and so you should search at arcGIS to find some software to get the height level depending on the 16-bit-Grayscale.

I dont know any library or software which is ready for this exotic enhancement.

Winni

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Tiff 16 bit grayscale pixel access?
« Reply #3 on: May 18, 2021, 11:26:47 pm »
Hi!

I am wrong. Besides all that Python, arcGIS and other rubbish there is still our beloved tool ImageMagic.

I did not know that ImageMagic can do that. Converting 16-Bit-Grayscale-Tiff to png.

* Solution 1: Install ImageMagic on your machine and then:

Code: Bash  [Select][+][-]
  1. convert infile.tif -depth 16 -type Grayscale outfile.png


* Solution 2:

Install PascalMagic on your Lazarus and do the same:

https://wiki.freepascal.org/PascalMagick


Winni

 

Frate

  • New Member
  • *
  • Posts: 30
Re: Tiff 16 bit grayscale pixel access?
« Reply #4 on: May 19, 2021, 03:07:10 pm »
Notice that 2/256=0.0078125 ~0.008

Thanks engkin, I'll take a look at cosintency on that calculation.

You might want to give BGRABitmap library a try. Read this post for instance.

Good news, seems like they have some sort of Tiff Lab support!

I did not know that ImageMagic can do that. Converting 16-Bit-Grayscale-Tiff to png.

Thanks Winni,
I didnt want to convert to another format because i may loose detail (maybe not?).
If i have no other solution I will batch convert al data to some format i can read easily.
Its a pretty big dataset around 300 GB so if i can minimize the work on it I'm happy ;)

I'll be doing my homeworks and let you know.

Frate
« Last Edit: May 20, 2021, 12:14:23 pm by Frate »

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Tiff 16 bit grayscale pixel access?
« Reply #5 on: May 19, 2021, 03:24:20 pm »
Did you try the large imaging libraries?

- FreeImage: https://freeimage.sourceforge.io/
- Vampyre Imaging: https://github.com/galfar/imaginglib or Online-Package-Manager

They support a huge selection of image formats. Vampyre directly supports Lazarus, FreeImage has a Delphi wrapper which works in Lazarus as well (if I am wrong here ask again because I once used FreeImage in a project, and I certainly can post the Lazarus version of the wrapper unit).
« Last Edit: May 19, 2021, 06:16:26 pm by wp »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Tiff 16 bit grayscale pixel access?
« Reply #6 on: May 19, 2021, 06:05:57 pm »
Hi!

GeoTiff is an enhancement by the Geo fraggles to the Tiff specification.
BGRAbitmap cannot do it, even ImageMagic fails although they can read 16 bit grayscal.

The only source to read the GeoTiffs is from the inventors of that specialized format themselves. This is GDAL and here are the sources:

https://github.com/OSGeo/GDAL

Everything in C.

Winni

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Tiff 16 bit grayscale pixel access?
« Reply #7 on: May 19, 2021, 07:05:06 pm »
It seems to me he is after Lab color space, not the meta data. Specifically the L value of each pixel

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Tiff 16 bit grayscale pixel access?
« Reply #8 on: May 19, 2021, 08:00:46 pm »
Hi!

Unless it is not quiet clear what Frate wants perhaps this canadian page about DEM helps him

https://library.carleton.ca/help/dem-formats

Winni

Frate

  • New Member
  • *
  • Posts: 30
Re: Tiff 16 bit grayscale pixel access?
« Reply #9 on: May 20, 2021, 12:13:48 pm »
It seems to me he is after Lab color space, not the meta data. Specifically the L value of each pixel

Yep, i need to get the correct pixel color to be used in elevation calculations. Lab color space seemed the best option.
I can even go to other color spaces (needs to be 16-bit) as long as no conversion math is happening and I get in some way the actual pixel value.

This is what the DEM data docs says about the format : "DEM: GeoTiff, signed 16 bits, and 1m/DN for DEM;"

Unfortunately this trick is not working
Quote
Notice that 2/256=0.0078125 ~0.008

imaginglib is not recognizing this specific Tif format... and reads as RGBA...
Code: Pascal  [Select][+][-]
  1.    
  2. if DataFormat = ifUnknown then
  3.     begin
  4.       // Use RGBA interface to read A8R8G8B8 TIFFs and mainly TIFFs in various
  5.       // formats with no Imaging equivalent, exotic color spaces etc.  
  6.  


and yes, i don't care about the geotif tags.

I have yet to try other solutions you mentioned.

Frate

Frate

  • New Member
  • *
  • Posts: 30
Re: Tiff 16 bit grayscale pixel access?
« Reply #10 on: May 20, 2021, 12:45:03 pm »
with BGRABitmap...
DecompressLZW goes wrong
« Last Edit: May 20, 2021, 12:52:55 pm by Frate »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Tiff 16 bit grayscale pixel access?
« Reply #11 on: May 20, 2021, 12:53:43 pm »
with BGRABitmap...
DecompressLZW goes wrong

I told you that before

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Tiff 16 bit grayscale pixel access?
« Reply #12 on: May 20, 2021, 12:56:41 pm »
Hello,
I'm working on a project with DEM tiff data and i need to access pixel color to retrieve elevation value of mutiple points.
I was successfull in loading the tiff image onto bitmap using this example https://bugs.freepascal.org/file_download.php?file_id=28524&type=bug
 
The problem is that i'm not able to get the correct color value from bitmap pixel access.
The tiff image i'm trying to read is 16 bit grayscale, so i'm using Lsa color space.
I am confused by this specification: you say the image is 16-bit grayscale - I assume that the pixels are 16-bit grayscale values, but you are talking of "color space". How does this fit together?

Can you upload one of these images, or at least provide us a link to get one of them?

Frate

  • New Member
  • *
  • Posts: 30
Re: Tiff 16 bit grayscale pixel access?
« Reply #13 on: May 20, 2021, 02:24:28 pm »
Hello,
I'm working on a project with DEM tiff data and i need to access pixel color to retrieve elevation value of mutiple points.
I was successfull in loading the tiff image onto bitmap using this example https://bugs.freepascal.org/file_download.php?file_id=28524&type=bug
 
The problem is that i'm not able to get the correct color value from bitmap pixel access.
The tiff image i'm trying to read is 16 bit grayscale, so i'm using Lsa color space.
I am confused by this specification: you say the image is 16-bit grayscale - I assume that the pixels are 16-bit grayscale values, but you are talking of "color space". How does this fit together?

Can you upload one of these images, or at least provide us a link to get one of them?

The thing with colorspace is that when i try to get pixel value from an image raster it is returned not as a raw pixel value (which I'm trying to get) but as color in whatever colorspace RGBA, FPcolor etc...
This make some conversion in between which i dont want.

This is a sample tile (maybe you need to register, so i'll upload one to OneDrive)
https://data.lpdaac.earthdatacloud.nasa.gov/lp-prod-protected/ASTGTM.003/ASTGTMV003_N45E012_dem.tif
https://1drv.ms/u/s!Ao9fJmV_S5FXiZYTnqNXlSW1qL9-pw?e=sby5Iq

This is documentation with tiles specs
https://ssl.jspacesystems.or.jp/ersdac/GDEM/ver3Validation/GDEM3_QuickGuide.pdf

I think at this time I will convert the database to another raw file format and work with that.

Frate

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Tiff 16 bit grayscale pixel access?
« Reply #14 on: May 20, 2021, 05:22:24 pm »
IrfanView was the only program on my PC to display this file. However, a Lazarus program based on the freeimage.dll is able to display something too, although it is in some kind of false colors - but at least it is something. I don't want to spend too much time in this, but when you play with freeimage you can probably find settings which show the image correctly.

My demo program is in the attachment. If you are on Windows you can download the pre-compiled freeimage.dll from https://freeimage.sourceforge.io/download.html; copy it into the folder with the exe of the demo program.

 

TinyPortal © 2005-2018