以文本 (CSV) 格式导入二维图像的 DM 脚本

DM Script to import a 2D image in text (CSV) format

使用内置的 "Import Data..." 功能,我们可以将格式正确的文本文件(如 CSV and/or 制表符分隔)导入为图像。编写脚本来执行此操作相当简单。但是,我的脚本编写方法效率不高 - 这需要我遍历每个原始文件(使用 "StreamReadTextLine" 函数),因此需要一段时间才能导入 512x512 图像。

是否有更好的方法或 "undocumented" 脚本函数可供我使用?

DigitalMicrograph 通过 File/Import Data... 菜单条目提供导入功能,这将为您提供此对话框:

此对话框引发的功能也可以通过脚本命令访问,使用命令

BasicImage ImageImportTextData( String img_name, ScriptObject stream, Number data_type_enum, ScriptObject img_size, Boolean lines_are_rows, Boolean size_by_counting )

与对话框一样,必须 pre-specify 一些事情。

图像的数据类型

这是一个数字。你可以通过f.e找到哪个数字属于哪种图像数据类型,创建一个输出其数据类型的图像:

image img := Realimage( "", 4, 100 )
Result("\n" + img.ImageGetDataType() )

文件流对象

此对象描述了数据的存储位置。 F1 help-documention 解释了如何从现有文件创建 file-stream ,但本质上你需要指定文件的路径,然后打开文件进行读取(这给你一个句柄),然后使用文件句柄创建流对象。

string path = "C:\test.txt"
number fRef = OpenFileForReading( path )
object fStream = NewStreamFromFileReference( fRef, 1 )

图片大小对象

这是您需要分配的特定脚本对象。它包装图像大小信息。如果auto-detecting来自文本的大小,您不需要指定实际大小,但您仍然需要对象。

object imgSizeObj = Alloc("ImageData_ImageDataSize")
imgSizeObj.SetNumDimensions(2)      // Not needed for counting!
imgSizeObj.SetDimensionSize(0,10)   // Not used for counting
imgSizeObj.SetDimensionSize(1,10)   // Not used for counting

布尔检查

与 UI 中的复选框一样,您指定两个条件:

  • 行就是行
  • 通过计数获取大小

请注意,只有在 "Lines are Rows" 也为真时才使用 "counting" 标志。与对话框相同。


以下脚本导入带有计算的文本文件:

image ImportTextByCounting( string path, number DataType )
{

    number fRef     = OpenFileForReading( path )
    object fStream  = NewStreamFromFileReference( fRef, 1 )
    number bLinesAreRows    = 1
    number bSizeByCount     = 1 
    bSizeByCount *= bLinesAreRows // Only valid together!

    object imgSizeObj = Alloc("ImageData_ImageDataSize")

    image img := ImageImportTextData( "Imag Name ", fStream, DataType, imgSizeObj, bLinesAreRows, bSizeByCount )
    return img
}

string path = "C:\test.txt"
number kREAL4_DATA      = 2
image img := ImportTextByCounting( path, kREAL4_DATA )
img.ShowImage()