private void BtnCalcAccord_Click(object sender, RoutedEventArgs e)
{
if (imageLoaded)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Accord.Imaging.UnmanagedImage unmanagedImage1 = Accord.Imaging.UnmanagedImage.FromManagedImage(GrayScaleImage);
Accord.Imaging.BlobCounter bc = new Accord.Imaging.BlobCounter
{
BackgroundThreshold = Color.Black,
CoupledSizeFiltering = true,
FilterBlobs = true,
MinHeight = 30,
MinWidth = 30,
MaxHeight = 100,
MaxWidth = 100
};
bc.ProcessImage(GrayScaleImage);
Bitmap indexMap = AForge.Imaging.Image.Clone(GrayScaleImage);
for (int x = 0; x < indexMap.Width; x++)
{
for (int y = 0; y < indexMap.Height; y++)
{
indexMap.SetPixel(x, y, System.Drawing.Color.Black);
}
}
System.Drawing.Rectangle[] rects = bc.GetObjectsRectangles();
// process blobs
BreadBlob[] breadBlob1 = new BreadBlob[bc.ObjectsCount];
int blobArrayIndex = 0;
int blobPt = Convert.ToInt16(txbBlobNum.Text);
int blobThreshold = Convert.ToInt16(txbBlobThreshold.Text);
if (blobPt > bc.ObjectsCount)
{
blobPt = bc.ObjectsCount - 1;
}
StaticsCalculator MuffinStatistics = new StaticsCalculator();
Graphics g = Graphics.FromImage(indexMap);
List <Accord.Imaging.Blob> blobList = new List <Accord.Imaging.Blob>();
foreach (Accord.Imaging.Blob blob in bc.GetObjects(GrayScaleImage, false))
{
blobList.Add(blob);
breadBlob1[blobArrayIndex] = new BreadBlob();
breadBlob1[blobArrayIndex].TopDownThreshold = blobThreshold;
byte[,] blobArray = new byte[blob.Rectangle.Width, blob.Rectangle.Height];
for (int x = blob.Rectangle.Left; x < blob.Rectangle.Right; x++)
{
for (int y = blob.Rectangle.Top; y < blob.Rectangle.Bottom; y++)
{
System.Drawing.Color tempPixelColor = GrayScaleImage.GetPixel(x, y);
blobArray[x - blob.Rectangle.Left, y - blob.Rectangle.Top] = tempPixelColor.G;
}
}
breadBlob1[blobArrayIndex].PixelArray = blobArray;
breadBlob1[blobArrayIndex].X = blob.Rectangle.X;
breadBlob1[blobArrayIndex].Y = blob.Rectangle.Y;
if (blobArrayIndex == blobPt)
{
System.Drawing.Rectangle tempRect = blob.Rectangle;
tempRect.X -= 1;
tempRect.Y -= 1;
tempRect.Width += 2;
tempRect.Height += 2;
Accord.Imaging.Drawing.Rectangle(unmanagedImage1, tempRect, System.Drawing.Color.Yellow);
}
if (breadBlob1[blobArrayIndex].IsTop())
{
Accord.Imaging.Drawing.Rectangle(unmanagedImage1, blob.Rectangle, System.Drawing.Color.Green);
}
else
{
Accord.Imaging.Drawing.Rectangle(unmanagedImage1, blob.Rectangle, System.Drawing.Color.Red);
}
RectangleF rectf = new RectangleF(blob.Rectangle.X, blob.Rectangle.Y, blob.Rectangle.Width, blob.Rectangle.Height);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString(Convert.ToString(blob.ID - 1), new Font("Arial", 5), System.Drawing.Brushes.White, rectf);
lblBlobHeight.Content = blob.Rectangle.Height;
lblBlobWidth.Content = blob.Rectangle.Width;
blobArrayIndex++;
}
lblAccordStdDev.Content = blobList[blobPt].ColorStdDev.B;
BitmapImage indexMap_temp = ToBitmapImage(indexMap);
g.Flush();
// conver to managed image if it is required to display it at some point of time
Bitmap managedImage = unmanagedImage1.ToManagedImage();
// create filter
Add filter = new Add(indexMap);
// apply the filter
Bitmap resultImage = filter.Apply(managedImage);
BitmapImage GrayImage_temp = ToBitmapImage(resultImage);
imgGray.Source = GrayImage_temp;
stopwatch.Stop();
lblTime.Content = stopwatch.ElapsedMilliseconds;
lblBlobCount.Content = bc.ObjectsCount;
lblLib.Content = "Accord";
lblVariance.Content = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.All);
lblX.Content = breadBlob1[blobPt].X;
lblY.Content = breadBlob1[blobPt].Y;
lblQ1Variance.Content = "";
lblQ2Variance.Content = "";
lblQ3Variance.Content = "";
lblQ4Variance.Content = "";
lblQAverage.Content = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.QAverage);
//lblAllMuffinStat.Content = MuffinStatistics.StandardDeviation;
}
}