/// <summary> /// Function should be called whenever the data from camera 2 changes. /// </summary> private void UpdateCamera2Image() { if (Camera2Display.InvokeRequired || gbCamera2.InvokeRequired) { UpdateCamera2ImageCallback d = new UpdateCamera2ImageCallback(UpdateCamera2Image); this.BeginInvoke(d, null); } else { // obtain ownership of gui control to enter critical section guiSem.WaitOne(); // verify camera2 data is not null if (camera2Data != null) { switch (Cam2DisplayType) { case DISPLAY_TYPE_NORMAL: try { if (!camera2Data.IsProcessed) { Camera2Display.SetImage(camera2Data.GetRawDataImage()); } } catch (Exception inner) { log.Error("MainForm.UpdateCamera2Image : Unable to set camera 2 display image.", inner); } break; case DISPLAY_TYPE_PROCESSED: try { if (camera2Data.IsProcessed) { Camera2Display.SetImage(camera2Data.GetProcessedDataImage()); } } catch (Exception inner) { log.Error("MainForm.UpdateCamera2Image : Unable to set camera 2 display image.", inner); } break; default: break; } // update camera 2 information camera2Period = 0.85 * camera2Period + 0.15 * camera2Data.CameraElapsedTime_s; lblCam2FPS.Text = "Frames Per Second: " + String.Format("{0:0.00}", (1 / camera2Period)); if (camera2Data.ImageProcessorElapsedTime_s != 0) { process2Period = 0.95 * process2Period + 0.05 * camera2Data.ImageProcessorElapsedTime_s; lblCam2IPFPS.Text = "Image Processor FPS: " + String.Format("{0:0.00}", (1 / process2Period)); } lblCam2Exposure.Text = "Exposure: " + String.Format("{0:0.00}", camera2Data.ImageExposure_s * 1000); if (camera2Data.IsProcessed) { lblCam2Intensity.Text = "Intensity: " + camera2Data.ImageIntensity_lsb.ToString(); lblCam2CracksDetected.Text = "Crack Detected: " + camera2Data.ContainsCrack.ToString(); lblCam2PotentialCracks.Text = "Potential Cracks: " + camera2Data.PotentialCrackCount.ToString(); } } // release ownership of critical section guiSem.Release(); } }
/// <summary> /// Saves raw image, processe image, and metadata file for each IPData inside a folder in rootLocation. /// </summary> /// <param name="rootLocation">Location to place folder containing IPData.</param> /// <param name="data">Data to save.</param> /// <exception cref="ImageHistoryBufferException"></exception> private void SaveIPData(string rootLocation, ref IPData data) { const string RAW_DATA_EXTENSION = "//rawimage_"; const string PROC_DATA_EXTENSION = "//procimage_"; const string METADATA_EXTENSION = "//data_"; const string IMAGE_FORMAT = ".bmp"; const string METADATA_FORMAT = ".txt"; // create a folder in current directory with timestamp of image data DateTime time = data.TimeStamp; string directory = time.Month.ToString("D2") + time.Day.ToString("D2") + time.Year.ToString("D4") + "_" + time.Hour.ToString("D2") + time.Minute.ToString("D2") + time.Second.ToString("D2") + "_" + "image" + data.ImageNumber.ToString("D8"); rootLocation = rootLocation + "//" + directory; // create the directory if it does not exist if (!Directory.Exists(rootLocation)) { try { Directory.CreateDirectory(rootLocation); } catch (Exception inner) { string errMsg = "ImageHistoryBuffer.SaveIPData : Exception thrown creating directory to save IPData in."; ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner); log.Error(errMsg, ex); throw ex; } } // save raw IPData Bitmap rawBitmap = null; bool isValid = true; try { rawBitmap = data.GetRawDataImage(); } catch (Exception inner) { string errMsg = "ImageHistoryBuffer.SaveIPData : Unable to retrieve raw data image for saving."; ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner); log.Error(errMsg, ex); isValid = false; } if (isValid) { string fileName = rootLocation + RAW_DATA_EXTENSION + data.ImageNumber.ToString("D8") + IMAGE_FORMAT; try { rawBitmap.Save(fileName); } catch (Exception inner) { string errMsg = "ImageHistoryBuffer.SaveIPData : Unable to save raw data image."; ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner); log.Error(errMsg, ex); throw ex; } } // dispose image to prevent memory leak if (rawBitmap != null) { rawBitmap.Dispose(); } // save processed IPData Bitmap processedBitmap = null; isValid = true; try { processedBitmap = data.GetProcessedDataImage(); } catch (Exception inner) { string errMsg = "ImageHistoryBuffer.SaveIPData : Unable to retrieve processed data image for saving."; ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner); log.Error(errMsg, ex); isValid = false; } if (isValid) { string fileName = rootLocation + PROC_DATA_EXTENSION + data.ImageNumber.ToString("D8") + IMAGE_FORMAT; try { processedBitmap.Save(fileName); } catch (Exception inner) { string errMsg = "ImageHistoryBuffer.SaveIPData : Unable to save processed data image."; ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner); log.Error(errMsg, ex); throw ex; } } // dispose image to prevent memory leak if (processedBitmap != null) { processedBitmap.Dispose(); } // create metadata file string to save MetaData metadata = MetaData.Instance; string dataToWrite = ""; dataToWrite += "General Settings: " + Environment.NewLine; dataToWrite += "Sample Number: " + metadata.SampleNumber + Environment.NewLine; dataToWrite += "Test Number: " + metadata.TestNumber + Environment.NewLine; dataToWrite += "Processing Frequency: " + metadata.TestFrequency + Environment.NewLine; dataToWrite += "Imager Noise: " + metadata.ImagerNoise.ToString("D3") + Environment.NewLine; dataToWrite += "Minimum Contrast: " + metadata.MinimumContrast.ToString("D3") + Environment.NewLine; dataToWrite += "Target Intensity: " + metadata.TargetIntenstiy.ToString("D3") + Environment.NewLine; dataToWrite += "Minimum Line Length: " + metadata.MinimumLineLength.ToString("D3") + Environment.NewLine; dataToWrite += Environment.NewLine; dataToWrite += "Camera Information: " + Environment.NewLine; dataToWrite += "Timestamp: " + data.TimeStamp.Month.ToString("D2") + "/" + data.TimeStamp.Day.ToString("D2") + "/" + data.TimeStamp.Year.ToString("D4") + "_" + data.TimeStamp.Hour.ToString("D2") + ":" + data.TimeStamp.Minute.ToString("D2") + ":" + data.TimeStamp.Second.ToString("D2") + "." + data.TimeStamp.Millisecond.ToString("D3") + Environment.NewLine; dataToWrite += "Image Number: " + data.ImageNumber.ToString("D8") + Environment.NewLine; dataToWrite += "Image Size: " + data.ImageSize.Width.ToString("D4") + "x" + data.ImageSize.Height.ToString("D4") + Environment.NewLine; dataToWrite += "Exposure (s): " + data.ImageExposure_s.ToString() + Environment.NewLine; dataToWrite += "Intensity (lsb): " + data.ImageIntensity_lsb.ToString("D3") + Environment.NewLine; dataToWrite += "Potential Cracks: " + data.PotentialCrackCount.ToString("D2") + Environment.NewLine; dataToWrite += "Contains Crack: " + data.ContainsCrack.ToString() + Environment.NewLine; // attempt to write metadata file string to file StreamWriter write = null; try { write = new StreamWriter(rootLocation + METADATA_EXTENSION + data.ImageNumber.ToString("D8") + METADATA_FORMAT); } catch (Exception inner) { string errMsg = "ImageHistoryBuffer.SaveIPData : Error opening stream writer for metadata file."; ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner); log.Error(errMsg, inner); throw ex; } if (write != null) { try { write.Write(dataToWrite); } catch (Exception inner) { string errMsg = "ImageHistoryBuffer.SaveIPData : Error writing data to metadata file."; ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner); log.Error(errMsg, inner); throw ex; } try { write.Close(); } catch (Exception inner) { string errMsg = "ImageHistoryBuffer.SaveIPData : Error closing stream writer for metadata file."; ImageHistoryBufferException ex = new ImageHistoryBufferException(errMsg, inner); log.Error(errMsg, inner); throw ex; } } }