2.10.2 Importing Images

The ImpImage X-Function supports importing image files into Origin from script. By default, the image is stored in Origin as an image (i.e., RGB values). You have the option to convert the image to grayscale.

Multiple-file importing is supported. By default, multiple images will be appended to the target page by creating new layers. If importing to a matrix, each matrix-layer will be renamed to the corresponding imported file's name.

Import Image to Matrix and Convert to Data

This example imports a single image file to a matrix and then converts the (RGB color) image to grayscale values, storing them in a new matrix.

newbook mat:=1;              // Create a new matrix book
fpath$ = "Samples\Image Processing and Analysis\car.bmp";
string fname$ = system.path.program$ + fpath$;

// Imports the image on path 'fname$' to the active window 
//(the new matrix book)
impimage;    
  
// Converts the image to grayscale values,and puts them in a new matrix  
// 'type' specifies bit-depth:  0=short (2-byte/16-bit, default); 
// 1=byte (1-byte/8-bit)                                        
img2m type:=byte;

Import Single Image to Matrix

This example imports a series of *.TIF images into a new Matrix Book. As an alternative to the img2m X-Function (shown above), the keyboard shortcuts Ctrl+Shift+d and Ctrl+Shift+i toggle between the matrix data and image representations of the file.

newbook mat:=1;
fpath$ = "Samples\Image Processing and Analysis\";
string fns, path$ = system.path.program$ + fpath$;
// Find the files whose names begin with 'myocyte'
findfiles fname:=fns$ ext:="myocyte*.tif";
// Import each file into a new sheet (options.Mode = 4)                 
impimage options.Mode:=4 fname:=fns$;

Import Multiple Images to Matrix Book

This example imports a folder of JPG images to different Matrix books.

string pth1$ = "C:\Documents and Settings\All Users\"
string pth2$ = "Documents\My Pictures\Sample Pictures\";
string fns, path$ = pth1$ + pth2$;
// Find all *.JPG files (in 'path$', by default)
findfiles fname:=fns$ ext:="*.jpg";   
// Assign the number of files found to integer variable 'n'
// 'CRLF' ==> files separated by a 'carriage-return line-feed'    
int n = fns.GetNumTokens(CRLF);            
string bkName$;
string fname$;
// Loop through all files, importing each to a new matrix book
for(int ii = 1; ii<=n; ii++)               
{
    fname$ = fns.GetToken(ii, CRLF)$;

    //create a new matrix page
    newbook s:=0 mat:=1 result:=bkName$;   
    //import image to the first layer of the matrix page,
    //defaut file name is fname$ 
    impimage orng:=[bkName$]msheet1;       
}

Import Image to Graph Layer

You also can import an Image to an existing GraphLayer. Here the image is only for display (the data will not be visible, unless it is converted to a matrix, see next example).

string fpath$ = "Samples\Image Processing and Analysis\cell.jpg";
string fn$ = system.path.program$ + fpath$;
insertImg2g fname:=fn$ ipg:=graph1;