Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: tuffi on June 09, 2016, 04:29:47 PM

Title: import csv image in photoscan
Post by: tuffi on June 09, 2016, 04:29:47 PM
Dear friends,
I need help for analyze some thermal image dataset.
I have some thousand of .CSV matrix that are a 382x288 array, where every cells are a temperature in celsius with one or two decimal data.
I need to make a photogrammetric process to my data, so i need to convert my CSV data into TIFF image (16bit - the sensor works at 12bit). So:
1 - Looking for a minimum and maximum values of temperature of all dataset of array
2 - Create a LUT where the min temp is black (0) and max temp is white (65,536)
3 - Whit this palette create a TIFF image of every CSV.
After photogrammetric recostruction I need to resample final orthoimage with the same LUT and create a 32 bit TIFF possible with radiometric information for every pixel.
Anyone know some script that can help me!?
Thank you so much..
Title: Re: import csv image in photoscan
Post by: tuffi on June 10, 2016, 01:05:45 PM
...of course the perfect way is make it direct from phyton console and import the created TIFF directly in Photoscan
Title: Re: import csv image in photoscan
Post by: Alexey Pasumansky on June 10, 2016, 02:07:56 PM
Hello tuffi,

I think you can create PhotoScan.Image() instance of a required resolution and fill it's pixel data with the information from CSV, then save the images to disk and load them as photos.
Title: Re: import csv image in photoscan
Post by: tuffi on June 10, 2016, 03:24:32 PM
Thank you,

so, for the first stemp (1 - Looking for a minimum and maximum values of temperature of all dataset of array) I need to work outside Photoscan, right?


I acquire data from OPTRIS PI400 camera...
Title: Re: import csv image in photoscan
Post by: Alexey Pasumansky on June 16, 2016, 09:39:47 PM
Hello tuffi,

I think that the first step can be also incorporated to the script. If I have understood the input format correctly, you can loop through all the input csv files to get min and max values, then convert the csv to PhotoScan.Image() taking into account the values thresholds.

Can you provide a couple of sample files in this format and also specify, where the images should be normalized accross the whole image set or for each image individually?
Title: Re: import csv image in photoscan
Post by: tuffi on June 17, 2016, 12:47:08 PM
Yes, of course.

I attach the files.

Thanks
E.t.
Title: Re: import csv image in photoscan
Post by: tuffi on June 17, 2016, 05:58:26 PM
The normalization is need on all set of image.....

So, if black (RGB: 0.0.0) is 20 degrees in one image, must be 20 degrees in all image set..(if in one file there is no 20 degree data..so in this image the value RGB 0,0,0 will be not present)...
Title: Re: import csv image in photoscan
Post by: Alexey Pasumansky on June 19, 2016, 08:52:24 PM
Hello tuffi,

Thank you for providing the sample data, we'll try to check what could be done with that.
Title: Re: import csv image in photoscan
Post by: tuffi on June 22, 2016, 12:35:56 PM
Thank you so much Alexey!
Title: Re: import csv image in photoscan
Post by: Alexey Pasumansky on March 14, 2017, 12:38:59 PM
Hello tuffi,

Maybe it's already too late, but here's the code that reads the data from the csv files in the user-defined folder, scales the data to 0 - 65535 range using min and max values across the dataset, creates 16 bit TIFF images and loads them back to PhotoScan:

Code: [Select]
#compatibility PhotoScan Professional 1.3.0

import PhotoScan, os

main_path = PhotoScan.app.getExistingDirectory("Specify the folder with the CSV files:")

WIDTH = 382 #640
HEIGHT = 288 #512
DELIMITER = ";" #","
ABS_MAX = 65535
ABS_MIN = 0

doc = PhotoScan.app.document
chunk = doc.addChunk()
chunk.label = "Chunk thermal"

files = os.listdir(main_path)
thermal = list()
min_t, max_t = 10.0E+10, 10.0E-10

#finding min and max
for file in files:

if file[-3:].lower() != "csv":
continue
file_csv = open(main_path + "/" + file, "rt")
for y in range(HEIGHT):
line = file_csv.readline()
pixels = [float(x) for x in line.strip().split(DELIMITER) if x]
max_t = max(max(pixels), max_t)
min_t = min(min(pixels), min_t)
file_csv.close()

#creating images
for file in files:

if file[-3:].lower() != "csv":
continue

file_csv = open(main_path + "/" + file, "rt")

image = PhotoScan.Image(WIDTH, HEIGHT, " ", "U16")

