// Analyze Taber Scratch Images // JMP Discovery Summit 2018 // Last Edited: Evan Bittner 9/21/2018 // Original script: Casey Volino // This script reads in all images files in a directory by pixel // then performs calculations and populates a data table // Specifically it calculates the Taber Scratch Damage Severity Metric // Go to the location where the images are stored Names Default To Here( 1 ); path = fName = "\\NA.corning.com\CVUD-MTE$\UD3\bittnere\My Documents\JMP\JMP Discovery Summit 2018\Presentation\Data and Script\Testing\Images\Named"; // Get a list of all bmp files // This can be switched to a different file type file_list = {}; final_files = {}; file_list = Files In Directory( path, recursive ); Show( file_list ); j = 0; For( i = 1, i <= N Items( file_list ), i++, If( Contains( file_list[i], "bmp" ), j = j + 1; Show( i, j ); final_files[j] = file_list[i]; ) ); nFiles = j; /* For debugging Show( nFiles); Show( final_files ); */ //Make a new data table that stores the results dtResults = New Table( "Results", New Column( "Filename", Character, Nominal ), New Column( "Sample Number", Numeric, Continuous), New Column( "Scratch Intensity", Numeric, Continuous), New Column( "Background Intensity", Numeric, Continuous), New Column( "Intensity (Adj)", Numeric, Continuous), New Column( "Scratch Response", Numeric, Continuous), Set Label Columns( :Filename ) ); // Add a row to the table for each image file dtResults << Add Rows( nFiles); Wait(6); // For presentation // Open each image file, get the data, calculate summary stats, // and store the results for analyses. For( i = 1, i <= nFiles, i++, //// i = 1;//for debugging fName = path || "\" || final_files[i]; // show(fName); img = New Image( fName ); // Uncomment next line to view full image of sample full_window = new window("Full Image", img); Wait(3); // For presentation full_window << close window; // Get the individual pixels from the image // in the form of a list of red, green, and blue matricies {r} = img << GetPixels( "r" ); // Populate variables to put in each column samp = num(substr(final_files[i], 1, 1)); // Make sure area of interest is centered in the damaged area // This is to specify the corner reference point of the area of interest // It varies by sample for this application if(samp == 7, TopLimit = 730); if(samp == 9, TopLimit = 725); LeftLimit = 1080; /* // For debugging Show(TopLimit); Show(LeftLimit); */ // Subset the pixels to only include the (scratch) area of interest // Size of area of interest for Taber scratches is 340x100 for this application r1 = r[TopLimit :: (TopLimit + 100), LeftLimit :: (LeftLimit + 340)]; //Uncomment to view table of intensities pixel by pixel //dtr1 = As Table(r1); // Do the same for the background images TopLimit = TopLimit + 200; r2 = r[TopLimit :: (TopLimit + 100), LeftLimit :: (LeftLimit + 340)]; //Uncomment to view table of intensities pixel by pixel //dtr2 = As Table(r2); // Create images of just the areas of interest img1 = New Image( "rgb", {r1, r1, r1} ); img2 = New Image( "rgb", {r2, r2, r2} ); scratch_window = New Window( fName || " Scratch", img1 ); background_window = New Window( fName || " Background", img2 ); wait(6); // For presentation scratch_window << close window; background_window << close window; // Calculate the scratch response Mean_Scratch = Mean( r1 * 255); // Average pixel intensity of scratch area Mean_Back = Mean( r2 * 255); // Average pixel intensity of background area Mean_Adj = Mean_Scratch - Mean_Back; // Take the difference Log_Mean_Adj = Log(Max(Mean_Adj,1)); // Take the natural log of the difference or one // Populate the table with the results for the respective file/image Column( dtResults, "Filename" )[i] = final_files[i]; Column( dtResults, "Sample Number" )[i] = samp; Column( dtResults, "Scratch Intensity" )[i] = Mean_Scratch; Column( dtResults, "Background Intensity" )[i] = Mean_Back; Column( dtResults, "Intensity (Adj)" )[i] = Mean_Adj; Column( dtResults, "Scratch Response" )[i] = Log_Mean_Adj; Wait(5); // For presentation );