ToManagedImage() public method

Create managed image from the unmanaged.

The method creates a managed copy of the unmanaged image with the same size and pixel format (it calls ToManagedImage(bool) specifying for the makeCopy parameter).

public ToManagedImage ( ) : Bitmap
return System.Drawing.Bitmap
Beispiel #1
0
        private void BtnLoadimage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Multiselect = true,
                Filter      = "Image files (*.png;*.jpeg;*.bmp)|*.png;*.jpeg;*.bmp|All files (*.*)|*.*",
                //openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                //InitialDirectory = "C:\\Users\\BrianWang\\Desktop\\English Muffin Scan\\Dec 1 2017"
                InitialDirectory = "C:\\Users\\kai23\\Projects\\ABI\\EnglishMuffinVision_AForge\\Images\\English Muffin\\Batch 1\\All Top"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                foreach (string filename in openFileDialog.FileNames)
                {
                    lblfilename.Content = filename;

                    int    startPos = filename.LastIndexOf("SK Foods On-Site Scan") + "SK Foods On-Site Scan".Length + 1;
                    int    length   = filename.IndexOf("æ") - startPos - 1;
                    string sub      = filename.Substring(startPos, length);

                    lblFolder.Content = sub;

                    GrayScaleImage = AForge.Imaging.Image.FromFile(filename);
                    imageLoaded    = true;
                }
            }
            if (imageLoaded)
            {
                AForge.Imaging.UnmanagedImage unmanagedImage1 = AForge.Imaging.UnmanagedImage.FromManagedImage(GrayScaleImage);
                Bitmap      managedImage   = unmanagedImage1.ToManagedImage();
                BitmapImage GrayImage_temp = ToBitmapImage(managedImage);
                imgGray.Source = GrayImage_temp;
            }
        }
        /// <summary>
        /// Looks for the brightest pixel after applying a redness filter. Narrows search first using a resampled copy of the image to eliminate edge dots. 
        /// Expects an image that is already cropped to the interested area for faster processing.
        /// </summary>
        /// <param name="img"></param>
        /// <param name="mouse"></param>
        /// <param name="maxDistanceFromMouse"></param>
        /// <returns></returns>
        public unsafe Point FindMaxPixel(UnmanagedImage img, PointF mouse, float maxDistanceFromMouse)
        {
            int width = 15;
            int height = (int)Math.Ceiling((double)img.Height / (double)img.Width * width);

            if (width <= img.Width && height <= img.Height + 1) {
                width = img.Width;
                height = img.Height;
            }

            double scale = (double)img.Width / (double)width;

            UnmanagedImage lowRed = null;
            try {
                if (width != img.Width && height != img.Height) {
                    using (Bitmap reduced = new Bitmap(width, height, PixelFormat.Format24bppRgb))
                    using (Graphics g = Graphics.FromImage(reduced))
                    using (ImageAttributes ia = new ImageAttributes()) {
                        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        ia.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);
                        g.DrawImage(img.ToManagedImage(false), new Rectangle(0, 0, width, height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
                        //TODO: Not sure if ToManagedImage will stick around after the underying image is disposed. I know that the bitmap data will be gone, guess that's most of it.
                        using (UnmanagedImage rui = UnmanagedImage.FromManagedImage(reduced)) {
                            lowRed = new RedEyeFilter(2).Apply(rui); // Make an copy using the red eye filter
                        }
                    }
                } else {
                    //Don't resample unless needed
                    lowRed = new RedEyeFilter(2).Apply(img);
                }

                Point max = GetMax(lowRed, new PointF(mouse.X / (float)scale, mouse.Y / (float)scale), maxDistanceFromMouse / scale);

                //We weren't scaling things? OK, cool...
                if (scale == 0) return max;

                //Otherwise, let's get the unscaled pixel.
                //Calculate the rectangle surrounding the selected pixel, but in source coordinates.
                int tinySize = (int)Math.Ceiling(scale) + 1;
                Rectangle tinyArea = new Rectangle((int)Math.Floor(scale * (double)max.X), (int)Math.Floor(scale * (double)max.Y), tinySize, tinySize);
                if (tinyArea.Right >= img.Width) tinyArea.Width -= img.Width - tinyArea.Right + 1;
                if (tinyArea.Bottom >= img.Height) tinyArea.Height -= img.Height - tinyArea.Bottom + 1;
                //Filter it and look
                using (UnmanagedImage tiny = new Crop(tinyArea).Apply(img)) {
                    using (UnmanagedImage tinyRed = new RedEyeFilter(2).Apply(tiny)) {
                        max = GetMax(tinyRed);
                        max.X += tinyArea.X;
                        max.Y += tinyArea.Y;
                    }
                }
                return max;
            } finally {
                if (lowRed != null) lowRed.Dispose();
            }
        }
Beispiel #3
0
        /// <summary>
        ///   Displays an image on the screen.
        /// </summary>
        /// 
        /// <param name="title">The text to display in the title bar of the image box.</param>
        /// <param name="image">The image to show.</param>
        /// <param name="sizeMode">How to display the image inside the image box.</param>
        /// <param name="width">The width of the image box.</param>
        /// <param name="height">The height of the image box.</param>
        /// <param name="backColor">The background color to use in the window. 
        ///   Default is <see cref="Color.Black"/>.</param>
        /// 
        public static DialogResult Show(string title, UnmanagedImage image, PictureBoxSizeMode sizeMode, int width, int height, Color backColor)
        {
            if (image == null)
                throw new ArgumentNullException("image");

            DialogResult result;

            using (ImageBox box = new ImageBox())
            {
                box.Text = title;
                box.pictureBox.Width = width;
                box.pictureBox.Height = height;
                box.pictureBox.SizeMode = sizeMode;
                box.pictureBox.Image = image.ToManagedImage();
                box.BackColor = backColor;
                result = box.ShowDialog();
            }

            return result;
        }
        private static PointUV GetCenterOfMass(UnmanagedImage image, Rectangle rectangle)
        {
            #if false
            centerX = rectangle.X + rectangle.Width / 2;
            centerY = rectangle.Y + rectangle.Height / 2;
            #else
            double xTotal = 0;
            double yTotal = 0;
            double massTotal = 0;
            Bitmap bitmap = image.ToManagedImage();
            for (int x = 0; x < rectangle.Width; x++) {
                for (int y = 0; y < rectangle.Height; y++) {
                    double value = bitmap.GetPixel(rectangle.X + x, rectangle.Y + y).R;
                    massTotal += value;
                    xTotal += x * value;
                    yTotal += y * value;
                }
            }

            return PointUV.Create(
                xTotal / massTotal + rectangle.X,
                yTotal / massTotal + rectangle.Y
            );
            #endif
        }
        /// <summary>
        ///   Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="sourceData">Source image data.</param>
        /// <param name="destinationData">Destination image data.</param>
        ///
        protected override void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            if (sourceData.PixelFormat == PixelFormat.Format8bppIndexed)
                sourceData = toRGB.Apply(sourceData);

            // Copy image contents
            sourceData.Copy(destinationData);

            Bitmap managedImage = destinationData.ToManagedImage(makeCopy: false);

            using (Graphics g = Graphics.FromImage(managedImage))
            using (Pen positive = new Pen(Color.Red))
            using (Pen negative = new Pen(Color.Blue))
            using (Pen line = new Pen(Color.FromArgb(0, 255, 0)))
            {
                // mark all points
                foreach (SpeededUpRobustFeaturePoint p in points)
                {
                    int S = (int)(scale * p.Scale);
                    int R = (int)(S / 2f);

                    Point pt = new Point((int)p.X, (int)p.Y);
                    Point ptR = new Point((int)(R * System.Math.Cos(p.Orientation)),
                                          (int)(R * System.Math.Sin(p.Orientation)));

                    Pen myPen = (p.Laplacian > 0 ? negative : positive);

                    g.DrawEllipse(myPen, pt.X - R, pt.Y - R, S, S);
                    g.DrawLine(line, new Point(pt.X, pt.Y), new Point(pt.X + ptR.X, pt.Y + ptR.Y));
                }
            }

        }
