3.17.2 Importing Images
Version Info
Minimum Origin Version Required: Origin 8 SR0
Import a BMP File
The following function shows how to import an image into a matrix by simply calling LabTalk script using Origin C's LT_execute function. After calling this function the matrix will contain an image.
void import_image_to_matrix(LPCSTR lpcszMatrixName, LPCSTR lpcszFileName)
{
string strLT;
strLT.Format("image.fileName$=%s;image.import.matrix(%s);", lpcszFileName, lpcszMatrixName);
LT_execute(strLT);
}
This next function shows how to import an image into matrix data by calling a function exported from Origin's OIMG DLL. After calling this function the matrix will contain gray scale data that represents the image.
#include <import_image.h> // For oimg_image_info and oimg_load_image function declarations
bool import_image_to_matrix_data(LPCSTR lpcszMatrixName, LPCSTR lpcszFileName, int nGrayDepth)
{
int nWidth, nHeight, nBPP;
if( !oimg_image_info(lpcszFileName, &nWidth, &nHeight, &nBPP) )
return false;
MatrixObject mo(lpcszMatrixName);
if( !mo.IsValid() )
return false;
if( !mo.SetSize(nHeight, nWidth, 0) )
return false;
// init data type
int nDataType = (16 == nGrayDepth ? FSI_USHORT : FSI_BYTE);
// set matrix data type
if( !mo.SetInternalData(nDataType, FALSE, FALSE) )
return false;
bool bRet;
if( FSI_USHORT == nDataType )
{
Matrix<WORD>& mm = mo.GetDataObject();
bRet = oimg_load_image(lpcszFileName, &mm, 16, nHeight, nWidth);
}
else // FSI_BYTE
{
Matrix<BYTE>& mm = mo.GetDataObject();
bRet = oimg_load_image(lpcszFileName, &mm, 8, nHeight, nWidth);
}
return bRet;
}
The following function will show you how to import an image to matrix data, using the function above, and access the matrix data to get the min, max, and mean values. This function can be called from LabTalk. If no arguments are given then a new matrixbook will be created and an Origin sample image will be imported.
void MyTest(string strMatrixName = "", string strFileName = "")
{
// If no matrix name given then use active or create
if( strMatrixName.IsEmpty() )
{
MatrixPage mp = Project.Pages();
if( !mp.IsValid() || !mp.GetName(strMatrixName) )
{
if( !mp.Create("origin") || !mp.GetName(strMatrixName) )
{
printf("Failed to create and get matrix name\n");
return;
}
}
}
// If no file name given then use an Origin sample image
if( strFileName.IsEmpty() )
strFileName.Format("%sSamples\Image Processing and Analysis\Car.bmp", GetOriginPath());
// Import the image as 8-bit gray scale data
import_image_to_matrix_data(strMatrixName, strFileName, 8);
// Access the matrix using the same data size. 8-bit gray scale equals BYTE data.
Matrix<BYTE> mm(strMatrixName);
if( mm.IsValid() )
{
double dMin = mm.GetMin();
double dMax = mm.GetMax();
printf("Min and Max values of %s are %g and %g respectively\n", strMatrixName, dMin, dMax);
double dMean = mm.GetMean();
printf("Mean of %s is %g\n", strMatrixName, dMean);
}
}
The following function will show you how to copy an image from a matrix to a worksheet cell.
This function can be called from LabTalk.
#include <image_utils.h> // needed for Image class
bool copy_image_from_matrix_to_wks_cell(string strMBookName, string strWBookName, int nRow = 1, int nCol = 1)
{
if( nRow > 0 )
nRow--; // LabTalk to Origin C zero based index
if( nCol > 0 )
nCol--; // LabTalk to Origin C zero based index
MatrixPage mp(strMBookName);
if( !mp.IsValid() )
{
printf("Failed to get the matrix page for %s\n", strMBookName);
return false;
}
MatrixLayer ml = mp.Layers(-1);
if( !ml.IsValid() )
{
printf("Failed to get the matrix layer\n");
return false;
}
MatrixObject mo = ml.MatrixObjects.Item(0); // get first matrix object
if( !mo.IsValid() )
{
printf("Failed to get the matrix object\n");
return false;
}
pBITMAPHANDLE lbmp = (pBITMAPHANDLE)mo.GetLeadBitmap();
if( NULL == lbmp )
{
printf("Failed to get the lead bitmap\n");
return false;
}
Image img;
img.SetLBmp(lbmp, false);
Worksheet wks(strWBookName);
if( !wks.IsValid() )
{
printf("Failed to get worksheet for %s\n", strWBookName);
return false;
}
if( FALSE == wks.SetCell(nRow, nCol, img) )
{
printf("failed to put image into worksheet cell.\n");
return false;
}
return true;
}
|