Probe Software Users Forum

General EPMA => Discussion of General EPMA Issues => Topic started by: Ben Buse on December 04, 2024, 12:59:49 AM

Title: qgis
Post by: Ben Buse on December 04, 2024, 12:59:49 AM
Importing images into qgis.

Before I write a script I wonder if anyone else has already done this - import images from JEOL EPMA, or Thermo Fisher SEM, and determine and apply coordinate referencing from metadata.

I know WISC sims did it for other instruments.

(And it already loads grd images correctly)

Thanks
Title: Re: qgis
Post by: Ben Buse on December 04, 2024, 04:44:47 AM
Ok here's a quick code for JEOL images - creating georeferencer points file

import tkinter
import tkinter.filedialog
import pandas as pd
imagefileparameters=tkinter.filedialog.askopenfile()
df = pd.read_csv(imagefileparameters.name,sep=' ',header=None,names=range(13))
value = df[df=='$CM_MAG'].dropna(axis=0, how='all').dropna(axis=1, how='all')
magnification = df.loc[value.index.values[0],value.columns.values[0]+1]
value = df[df=='$CM_STAGE_POS'].dropna(axis=0, how='all').dropna(axis=1, how='all')
stagepositionx = df.loc[value.index.values[0],value.columns.values[0]+1]
stagepositiony = df.loc[value.index.values[0],value.columns.values[0]+2]
value = df[df=='$$SM_MICRON_BAR'].dropna(axis=0, how='all').dropna(axis=1, how='all')
ScaleBarPixelLength = df.loc[value.index.values[0],value.columns.values[0]+1]
value = df[df=='$$SM_MICRON_MARKER'].dropna(axis=0, how='all').dropna(axis=1, how='all')
ScaleBarMicronLength = df.loc[value.index.values[0],value.columns.values[0]+1]
ScaleBarMicronLength2 = ScaleBarMicronLength.split("um")[0]
MicronsPerPixel = float(ScaleBarMicronLength2)/float(ScaleBarPixelLength)
mmPerPixel = MicronsPerPixel/1000
value = df[df=='$CM_FULL_SIZE'].dropna(axis=0, how='all').dropna(axis=1, how='all')
PixelsX = df.loc[value.index.values[0],value.columns.values[0]+1]
PixelsY = df.loc[value.index.values[0],value.columns.values[0]+2]
BLx=float(stagepositionx)-((float(PixelsX)/2)*mmPerPixel)
BLy=float(stagepositiony)-((float(PixelsY)/2)*mmPerPixel)
TRx=float(stagepositionx)+((float(PixelsX)/2)*mmPerPixel)
BLy=float(stagepositiony)+((float(PixelsY)/2)*mmPerPixel)
f = open(spectrafile.name.split(".txt")[0]+".jpg.points", "w")
f.write("mapX,mapY,sourceX,sourceY,enable,dX,dY,residual")
f.write("\n")
line1=[str(BLx),str(BLy),"0",PixelsY,"1","0","0","0"]
f.write(','.join(line1))
f.write("\n")
line2=[str(TRx),str(TRy),PixelsX,"0","1","0","0","0"]
f.write(','.join(line2))
f.close()

And an image of qgis
(https://smf.probesoftware.com/gallery/453_04_12_24_4_43_35.png)

If I further develop will put on my GitHub site
Title: Re: qgis
Post by: sem-geologist on December 04, 2024, 12:41:47 PM
Quote from: Ben Buse on December 04, 2024, 12:59:49 AMImporting images into qgis.

Before I write a script I wonder if anyone else has already done this - import images from JEOL EPMA, or Thermo Fisher SEM, and determine and apply coordinate referencing from metadata.

I know WISC sims did it for other instruments.

(And it already loads grd images correctly)

Thanks

Yes, but not from JEOL or Thermo (ThermoFisher still not, but there is work in progress).
Seeing your later post where you show how you do it I want to point out that it is absolutely and absolutely enough to have 2 reference points if you use Helmert transformation. Of course that works only with flat surfaces. With Helmert transformation we suppose that x and y resolution is the same, Helmert transformation also allows to make rotation of image without any skew effect. However if there is skew (i.e. due to aberation) or pixel x and y size is different then 3 points are needed.

I would suggest to skip all bloated qgis georeferencer GUI, and just generate with script wld files (world files which contain transfromation matrix) - thus you don't need to have copy (and alter) original tiff, but by having wld files alongside tiff (and txt) can allow them be placed with right realation in qgis canvas.

I used Qgis as my integrated SEM/EPMA data workspace to explore and keep track of data for my PhD. Qgis had 200Gb of data at its fingerprint (at project) being able to jump to any part and instantly visualize on my then poor laptop with 4Gb ram... I should mention it was on linux and laptop was using hybrid linux RAID (overlay of same data at SSD and HDD :o). Oh and I used a modified recompiled Qgis as their meter scalebar was not working well for µm scale. I remember making some proposition to improve it but cant remember if it got changed in mainline of Qgis...

Title: Re: qgis
Post by: sem-geologist on December 04, 2024, 12:46:25 PM
BTW, (self advertisement incoming) if raster layers loaded in Qgis contain (english) names or abbreviations of element you could download and try using (it is marked as experimental) Element Table plugin, which allows to select such layers using periodic table. (Albeit there is a bug: if there is underscore anywhere in name then it prevents it from working correctly. Hopefully, some of my students will spot why it is like that.)
Title: Re: qgis
Post by: Ben Buse on December 05, 2024, 01:05:06 AM
Thanks for your suggestions - I will have a look at wld files, I also so far ignored that the JEOL coordinates are inverted, which I need to fix. Which can be done using helmert to apply 180 rotation, and then rotate the view in qgis 180.
Title: Re: qgis
Post by: Ben Buse on December 05, 2024, 01:15:35 AM
Will definitely give the element table plugin a try
Title: Re: qgis
Post by: JonF on December 22, 2024, 05:01:52 AM
Quote from: Ben Buse on December 05, 2024, 01:05:06 AMThanks for your suggestions - I will have a look at wld files, I also so far ignored that the JEOL coordinates are inverted, which I need to fix. Which can be done using helmert to apply 180 rotation, and then rotate the view in qgis 180.

Or just multiply the coordinates by -1?

You might want to look at the $$SM_SCAN_ROTATION value for the image from the metadata, as the image can be rotated relative to the XY plane of the stage. 
Title: Re: qgis
Post by: Ben Buse on December 23, 2024, 05:38:02 AM
Good point about scan rotation Jon, I'm currently assuming no scan rotation for the world file python scripts because I never use it on the probe. For the PFE flat bed scan image script reading 3 points from .ACQ file and creating points for georeferencer - from which any rotation on flat bed scan image can be dealt with [just created a world file for .acq image, using opencv to apply rotation to the .jpg]
Title: Re: qgis
Post by: Ben Buse on January 15, 2025, 08:12:23 AM
Here's an example of QGIS showing PFE picture snap image, and PFE grd images, and acquired points, as zoom in

overview
(https://smf.probesoftware.com/gallery/453_15_01_25_8_08_09.jpeg)

zoom in on area of analysis
(https://smf.probesoftware.com/gallery/453_15_01_25_8_10_24.jpeg)