问题
We need to treat the SEM images from FEI and Zeiss tools in DigitalMicrograph. They are stored as tif. DigitalMicrograph can read 2D tif but images appear uncalibrated in X,Y directions. Is there any import plugIn that transfers the calibration information ? Alternatively, I can imagine that the calibration can be red directly from a stream. Has anyone a clear idea about the offset where such numbers are stored in a stream of tif? I am not very familiar with organization of tif and I know some variations exist. In particular, FEI and Zeiss tifs seems to be organized differently.
回答1:
Both FEI and ZEISS seem to store the calibration info in their own, custom ASCII text tags. Following the TIFF Format specifications (PDF) one can easily write a script which extracts all ASCII fields from the TIFF. From there on, one has to perform text-manipulations to get to the calibrations and set them to the image.
The script below does this for both FEI and ZEISS images, using the following information:
FEI
The size information is in the text in Form:
[Scan]
PixelWidth=8.26823e-010
PixelHeight=8.26823e-010
This specifies the pixel size in [meter]ZEISS
The size information is in the text in Form:
AP_HEIGHT
Height = 343.0 nm
AP_WIDTH
Width = 457.3 nm
This specifies the FOV size.
Script on PasteBin.
// Auxilliary method for stream-reading of values
number ReadValueOfType(object fStream, string type, number byteOrder)
{
number val = 0
TagGroup tg = NewTagGroup()
if ( type == "bool" )
{
tg.TagGroupSetTagAsBoolean( type, 0 )
tg.TagGroupReadTagDataFromStream( type, fstream, byteOrder )
tg.TagGroupGetTagAsBoolean( type, val )
}
else if ( type == "uint16" )
{
tg.TagGroupSetTagAsUInt16( type, 0 )
tg.TagGroupReadTagDataFromStream( type, fstream, byteOrder )
tg.TagGroupGetTagAsUInt16( type, val )
}
else if ( type == "uint32" )
{
tg.TagGroupSetTagAsUInt32( type, 0 )
tg.TagGroupReadTagDataFromStream( type, fstream, byteOrder )
tg.TagGroupGetTagAsUInt32( type, val )
}
else Throw("Invalid read-type:"+type)
return val
}
string ExtractTextFromTiff( string path )
{
string txt
if ( !DoesFileExist(path) ) Throw("File not found.\n"+path)
// Open Stream
number fileID = OpenFileForReading( path )
object fStream = NewStreamFromFileReference(fileID,1)
// Read data byte order. (1 = big Endian, 2= little Endian for Gatan)
number val
number byteOrder = 0
val = fStream.ReadValueOfType( "uint16", byteOrder )
byteOrder = ( 0x4949 == val ) ? 2 : ( 0x4D4D == val ? 1 : 0 )
//Result("\n TIFF endian:"+byteOrder)
// Verify TIFF image
val = fStream.ReadValueOfType( "uint16", byteOrder )
if ( val != 42 ) Throw( "Not a valid TIFF image" )
// Browse all directories
number offset = fStream.ReadValueOfType( "uint32", byteOrder )
while( 0 != offset )
{
fStream.StreamSetPos( 0, offset ) // Start of IFD
number nEntries = fStream.ReadValueOfType( "uint16", byteOrder )
for ( number e=0;e<nEntries;e++)
{
number tag = fStream.ReadValueOfType( "uint16", byteOrder )
number typ = fStream.ReadValueOfType( "uint16", byteOrder )
number count = fStream.ReadValueOfType( "uint32", byteOrder )
number dataOffset = fStream.ReadValueOfType( "uint32", byteOrder )
//Result("\n entry # "+e+": ID["+tag+"]\ttyp="+typ+"\tcount="+count+"\t offset @ "+dataOffset)
if ( 2 == typ ) // ASCII
{
number currentPos = fStream.StreamGetPos()
fStream.StreamSetPos( 0, dataOffset )
string textField = fStream.StreamReadAsText( 0, count )
txt+=textField
fStream.StreamSetPos( 0, currentPos )
}
}
offset = fStream.ReadValueOfType( "uint32", byteOrder ) // this is 0000 for the last directory according to spec
}
return txt
}
String TruncWhiteSpaceBeforeAndAfter( string input )
{
string work = input
if ( len(work) == 0 ) return ""
while ( " " == left(work,1) )
{
work = right( work, len(work) - 1 )
if ( len(work) == 0 ) return ""
}
while ( " " == right(work,1) )
{
work = left( work, len(work) - 1 )
if ( len(work) == 0 ) return ""
}
return work
}
// INPUT: String with line-wise information
// OUTPUT: TagGroup
// Assumptions:
// - Groups are specified in a line in the format: [GroupName]
// - The string contains information line-wise in the format: KeyName=Vale
TagGroup CreateTagsFromString( string input )
{
TagGroup tg = NewTagGroup()
string work = input
string eoL = "\n"
string GroupLeadIn = "["
string GroupLeadOut = "]"
string keyToValueSep= "="
string groupName = ""
number pos = find(work,eoL )
while( -1 != pos )
{
string line = left(work,pos)
work = right(work,len(work)-pos-len(eoL))
number leadIn = find(line,GroupLeadIn)
number leadOut = find(line,GroupLeadOut)
number sep = find(line,keyToValueSep)
if ( ( -1 < leadIn ) && ( -1 < leadOut ) && ( leadIn < leadOut ) ) // Is it a new group? "[GROUPNAME]"
{
groupName = mid(line,leadIn+len(GroupLeadIn),leadOut-leadIn-len(GroupLeadOut))
groupName = TruncWhiteSpaceBeforeAndAfter(groupName)
}
else if( -1 < sep ) // Is it a value? "KEY=VALUE" ?
{
string key = left(line,sep)
string value= right(line,len(line)-sep-len(keyToValueSep))
key = TruncWhiteSpaceBeforeAndAfter(key)
value = TruncWhiteSpaceBeforeAndAfter(value)
string tagPath = groupName + ( "" == groupName ? "" : ":" ) + key
tg.TagGroupSetTagAsString( tagPath, value )
}
pos = find(work,eoL)
}
return tg
}
void ImportTIFFWithTags()
{
string path = GetApplicationDirectory("open_save",0)
if (!OpenDialog(NULL,"Select TIFF file",path, path)) exit(0)
string extractedText = ExtractTextFromTiff(path)
/*
if ( TwoButtonDialog("Show extracted text?","Yes","No") )
result(extractedtext)
*/
tagGroup infoAsTags = CreateTagsFromString(extractedText )
/*
if ( TwoButtonDialog("Output tagstructure?","Yes","No") )
infoAsTags.TagGroupOpenBrowserWindow(path,0)
*/
// Import data and add info-tags
image imported := OpenImage(path)
imported.ImageGetTagGroup().TagGroupSetTagAsTagGroup("TIFF Tags",infoAsTags)
imported.ShowImage()
// Calibrate image, if info is found
// It seems FEI stores this value as [m] in the tags PixelHeight and PixelWidth
// while ZEISS images contain the size of the FOV in the tags "Height" and "Width" as string including unit
number scaleX = 0
number scaleY = 0
string unitX
string UnitY
string hStr
string wStr
if ( imported.GetNumberNote( "TIFF Tags:Scan:PixelWidth", scaleX ) )
{
unitX = "nm"
scaleX *= 1e9
}
if ( imported.GetNumberNote( "TIFF Tags:Scan:PixelHeight", scaleY ) )
{
unitY = "nm"
scaleY *= 1e9
}
if ( imported.GetStringNote( "TIFF Tags:Width", wStr ) )
{
number pos = find( wStr, " " )
if ( -1 < pos )
{
scaleX = val( left(wStr,pos) )
scaleX /= imported.ImageGetDimensionSize(0)
unitX = right( wStr, len(wStr)-pos-1 )
}
}
if ( imported.GetStringNote( "TIFF Tags:Height", hStr ) )
{
number pos = find( hStr, " " )
if ( -1 < pos )
{
scaleY = val( left(hStr,pos) )
scaleY /= imported.ImageGetDimensionSize(1)
unitY = right( hStr, len(hStr)-pos-1 )
}
}
if ( 0 < scaleX )
{
imported.ImageSetDimensionScale(0,scaleX)
imported.ImageSetDimensionUnitString(0,unitX)
}
if ( 0 < scaleY )
{
imported.ImageSetDimensionScale(1,scaleY)
imported.ImageSetDimensionUnitString(1,unitY)
}
}
ImportTIFFWithTags()
回答2:
FEI uses a different TIF format for each toolset (SEM versus TEM and both are version-dependent). The case is the same for TIA, for which the newer versions break the widely-used SerReader scripts. I can post a simple fix to that if requested.
FEI TIFF for TEM stores all metadata in a single tag formatted as XML. There are also separate tags for the scale, but it's also formatted as XML. I made a small tweak to BmyGuest's code above to read the scale. The other metadata have to be parsed.
// Auxilliary method for stream-reading of values
// BmyGuest's March 10, 2016 code modified to read FEI TEM TIF
number ReadValueOfType(object fStream, string type, number byteOrder)
{
number val = 0
TagGroup tg = NewTagGroup()
if ( type == "bool" )
{
tg.TagGroupSetTagAsBoolean( type, 0 )
tg.TagGroupReadTagDataFromStream( type, fstream, byteOrder )
tg.TagGroupGetTagAsBoolean( type, val )
}
else if ( type == "uint16" )
{
tg.TagGroupSetTagAsUInt16( type, 0 )
tg.TagGroupReadTagDataFromStream( type, fstream, byteOrder )
tg.TagGroupGetTagAsUInt16( type, val )
}
else if ( type == "uint32" )
{
tg.TagGroupSetTagAsUInt32( type, 0 )
tg.TagGroupReadTagDataFromStream( type, fstream, byteOrder )
tg.TagGroupGetTagAsUInt32( type, val )
}
else Throw("Invalid read-type:"+type)
return val
}
string ExtractTextFromTiff( string path )
{
string txt
if ( !DoesFileExist(path) ) Throw("File not found.\n"+path)
// Open Stream
number fileID = OpenFileForReading( path )
object fStream = NewStreamFromFileReference(fileID,1)
// Read data byte order. (1 = big Endian, 2= little Endian for Gatan)
number val
number byteOrder = 0
val = fStream.ReadValueOfType( "uint16", byteOrder )
byteOrder = ( 0x4949 == val ) ? 2 : ( 0x4D4D == val ? 1 : 0 )
//Result("\n TIFF endian:"+byteOrder)
// Verify TIFF image
val = fStream.ReadValueOfType( "uint16", byteOrder )
if ( val != 42 ) Throw( "Not a valid TIFF image" )
// Browse all directories
number offset = fStream.ReadValueOfType( "uint32", byteOrder )
while( 0 != offset )
{
fStream.StreamSetPos( 0, offset ) // Start of IFD
number nEntries = fStream.ReadValueOfType( "uint16", byteOrder )
for ( number e=0;e<nEntries;e++)
{
number tag = fStream.ReadValueOfType( "uint16", byteOrder )
number typ = fStream.ReadValueOfType( "uint16", byteOrder )
number count = fStream.ReadValueOfType( "uint32", byteOrder )
number dataOffset = fStream.ReadValueOfType( "uint32", byteOrder )
//Result("\n entry # "+e+": ID["+tag+"]\ttyp="+typ+"\tcount="+count+"\t offset @ "+dataOffset)
if ( 2 == typ ) // ASCII
{
number currentPos = fStream.StreamGetPos()
fStream.StreamSetPos( 0, dataOffset )
string textField = fStream.StreamReadAsText( 0, count )
txt+=textField
fStream.StreamSetPos( 0, currentPos )
}
}
offset = fStream.ReadValueOfType( "uint32", byteOrder ) // this is 0000 for the last directory according to spec
}
return txt
}
String TruncWhiteSpaceBeforeAndAfter( string input )
{
string work = input
if ( len(work) == 0 ) return ""
while ( " " == left(work,1) )
{
work = right( work, len(work) - 1 )
if ( len(work) == 0 ) return ""
}
while ( " " == right(work,1) )
{
work = left( work, len(work) - 1 )
if ( len(work) == 0 ) return ""
}
return work
}
// INPUT: String with line-wise information
// OUTPUT: TagGroup
// Assumptions:
// - Groups are specified in a line in the format: [GroupName]
// - The string contains information line-wise in the format: KeyName=Vale
TagGroup CreateTagsFromString( string input )
{
TagGroup tg = NewTagGroup()
string work = input
string eoL = "\n"
string GroupLeadIn = "["
string GroupLeadOut = "]"
string keyToValueSep= "="
string groupName = ""
number pos = find(work,eoL )
while( -1 != pos )
{
string line = left(work,pos)
work = right(work,len(work)-pos-len(eoL))
number leadIn = find(line,GroupLeadIn)
number leadOut = find(line,GroupLeadOut)
number sep = find(line,keyToValueSep)
if ( ( -1 < leadIn ) && ( -1 < leadOut ) && ( leadIn < leadOut ) ) // Is it a new group? "[GROUPNAME]"
{
groupName = mid(line,leadIn+len(GroupLeadIn),leadOut-leadIn-len(GroupLeadOut))
groupName = TruncWhiteSpaceBeforeAndAfter(groupName)
}
else if( -1 < sep ) // Is it a value? "KEY=VALUE" ?
{
string key = left(line,sep)
string value= right(line,len(line)-sep-len(keyToValueSep))
key = TruncWhiteSpaceBeforeAndAfter(key)
value = TruncWhiteSpaceBeforeAndAfter(value)
string tagPath = groupName + ( "" == groupName ? "" : ":" ) + key
tg.TagGroupSetTagAsString( tagPath, value )
}
pos = find(work,eoL)
}
return tg
}
void ImportTIFFWithTags()
{
string path = GetApplicationDirectory("open_save",0)
if (!OpenDialog(NULL,"Select TIFF file",path, path)) exit(0)
string extractedText = ExtractTextFromTiff(path)
/*
if ( TwoButtonDialog("Show extracted text?","Yes","No") )
result(extractedtext)
*/
tagGroup infoAsTags = CreateTagsFromString(extractedText )
/*
if ( TwoButtonDialog("Output tagstructure?","Yes","No") )
infoAsTags.TagGroupOpenBrowserWindow(path,0)
*/
// Import data and add info-tags
image imported := OpenImage(path)
imported.ImageGetTagGroup().TagGroupSetTagAsTagGroup("TIFF Tags",infoAsTags)
imported.ShowImage()
// Calibrate image, if info is found
// It seems FEI stores this value as [m] in the tags PixelHeight and PixelWidth
// while ZEISS images contain the size of the FOV in the tags "Height" and "Width" as string including unit
number scaleX = 0
number scaleY = 0
string unitX
string UnitY
string scaletemp
number scalestart, scaleend
string hStr
string wStr
if ( imported.GetNumberNote( "TIFF Tags:Scan:PixelWidth", scaleX ) )
{
unitX = "nm"
scaleX *= 1e9
}
if ( imported.GetNumberNote( "TIFF Tags:Scan:PixelHeight", scaleY ) )
{
unitY = "nm"
scaleY *= 1e9
}
if (imported.GetStringNote( "TIFF Tags:<X unit", scaletemp ) )
{
unitX = "nm"
scalestart = scaletemp.find("\">") + 2
scaleend = scaletemp.find("</X>")
scaleX = val(scaletemp.mid(scalestart,scaleend-scalestart))*1e9
}
if ( imported.GetStringNote( "TIFF Tags:<Y unit", scaletemp ) )
{
unitY = "nm"
scalestart = scaletemp.find("\">") + 2
scaleend =scaletemp.find("</Y>")
scaleY = val(scaletemp.mid(scalestart,scaleend-scalestart))*1e9
}
if ( imported.GetStringNote( "TIFF Tags:Width", wStr ) )
{
number pos = find( wStr, " " )
if ( -1 < pos )
{
scaleX = val( left(wStr,pos) )
scaleX /= imported.ImageGetDimensionSize(0)
unitX = right( wStr, len(wStr)-pos-1 )
}
}
if ( imported.GetStringNote( "TIFF Tags:Height", hStr ) )
{
number pos = find( hStr, " " )
if ( -1 < pos )
{
scaleY = val( left(hStr,pos) )
scaleY /= imported.ImageGetDimensionSize(1)
unitY = right( hStr, len(hStr)-pos-1 )
}
}
if ( 0 < scaleX )
{
imported.ImageSetDimensionScale(0,scaleX)
imported.ImageSetDimensionUnitString(0,unitX)
}
if ( 0 < scaleY )
{
imported.ImageSetDimensionScale(1,scaleY)
imported.ImageSetDimensionUnitString(1,unitY)
}
}
ImportTIFFWithTags()
回答3:
TIF or TIFF (Tagged Image File Format) is rather general format where any addtional information can be stored as "Tag" in the file. (This is not the same as a Tag in DM though.) What exactly is written to the tags (and where) depends on the software writing the file, i.e. FEI in this case. I assume that the physical calibration is written to these tags, but I do not have a file to test. (You might upload one to your question?)
I do believe, but haven't checked, that DM writes all or some of thes tags into its own TagGroup structure on TIFF import. Did you check? (i.e. if you import a a TIFF file from FEI via DM and go to "Image Display -> Tags", what do you see? It might be, that the necessary information for calibration then in there and you can write a simple script to utilize this for calibration.
Alternatively, one can most often open a TIFF file in a Text-Editor to see - besides a lot of binary for the image - the tags in ASCII text. This will give you an idea where things are stored in this particular TIFF file.
Equipped with this knowledge, you can use the RAW streaming commands to find the according information and write an import script which copies the calibration as well.
来源:https://stackoverflow.com/questions/35801464/how-to-import-tif-calibration-into-dm