Beispiel #6
0
 /// <summary>
 ///   Displays an image on the screen.
 /// </summary>
 /// 
 /// <param name="title">The text to display in the title bar of the image box.</param>
 /// <param name="image">The image to show.</param>
 /// <param name="sizeMode">How to display the image inside the image box.</param>
 /// <param name="width">The width of the image box.</param>
 /// <param name="height">The height of the image box.</param>
 /// <param name="backColor">The background color to use in the window. 
 ///   Default is <see cref="Color.Black"/>.</param>
 /// 
 public static DialogResult Show(string title, UnmanagedImage image, PictureBoxSizeMode sizeMode, int width, int height, Color backColor)
 {
     return Show(title, image.ToManagedImage(), sizeMode, width, height, backColor);
 }
Beispiel #7
0
 public void DisplayNewImage(UnmanagedImage image, PictureBox dest)
 {
     dest.Image = image.ToManagedImage();
     //txt_nb_fps.Text = ((int)FPS).ToString();
 }
Beispiel #8
0
        private void BtnCalculate_Click(object sender, RoutedEventArgs e)
        {
            if (imageLoaded)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                AForge.Imaging.UnmanagedImage unmanagedImage1 = AForge.Imaging.UnmanagedImage.FromManagedImage(GrayScaleImage);
                AForge.Imaging.BlobCounter    bc = new AForge.Imaging.BlobCounter
                {
                    CoupledSizeFiltering = true,
                    FilterBlobs          = true,
                    MinHeight            = 30,
                    MinWidth             = 30,
                    MaxHeight            = 100,
                    MaxWidth             = 100
                };

                bc.ProcessImage(GrayScaleImage);

                lblBlobCount.Content = bc.ObjectsCount;

                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);
                foreach (System.Drawing.Rectangle rect in rects)
                {
                    //initialize Object
                    breadBlob1[blobArrayIndex] = new BreadBlob();
                    breadBlob1[blobArrayIndex].TopDownThreshold = blobThreshold;
                    byte[,] blobArray = new byte[rect.Width, rect.Height];

                    for (int x = rect.Left; x < rect.Right; x++)
                    {
                        for (int y = rect.Top; y < rect.Bottom; y++)
                        {
                            System.Drawing.Color tempPixelColor = GrayScaleImage.GetPixel(x, y);
                            blobArray[x - rect.Left, y - rect.Top] = tempPixelColor.G;
                        }
                    }

                    breadBlob1[blobArrayIndex].PixelArray = blobArray;
                    breadBlob1[blobArrayIndex].X          = rect.X;
                    breadBlob1[blobArrayIndex].Y          = rect.Y;
                    MuffinStatistics.Add(breadBlob1[blobArrayIndex].Variance.QAverage);

                    if (blobArrayIndex == blobPt)
                    {
                        System.Drawing.Rectangle tempRect = rect;
                        tempRect.X      -= 1;
                        tempRect.Y      -= 1;
                        tempRect.Width  += 2;
                        tempRect.Height += 2;

                        AForge.Imaging.Drawing.Rectangle(unmanagedImage1, tempRect, System.Drawing.Color.Yellow);
                    }

                    if (breadBlob1[blobArrayIndex].IsTop())
                    {
                        AForge.Imaging.Drawing.Rectangle(unmanagedImage1, rect, System.Drawing.Color.Green);
                    }
                    else
                    {
                        AForge.Imaging.Drawing.Rectangle(unmanagedImage1, rect, System.Drawing.Color.Red);
                    }

                    RectangleF rectf = new RectangleF(rect.X, rect.Y, rect.Width, rect.Height);

                    g.SmoothingMode     = SmoothingMode.AntiAlias;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                    g.DrawString(Convert.ToString(blobArrayIndex), new Font("Arial", 5), System.Drawing.Brushes.White, rectf);

                    lblBlobHeight.Content = rect.Height;
                    lblBlobWidth.Content  = rect.Width;

                    blobArrayIndex++;
                }

                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;



                lbl9var_1.Content   = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S1);
                lbl9var_2.Content   = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S2);
                lbl9var_3.Content   = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S3);
                lbl9var_4.Content   = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S4);
                lbl9var_5.Content   = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S5);
                lbl9var_6.Content   = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S6);
                lbl9var_7.Content   = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S7);
                lbl9var_8.Content   = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S8);
                lbl9var_9.Content   = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S9);
                lbl9var_avg.Content = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Savg);

                lblLib.Content           = "AForge";
                lblVariance.Content      = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.All);
                lblX.Content             = breadBlob1[blobPt].X;
                lblY.Content             = breadBlob1[blobPt].Y;
                lblQ1Variance.Content    = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q1);
                lblQ2Variance.Content    = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q2);
                lblQ3Variance.Content    = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q3);
                lblQ4Variance.Content    = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q4);
                lblQAverage.Content      = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.QAverage);
                lblAllMuffinStat.Content = MuffinStatistics.StandardDeviation;


                // System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\Histogram.txt", GrayImage1Histogram_str);
                // E:\Brian\Project 3 - English Muffin Onsite Data Gathering\Data Analysis
                //System.IO.File.WriteAllLines(@"E:\Brian\Project 3 - English Muffin Onsite Data Gathering\Data Analysis\Histogram.txt", GrayImage1Histogram_str);
                bool fileExist = File.Exists("C:\\Users\\kai23\\Projects\\ABI\\EnglishMuffinVision_AForge\\Data Analysis\\Data.csv");
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\kai23\Projects\ABI\EnglishMuffinVision_AForge\Data AnalysisData.csv", true))
                {
                    if (!fileExist)
                    {
                        file.WriteLine("File Info" +
                                       "Variance All," +
                                       "Vari Q1:," +
                                       "Vari Q2:," +
                                       "Vari Q3:," +
                                       "Vari Q4:," +
                                       "Variance Average:");
                    }

                    file.WriteLine(Convert.ToString(lblFolder.Content) + "," +
                                   Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.All)) + "," +
                                   Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q1)) + "," +
                                   Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q2)) + "," +
                                   Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q3)) + "," +
                                   Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q4)) + "," +
                                   Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.QAverage)));
                }
            }
        }