for y in range(HEIGHT):
line = file_csv.readline()
pixels = [float(x) for x in line.strip().split(DELIMITER) if x]
for x in range(WIDTH):
pixel = pixels[x] * (ABS_MAX - ABS_MIN) / (max_t - min_t)
image[x,y] = (pixel,)
file_csv.close()

image.save(main_path + "/" + file[:-3] + "tif")
thermal.append(main_path + "/" + file[:-3] + "tif")

doc.chunk = chunk
chunk.addPhotos(thermal)

print("script finished")
Title: Re: import csv image in photoscan
Post by: gio_scan on January 03, 2018, 05:57:19 PM
Hi there; new to the forum and this is my first post; first of all Happy 2018!!!
I know this thread is old, but I'll try anyways :)
@ Alexey - your script to convert the Optris CSVs' into linear 16 bit TIFFs is intended to process one batch/chunk.
In case of large area surveys/missions, multiple flights are needed requiring one or more days to cover the whole target area; this means there will be several batches/chunks, each with its own set of CSVs, and individual max and min temperatures (which will also be influenced by the ambient temperature that obviously changes throughout the day). As an example with round numbers, let's assume that the target mission is to survey a 15 ha area: this is accomplished with 10 flights, and each flight generates 400 thermal images (in CSV format).
To generate the whole orthophoto/3D model of the entire 15 ha area, all the 4000 CSV files could be processed at once: while this will correctly take into account the absolute max and min temperatures of the entire 15 ha area recorded during the 10 flights, and will generate the re-scaled linear 16 bit TIFF images to be processed, I think it can be very hardware intensive and time-taking.
Now my question: is thee a way to process each of the 10 batches/chunks individually, and then mosaic them taking into account the individual max and min temperatures, in order to get the overall "temperature normalized" orthophoto?
Thanks a lot
Regards
Giovanni
Title: Re: import csv image in photoscan
Post by: Alexey Pasumansky on January 03, 2018, 07:32:15 PM
Hello Giovanni,

As far as I remember, the script creates the new chunk, then generated 16-bit TIFFs from the CSV files and loads them to the chunk as images. So in principle, you can run the script several times and create multiple chunks, then merge the chunks and process them in the single block.
Alternatively, you can process them individually and either export the orthomosaic from each block.
Title: Re: import csv image in photoscan
Post by: gio_scan on January 03, 2018, 07:55:26 PM
Thanks a lot Alexey for the prompt reply.
So, either processing methods you suggested will generate a final orthophoto (from the individual chunks) which takes into account the different "scale" (DNs linear with temperatures from CSVs) of each individual chunks?
Thanks
Giovanni
Title: Re: import csv image in photoscan
Post by: Alexey Pasumansky on January 03, 2018, 07:59:11 PM
Hello Giovanni,

Yes, if the images are loaded in "chunks" then they will be only scaled according to the actual block.

