You are on page 1of 3

/*------------------------------------------------------------------------------* * File Name: Image Processing.

c * * Creation: ER 7/16/2001 * * Purpose: Origin C file for applying image filters/masks to image matrices * * Copyright (c) OriginLab Corp. 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 * * All Rights Reserved * * * * Modification Log: * *------------------------------------------------------------------------------*/ //////////////////////////////////////////////////////////////////////////////////// // you must include this header file for all Origin built-in functions and classes #include <origin.h> // include header file for external dll function call //#include "IPFilter.h" // //////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// // // This function implements image processing by applying a mask/filter matrix to an image matri // iPad = 0 : pad border of image with zeroes // iPad = 1 : pad border of image with cells from image // iNorm = 0 : do not normalize after filter computation // iNorm = 1 : normalize after filter computation with sum of filter elements // int FilterImage(string strImage, string strFilter, int iPad = 1, int iNorm = 1) { int ii, jj, iMRows, iMCols, iFRows, iFCols; // Define the image matrix to be of type unsigned short // In Origin, the type should be set to short(2) Matrix <ushort> matImage; matImage.Attach(strImage); if(!matImage.IsValid()) return 1; // Get image matrix size iMRows = matImage.GetNumRows(); iMCols = matImage.GetNumCols(); // Gefine the filter/mask matrix to be of type double. In Origin, set this to Double(8) Matrix <double> matFilter; matFilter.Attach(strFilter); if(!matFilter.IsValid()) return 1; // Get filter matrix size iFRows = matFilter.GetNumRows(); iFCols = matFilter.GetNumCols(); // If image matrix is too small, quit if( (iMRows < iFRows) | (iMCols < iFCols) ) { printf("Image is too small!\n"); return 1; } // Perform some checks on the filter matrix size if (iFRows != iFCols) { printf("Filter matrix is not square!\n"); return 1; } else if (iFRows < 3 | iFRows > 7) { printf("Filter is too large!\n"); return 1; } else if ( (iFRows % 2) != 1) { printf("Filter size is incorrect!\n"); return 1; } // Report image and filter sizes printf("image size = %d x %d\n",iMRows,iMCols);

printf("filter size

= %d x %d\n",iFRows,iFCols);

matImage.SetZRangeUpdate(FALSE);// this will disable Z1 Z2 recalculation, make it faster // Now perform filter operation printf("Applying filter to image matrix...\n"); matImage.ApplyFilter(matFilter, iPad); printf("Done!\n"); return 0; } //////////////////////////////////////////////////////////////////////////////////// // // This function performs median filtering - repalces each pixel with the median // value of pixels in the isizexisize neighbourhood // int MedianFilter(String strImage, int isize = 3, int ipad = 1) { // Define the image matrix to be of type unsigned short // In Origin, the type should be set to Int(2) Matrix <ushort> matImage; matImage.Attach(strImage); if(!matImage.IsValid()) return 1; // Perform median filtering matImage.MedianFilter(isize, ipad); return 0; } //////////////////////////////////////////////////////////////////////////////////// // // This function adds noise to the image // int AddNoise(String strImage) { int ii, jj, iMRows, iMCols; // Define the image matrix to be of type unsigned short // In Origin, the type should be set to Int(2) Matrix <ushort> matImage; matImage.Attach(strImage); // Get image matrix size iMRows = matImage.GetNumRows(); iMCols = matImage.GetNumCols(); // Add noise to 5% of cells int iNsize = 0.05*iMRows*iMCols; for (ii = 0; ii<iNsize; ii++) { int ir = iMRows*rnd(); int ic = iMCols*rnd(); matImage[ir][ic] = 255; } return 0; } //////////////////////////////////////////////////////////////////////////////////// // // Call this function to ApplyFilter // void ApplyFilter() { //Get what's choosed in the combo box double dChoice; LT_get_var("combox.choice", &dChoice); int iChoice = (int)dChoice; if (iChoice < 1 || iChoice > 9)

{ MessageBox( NULL, "Choose a filter!", NULL, MB_OK ); return; } int iRtn; if (iChoice == 1) iRtn = AddNoise("Image"); if (iChoice == 2) iRtn = FilterImage("Image","Blur"); if (iChoice == 3) iRtn = FilterImage("Image","Detail"); if (iChoice == 4) iRtn = FilterImage("Image","EdgeDetect"); if (iChoice == 5) iRtn = FilterImage("Image","EdgeEnhance"); if (iChoice == 6) iRtn = FilterImage("Image","Mean"); if (iChoice == 7) iRtn = MedianFilter("Image"); if (iChoice == 8) iRtn = FilterImage("Image","Sharpen"); if (iChoice == 9) iRtn = FilterImage("Image","Smooth"); if (iRtn != 0) { MessageBox( NULL, "Filter operation failed", NULL, MB_OK); } }