Beispiel #9
0
        private void btnMassAnalysis_Click(object sender, RoutedEventArgs e)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int index = 0; index < 6; index++)
            {
                string folderSettingText = "";

                switch (index)
                {
                case 0:
                    folderSettingText = "Batch 1\\All Top";
                    break;

                case 1:
                    folderSettingText = "Batch 2\\All Top";
                    break;

                case 2:
                    folderSettingText = "Batch 3\\All Top";
                    break;

                case 3:
                    folderSettingText = "Batch 1\\All Bottom";
                    break;

                case 4:
                    folderSettingText = "Batch 2\\All Bottom";
                    break;

                case 5:
                    folderSettingText = "Batch 3\\All Bottom";
                    break;

                default:
                    folderSettingText = "";
                    break;
                }
                string[] filename = { "" };
                //string path = @"E:\Brian\Project 3 - English Muffin Onsite Data Gathering\SK Foods On-Site Scan\English Muffin\Batch 3\All Bottom";


                string path          = "C:\\Users\\kai23\\Projects\\ABI\\EnglishMuffinVision_AForge\\Images\\English Muffin\\" + folderSettingText;
                string searchPattern = "æKatanaScoring_CameraImageGray1*";
                try
                {
                    filename = Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories);
                }
                catch (UnauthorizedAccessException)
                {
                }

                foreach (string f in filename)
                {
                    if (f != null)
                    {
                        GrayScaleImage = AForge.Imaging.Image.FromFile(f);

                        int    startPos = f.LastIndexOf("SK Foods On-Site Scan") + "SK Foods On-Site Scan".Length + 1;
                        int    length   = f.IndexOf("æ") - startPos - 1;
                        string sub      = f.Substring(startPos, length);

                        lblFolder.Content = sub;
                        //AForge.Imaging.UnmanagedImage unmanagedImage1 = AForge.Imaging.UnmanagedImage.FromManagedImage(GrayScaleImage);
                        //Bitmap managedImage = unmanagedImage1.ToManagedImage();
                        //BitmapImage GrayImage_temp = ToBitmapImage(managedImage);
                        //imgGray.Source = GrayImage_temp;

                        //Stopwatch stopwatch = new Stopwatch();
                        //stopwatch.Start();

                        AForge.Imaging.UnmanagedImage unmanagedImage1 = AForge.Imaging.UnmanagedImage.FromManagedImage(GrayScaleImage);
                        AForge.Imaging.BlobCounter    bc = new AForge.Imaging.BlobCounter
                        {
                            CoupledSizeFiltering = true,
                            FilterBlobs          = true,
                            MinHeight            = 30,
                            MinWidth             = 30,
                            MaxHeight            = 100,
                            MaxWidth             = 100
                        };

                        bc.ProcessImage(GrayScaleImage);

                        lblBlobCount.Content = bc.ObjectsCount;

                        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);
                        foreach (System.Drawing.Rectangle rect in rects)
                        {
                            //initialize Object
                            breadBlob1[blobArrayIndex] = new BreadBlob();
                            breadBlob1[blobArrayIndex].TopDownThreshold = blobThreshold;
                            byte[,] blobArray = new byte[rect.Width, rect.Height];

                            for (int x = rect.Left; x < rect.Right; x++)
                            {
                                for (int y = rect.Top; y < rect.Bottom; y++)
                                {
                                    System.Drawing.Color tempPixelColor = GrayScaleImage.GetPixel(x, y);
                                    blobArray[x - rect.Left, y - rect.Top] = tempPixelColor.G;
                                }
                            }

                            breadBlob1[blobArrayIndex].PixelArray = blobArray;
                            breadBlob1[blobArrayIndex].X          = rect.X;
                            breadBlob1[blobArrayIndex].Y          = rect.Y;
                            MuffinStatistics.Add(breadBlob1[blobArrayIndex].Variance.QAverage);

                            if (blobArrayIndex == blobPt)
                            {
                                System.Drawing.Rectangle tempRect = rect;
                                tempRect.X      -= 1;
                                tempRect.Y      -= 1;
                                tempRect.Width  += 2;
                                tempRect.Height += 2;

                                AForge.Imaging.Drawing.Rectangle(unmanagedImage1, tempRect, System.Drawing.Color.Yellow);
                            }

                            if (breadBlob1[blobArrayIndex].IsTop())
                            {
                                AForge.Imaging.Drawing.Rectangle(unmanagedImage1, rect, System.Drawing.Color.Green);
                            }
                            else
                            {
                                AForge.Imaging.Drawing.Rectangle(unmanagedImage1, rect, System.Drawing.Color.Red);
                            }

                            RectangleF rectf = new RectangleF(rect.X, rect.Y, rect.Width, rect.Height);

                            g.SmoothingMode     = SmoothingMode.AntiAlias;
                            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            g.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                            g.DrawString(Convert.ToString(blobArrayIndex), new Font("Arial", 5), System.Drawing.Brushes.White, rectf);

                            lblBlobHeight.Content = rect.Height;
                            lblBlobWidth.Content  = rect.Width;

                            blobArrayIndex++;
                        }

                        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;



                        lblLib.Content           = "AForge";
                        lblVariance.Content      = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.All);
                        lblX.Content             = breadBlob1[blobPt].X;
                        lblY.Content             = breadBlob1[blobPt].Y;
                        lblQ1Variance.Content    = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q1);
                        lblQ2Variance.Content    = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q2);
                        lblQ3Variance.Content    = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q3);
                        lblQ4Variance.Content    = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q4);
                        lblQAverage.Content      = breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.QAverage);
                        lblAllMuffinStat.Content = MuffinStatistics.StandardDeviation;


                        // System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\Histogram.txt", GrayImage1Histogram_str);
                        // E:\Brian\Project 3 - English Muffin Onsite Data Gathering\Data Analysis
                        //System.IO.File.WriteAllLines(@"E:\Brian\Project 3 - English Muffin Onsite Data Gathering\Data Analysis\Histogram.txt", GrayImage1Histogram_str);

                        bool fileExist = File.Exists("C:\\Users\\kai23\\Projects\\ABI\\EnglishMuffinVision_AForge\\Data Analysis\\Data.csv");

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\kai23\Projects\ABI\EnglishMuffinVision_AForge\Data Analysis\Data.csv", true))
                        {
                            if (!fileExist)
                            {
                                file.WriteLine("File Info," +
                                               "Variance All," +
                                               "Vari Q1:," +
                                               "Vari Q2:," +
                                               "Vari Q3:," +
                                               "Vari Q4:," +
                                               "Variance Average:," +
                                               "S1," +
                                               "S2," +
                                               "S3," +
                                               "S4," +
                                               "S5," +
                                               "S6," +
                                               "S7," +
                                               "S8," +
                                               "S9," +
                                               "Savg," +
                                               "L1," +
                                               "L2," +
                                               "L3," +
                                               "L4," +
                                               "L5," +
                                               "L6," +
                                               "L7," +
                                               "L8," +
                                               "L9," +
                                               "L10," +
                                               "L11," +
                                               "L12," +
                                               "L13," +
                                               "L14," +
                                               "L15," +
                                               "L16," +
                                               "Lavg");
                            }

                            file.WriteLine(Convert.ToString(lblFolder.Content) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.All)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q1)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q2)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q3)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Q4)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.QAverage)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S1)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S2)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S3)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S4)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S5)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S6)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S7)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S8)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.S9)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Savg)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L1)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L2)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L3)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L4)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L5)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L6)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L7)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L8)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L9)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L10)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L11)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L12)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L13)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L14)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L15)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.L16)) + "," +
                                           Convert.ToString(breadBlob1[blobPt].GetVariance(BreadBlob.VarianceType.Lavg)));
                        }

                        //lblFolder.Content = "";
                    }
                }
            }
            stopwatch.Stop();
            lblTime.Content = stopwatch.ElapsedMilliseconds;
        }
