private void ProcessImage() { Bitmap imageDetection = BitmapImage2Bitmap(imageSource); // If ML Engine Fails to Load if (cancerDetectionPredictionEngine == null) { Debug.WriteLine("ML Engine Failed"); txtOutput.Text = "ML Engine Failed"; return; } var frame = new ImageInputData { Image = imageDetection }; Debug.WriteLine("Detect Objects Using Model"); var prediction = DetectUsingModel(frame); // Text output to textblock // Note: Label 1 means having metastasis and Label 0 means not having metastasis txtOutput.Text = "Having metastasis: \n"; txtOutput.Text += Math.Round(prediction[1] * 100, 4) + "%\n\n"; txtOutput.Text += "Not having metastasis: \n"; txtOutput.Text += Math.Round(prediction[0] * 100, 4) + "%\n"; // Debug: print each predicted values // foreach (var result in prediction){ Debug.WriteLine($"BoxDescription: {result}");} }
private void ProcessWholeSlide(int ResolutionX, int ResolutionY) { Bitmap imageDetection = BitmapImage2Bitmap(imageSource); // Initialize an array and index count to save the processed prediction[1] (prob of having metasasis) output float[] haveCancerPrediction = new float[ResolutionX * ResolutionY]; int count = 0; // If ML Engine Fails to Load if (cancerDetectionPredictionEngine == null) { Debug.WriteLine("ML Engine Failed"); txtOutput.Text = "ML Engine Failed"; return; } // Note: Have better condition handling for resolution input // Given the resolution a crop of the whole slide image of size (ImageSettings.imageWidth by ImageSettings.imageHeight) will be processed using the ML model to return a float (prob of having metastasis) appended to the float[] for (float x = ImageSettings.imageWidth / 2; x <= imageDetection.Width - ImageSettings.imageWidth / 2; x += (float)(imageDetection.Width - ImageSettings.imageWidth) / (float)(ResolutionX - 1)) { for (float y = ImageSettings.imageHeight / 2; y <= imageDetection.Height - ImageSettings.imageHeight / 2; y += (float)(imageDetection.Height - ImageSettings.imageHeight) / (float)(ResolutionY - 1)) { // Crop whole slide image Int32Rect rect = new Int32Rect((int)(x - ImageSettings.imageWidth / 2), (int)(y - ImageSettings.imageHeight / 2), ImageSettings.imageWidth, ImageSettings.imageHeight); CroppedBitmap cb = new CroppedBitmap(GetBitmapSource(imageDetection), rect); // Predict outcome and set up the float[] to generate the overlay mask var frame = new ImageInputData { Image = BitmapImage2Bitmap(Bitmap2BitmapImage(cb)) }; var prediction = DetectUsingModel(frame); haveCancerPrediction.SetValue(prediction[1], count); count++; } } Bitmap overlayBitmap = new Bitmap(ResolutionX, ResolutionY); for (int pixel = 0; pixel < haveCancerPrediction.Length; pixel++) { int r_value = 255 - (int)(haveCancerPrediction[pixel] * 255 * 100000); // The intensity multiplier is 100000 if (r_value < 0) { r_value = 0; } overlayBitmap.SetPixel(pixel % ResolutionX, (int)Math.Floor((float)pixel / (float)ResolutionX), System.Drawing.Color.FromArgb(r_value, 0, 0)); overlayBitmap.SetPixel((int)Math.Floor((float)pixel / (float)ResolutionX), pixel % ResolutionX, System.Drawing.Color.FromArgb(r_value, 0, 0)); } // Scale the Bitmap mask to cover the whole slide image var targetBitmapImage = new TransformedBitmap(GetBitmapSource(overlayBitmap), new ScaleTransform((imageDetection.Width) / ResolutionX, (imageDetection.Height) / ResolutionY)); // Set the bitmap to display imgMask.Source = targetBitmapImage; }
// Use Model to Predict Feedback from ImageInputData (a single image) public float[] DetectUsingModel(ImageInputData imageInputData) { var labels = cancerDetectionPredictionEngine?.Predict(imageInputData).PredictedLabels; return(labels); }