If you need to normalize all the images at once, then you need to load all the files at once.
Title: Re: import csv image in photoscan
Post by: gio_scan on January 04, 2018, 11:47:35 PM
Hi Alexey; sorry about the delayed reply: I'm just familiarizing myself with Photoscan and the various workflows (I've been using different pieces of SfM and photogrammetry software for my projects; now a potential customer asked me to quote thermal surveys and post-processing with their  hardware/software, and for photogrammetry/3D reconstruction they use Photoscan).
So, if I get it right, the best procedure would be:
- create a "thermal chunk" comprising all the CSV files of a site (multiple flights if that's the case);
- align/optimize all the images;
- duplicate the chunk multiple times (good rule of thumb: take the number of photos, divide by 250, and then duplicate the chunk that number of times).
• Use the Resize Region tool to resize the bounding box in each duplicate chunk to 1/# chunks in size. There should be some overlap between the edges of the bounding boxes to help align the chunks later.
• Then process each chunk separately for the dense cloud and the mesh steps.
• Align the chunks. Merge the chunks.
• Export the DEM/Orthomosaic.
- The "Split in Chunks" script might streamline the process (haven't tried it yet).
I still have a couple of questions to achieve as a detailed and radiometric a process as possible:
- Photo alignment step: I would think "pair selection - generic" should be set; would instead "disable" help?
- Would "blending - disabled" be better than "average"?
Lastly, the 16 bit TIFF derived from the CSVs do not contain any camera/lens parameters: what would be a way to calibrate such images (keeping also in mind that any thermal infrared camera is extremely hard to be optically calibrated).
Thanks for the great help so far: should you have any further tips on this matter I surely would not get offended ;-)
Best Regards
Giovanni
Title: Re: import csv image in photoscan
Post by: dpoursanidis on May 13, 2022, 04:51:42 PM
Even if it is an old post, the information included here is invaluable.

We have a Pi 640 and need to go for photogrammetry analysis.
If shot in Tiff, then chaos appear since data have no metadata and camera calibration parameters are hard to be found since OPTRIS refuse to provide us up to now.
I have a 60 degrees lense on the camera which has a resolution image of 640x480.

Should i go for csv files and follow the above workflow?
In parallel i collect optical data with a Sony RX 100 and use Ardupilot Mission Planner for geotagging.

Any idea is much appreciate since we have stuck in the corner with that system
Title: Re: import csv image in photoscan
Post by: Paulo on May 14, 2022, 05:13:48 AM
Hi,

i would try to set camera  focal to 60° x 45° FOV / f = 10,5 mm  in camera calibration window as well as pixel sixe to 0.017 mm

and see how alignment goes. using tif images...

If you have a small set to share we can look at best practice for alignment.

Hope this can get yu going,
Title: Re: import csv image in photoscan
Post by: Paulo on May 16, 2022, 06:14:54 AM
Hello dpoursanidis,

regarding your parallel collection with RX100, does that mean that u are capturing both RGB Sony RX100 and Infrared Optris PI640 images on same platform?

If so what is the focal length of your RX100? If it is set at 10.4 mm then the following parameters would result from a capture at 65 m AGL with 80% front and 65% sise overlap for Infraredimagery see attachment...

As you can see the resulting GSD for the 2 sensors is very different (10.5 cm for Infrared Thermal vs 1.5 cm for Sony RGB sensor).  So it would make sense to process the 2 image sets in 2 different chunks and use the georeferenced RGB set to extract CPs to georeference the Thermal set...

Again if you can share a small sample with 2 sets of images (RGB and thermal) then we could have a look at it...
Title: Re: import csv image in photoscan
Post by: dpoursanidis on May 16, 2022, 03:54:25 PM
Hi,

i would try to set camera  focal to 60° x 45° FOV / f = 10,5 mm  in camera calibration window as well as pixel sixe to 0.017 mm

and see how alignment goes. using tif images...

If you have a small set to share we can look at best practice for alignment.

Hope this can get yu going,

Are these the only parameters we need for the reconstruction of the ortho? The rest of the calibration data how can influence the results?
Title: Re: import csv image in photoscan
Post by: dpoursanidis on May 16, 2022, 03:57:21 PM
Hello dpoursanidis,

regarding your parallel collection with RX100, does that mean that u are capturing both RGB Sony RX100 and Infrared Optris PI640 images on same platform?

If so what is the focal length of your RX100? If it is set at 10.4 mm then the following parameters would result from a capture at 65 m AGL with 80% front and 65% sise overlap for Infraredimagery see attachment...

As you can see the resulting GSD for the 2 sensors is very different (10.5 cm for Infrared Thermal vs 1.5 cm for Sony RGB sensor).  So it would make sense to process the 2 image sets in 2 different chunks and use the georeferenced RGB set to extract CPs to georeference the Thermal set...

Again if you can share a small sample with 2 sets of images (RGB and thermal) then we could have a look at it...


Thanks for that detailed insights and the calculations.

Here (ftp://ftp.iacm.forth.gr/incoming/DP/OPTRIS_SONY_TESTdata/) you can have access to geotagged imagery from both cameras.

The mission planning will be based on the parameters of the Thermal Camera, since we intend to collect data in cities (if and when we will have the permissions to operate) and create a complete 3D thermal cube for the selected areas.

Meanwhile, is the excel file available for reuse?
Title: Re: import csv image in photoscan
Post by: Paulo on May 16, 2022, 07:47:06 PM
Hello,

Ive looked at your georeferenced thermal images and the problem is that they are jpg RGB representations of thermal imagery colored with thermal palette and do not represent the absolute temperature. You would need the tif images with absolute temperature (16 bit) or CSV to realy try to align....

Also, I aligned the RGB data set and I find that the height above ground level AGL is a mere 3.76 m. Why so low? and the Sony overlaps of about 50% front and 60% side is too low as it would correspond to 40% front and 45% side lap for thermal Optris imagery. Way too low for good alignment of these low resolution images. You would need about 80% front/65 to 70% side for good reconstruction...

I would try a flight at 65 m Height with photos taken every 10 m (or every 2 s if drone flies at 5m/s) and lines spaced every 20 m. This would result in thermal images with 80% front/70% side lap. Then save themal imagery as Tiff absolute temperature (16 bit) and alignment should be viable....with Mission Planner, you can use Survey grid to plan a mission defining your PI640 camera parameters. I example, I used a flight at 65 m Height over a polygon of 21 000 m2, with flight lines spaced at 21 m and a photo taken every 10 m (80% forward and 68% side). see 3rd attachment...
Title: Re: import csv image in photoscan
Post by: dpoursanidis on May 17, 2022, 10:49:30 AM
Thanks for the advices. I am aware of that requirements. This set of data come from the suppier of the system.

We are on the process to learn all needed software and safety procedures since Mission Planner is complete a new world for me.

I will be back with a proper set of material for further analysis.

If you want to have a look at the "raw" therma data here they are > ftp://ftp.iacm.forth.gr/incoming/DP/OPTRIS_SONY_TESTdata/pi640-original-dataset_2022-05-05_1732/

Thank you for your kind support.
Title: Re: import csv image in photoscan
Post by: Paulo on May 17, 2022, 12:11:17 PM
Ok dpoursanidis,

I will look at the original themal images to see If I can get some alignment. By the way do you fly your drone with both cameras (RGB and LWIR) installed and controlled by the AutoPilot or do 2 flights (one with RGB then another with LWIR)?

Update. no luck no alignment also the tiff images are formed by 2 planes of data but each with  3 band RGB 8bit. 2nd plane is in greensih tones see attachment. Normally with RJPEG you have 1st plane with RGB grey level image and 2nd plane has absolute temperature (K) in 1 band 16 bit.

Also note that your location file shows a trajectory in a very unstable zig zag form. It seems that your on borne GPS is not performing well...see 2nd attachement
Title: Re: import csv image in photoscan
Post by: dpoursanidis on May 17, 2022, 12:53:38 PM
Dear Paulo

The data i provide as tiff files are radiometric files.

The same file as you use in Agisoft is attached as printscrenn from the OPTRIS PIX application as it reads only such files.

The camera is configured through that application and i can have either .tiff or .csv either image data .csv or temperature profile data .csv

I don't have option for RJPEG format and OPTRIS is very unsupportive in any request for such issues.

Thanks for the support

Dimitris Poursanidis
Title: Re: import csv image in photoscan
Post by: Paulo on May 17, 2022, 09:35:49 PM
Hello Dimitris,

I think I found how OPtris stores the temperature in second plane of tiff files. They are stored as RGB 3 band 8bit bit, the red band contains all 0 values, green all 247 or 248 value and blue band contains temp deg Celsius * 10...see attachement where I transform the 2nd plane image into a list of bytes that is like [0, 247, 190, 0, 247, 191,....] see attachment

PS. actually this is true for blue values less than  255 so it is more cpmplicates to extract temperature....
Title: Re: import csv image in photoscan
Post by: Paulo on May 18, 2022, 03:30:14 AM
Dear Dimitris,

I think I have found how to transform the tiff 2nd plane image from Optris into absolute temperature in cK. But I think, it would be better to test on a real thermal capture over some significant area maybe flying at 65 m AGL. Because right now with this sample over a limited area (flying at 3.76 m AGL), the temperatures are varying wildly from 0 deg C to more than 80 deg C...

So, once you have more data  please feel free to PM me for further discussions...

By the way, I was able, after a lot of trials to align the last 3 16 bit thermal  images. The result is not great due to flying Height and poor overlap. So with better overlap and  higher altitude, we should get better alignment. The thermal orto shows a variation of 18 deg C to 46 deg C... see attachment
Title: Re: import csv image in photoscan
Post by: dpoursanidis on May 19, 2022, 12:46:50 PM
Dear Paul

You make my day!

Tomorrow or on Monday i will do a proper data collection flight since the weather now is not good to fligh.
You create a script for the workflow?

Thanks a lot

Dimitris
Title: Re: import csv image in photoscan
Post by: Paulo on May 19, 2022, 10:56:27 PM
Dear Dimitris,

once you have your test flight feel free to share it with me. As for the script, it can be developped so as to automatically create the thermal 16 bit camera chunk from the pi640 original images.

Right now I am doing this with many GUI manual steps that need to be streamlined into a script...

From there you would need to align, georeference and generate requred products. I think the best would be to get GCPS from the RGB geotagged set and import into thermal set for referencing...

this way the 2 sets of image would have a common reference frame.

Keep in contact,