Beispiel #10
0
        // On new frame
        private void VideoNewFrame(object sender, NewFrameEventArgs e)
        {
            var tsBrush = new SolidBrush(iSpyServer.Default.TimestampColor);
            var f = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular, GraphicsUnit.Pixel);
            var sbTs = new SolidBrush(Color.FromArgb(128, 0, 0, 0));
            Bitmap bmOrig = null, bmp = null;
            Graphics g = null, gCam = null;
            try
            {
                if (e.Frame != null)
                {
                    _width = e.Frame.Width;
                    _height = e.Frame.Height;

                    lock (this)
                    {
                        if (LastFrameUnmanaged != null)
                            LastFrameUnmanaged.Dispose();

                        if (CW.Camobject.settings.resize && (CW.Camobject.settings.desktopresizewidth != e.Frame.Width || CW.Camobject.settings.desktopresizeheight != e.Frame.Height))
                        {
                            var result = new Bitmap(CW.Camobject.settings.desktopresizewidth, CW.Camobject.settings.desktopresizeheight, PixelFormat.Format24bppRgb);
                            using (Graphics g2 = Graphics.FromImage(result))
                                g2.DrawImage(e.Frame, 0, 0, result.Width, result.Height);
                            e.Frame.Dispose();
                            bmOrig = result;
                        }
                        else
                        {
                            bmOrig = e.Frame;
                        }

                        if (CW.Camobject.flipx)
                        {
                            bmOrig.RotateFlip(RotateFlipType.RotateNoneFlipX);
                        }
                        if (CW.Camobject.flipy)
                        {
                            bmOrig.RotateFlip(RotateFlipType.RotateNoneFlipY);
                        }

                        if (Mask != null)
                        {
                            g = Graphics.FromImage(bmOrig);
                            g.DrawImage(Mask, 0, 0, _width, _height);
                        }

                        LastFrameUnmanaged = UnmanagedImage.FromManagedImage(bmOrig);

                        if (CW.Camobject.settings.timestamplocation != 0 &&
                            CW.Camobject.settings.timestampformatter != "")
                        {
                            bmp = LastFrameUnmanaged.ToManagedImage();
                            gCam = Graphics.FromImage(bmp);

                            string timestamp =
                                String.Format(
                                    CW.Camobject.settings.timestampformatter.Replace("{FPS}",
                                                                                      string.Format("{0:F2}",
                                                                                                    CW.Framerate)),
                                    DateTime.Now);
                            Size rs = gCam.MeasureString(timestamp, f).ToSize();
                            var p = new Point(0, 0);
                            switch (CW.Camobject.settings.timestamplocation)
                            {
                                case 2:
                                    p.X = _width/2 - (rs.Width/2);
                                    break;
                                case 3:
                                    p.X = _width - rs.Width;
                                    break;
                                case 4:
                                    p.Y = _height - rs.Height;
                                    break;
                                case 5:
                                    p.Y = _height - rs.Height;
                                    p.X = _width/2 - (rs.Width/2);
                                    break;
                                case 6:
                                    p.Y = _height - rs.Height;
                                    p.X = _width - rs.Width;
                                    break;
                            }
                            var rect = new Rectangle(p, rs);

                            gCam.FillRectangle(sbTs, rect);
                            gCam.DrawString(timestamp, f, tsBrush, p);

                            LastFrameUnmanaged.Dispose();
                            LastFrameUnmanaged = UnmanagedImage.FromManagedImage(bmp);
                        }
                    }
                }
            }
            catch (UnsupportedImageFormatException ex)
            {
                CW.VideoSourceErrorState = true;
                CW.VideoSourceErrorMessage = ex.Message;
                if (LastFrameUnmanaged != null)
                {
                    try
                    {
                        lock (this)
                        {
                            LastFrameUnmanaged.Dispose();
                            LastFrameUnmanaged = null;
                        }
                    }
                    catch
                    {
                    }
                }
            }
            catch (Exception ex)
            {
                MainForm.LogExceptionToFile(ex);
            }
            if (gCam != null)
                gCam.Dispose();
            if (bmp != null)
                bmp.Dispose();
            if (g != null)
                g.Dispose();
            if (bmOrig != null)
                bmOrig.Dispose();
            tsBrush.Dispose();
            f.Dispose();
            sbTs.Dispose();

            if (NewFrame != null && LastFrameUnmanaged != null)
            {
                LastFrameNull = false;
                NewFrame(this, new EventArgs());
            }
        }