コード例 #1
0
        public CvImage ProcessImage(CvImage image, IMAGE_ALGO_TYPES type)
        {
            var gausianed = MatrixMath.Convolve(GAUSSIAN_FILTER, image.bitmap);
            var sobel_xed = MatrixMath.Convolve(SOBEL_X_FILTER, gausianed.bitmap);
            var sobel_yed = MatrixMath.Convolve(SOBEL_Y_FILTER, gausianed.bitmap);
            var canny_edged_image = DrawCannyEdges(sobel_xed.bitmap, sobel_yed.bitmap);

            return canny_edged_image;
        }
コード例 #2
0
        public CvImage ProcessImage(CvImage image, IMAGE_ALGO_TYPES type)
        {
            var gausianed         = MatrixMath.Convolve(GAUSSIAN_FILTER, image.bitmap);
            var sobel_xed         = MatrixMath.Convolve(SOBEL_X_FILTER, gausianed.bitmap);
            var sobel_yed         = MatrixMath.Convolve(SOBEL_Y_FILTER, gausianed.bitmap);
            var canny_edged_image = DrawCannyEdges(sobel_xed.bitmap, sobel_yed.bitmap);

            return(canny_edged_image);
        }
コード例 #3
0
        public CvImage OpenImage(string file)
        {
            CvImage result = new CvImage();

            if (file != string.Empty)
            {
                result.bitmap = new Bitmap(file);
            }

            return(result);
        }
コード例 #4
0
        public CvImage OpenImage(string file)
        {
            CvImage result = new CvImage();

            if(file != string.Empty)
            {
                result.bitmap = new Bitmap(file);
            }

            return result;
        }
コード例 #5
0
        /// <summary>
        /// 平滑化
        /// </summary>
        private void CreateSmoothImage()
        {
            try
            {
                if (this.FileName == null)
                {
                    this.FileName = this.SelectImageFile();
                }

                CvSmoothType smoothType = this.SmoothProperty.SmoothType;
                int          filterX    = this.SmoothProperty.FilterX;
                int          filterY    = this.SmoothProperty.FilterY;
                double       sigma1     = this.SmoothProperty.Sigma1;
                double       sigma2     = this.SmoothProperty.Sigma2;

                using (CvImage orgImage = new CvImage(this.FileName))
                    using (CvImage smoothImage = CvImgProc.Smooth(orgImage, smoothType, filterX, filterY, sigma1, sigma2))
                        using (MemoryStream stream = new MemoryStream())
                        {
                            Bitmap bitmap = smoothImage.GetImageBmp();

                            if (bitmap == null)
                            {
                                MessageBox.Show("bitmap null.");
                                return;
                            }

                            bitmap.Save(stream, ImageFormat.Bmp);

                            // BitmapImageの作成(キャッシュを切る)
                            BitmapImage bmpImage = new BitmapImage();
                            bmpImage.BeginInit();
                            bmpImage.CacheOption   = BitmapCacheOption.OnLoad;
                            bmpImage.CreateOptions = BitmapCreateOptions.None;
                            bmpImage.StreamSource  = stream;
                            bmpImage.EndInit();
                            bmpImage.Freeze();

                            this.Image006.Source = bmpImage;

                            bmpImage = null;
                        }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #6
0
        public CvImage ProcessPixels(CvImage input, IMAGE_ALGO_TYPES type)
        {
            CvImage result = null;

            try
            {
                var algorithm = algoList.First(a => a != null && a.GetAlgoType().Any(t => t == type));

                result = algorithm.ProcessImage(input, type);
            }
            catch (Exception ex)
            {
            }

            return(result);
        }
コード例 #7
0
        /// <summary>
        /// 適応型二値化
        /// </summary>
        private void CreateAdaptBinaryImage()
        {
            try
            {
                if (this.FileName == null)
                {
                    this.FileName = this.SelectImageFile();
                }

                CvAdaptMethod adaptMethod = this.AdaptiveThresholdProperty.AdaptMethod;
                int           blockSize   = this.AdaptiveThresholdProperty.BlockSize;
                double        param       = this.AdaptiveThresholdProperty.Param;

                using (CvImage orgImage = new CvImage(this.FileName))
                    using (CvImage smoothImage = CvImgProc.AdaptiveThresdhold(orgImage, adaptMethod, blockSize, param))
                        using (MemoryStream stream = new MemoryStream())
                        {
                            Bitmap bitmap = smoothImage.GetImageBmp();

                            if (bitmap == null)
                            {
                                MessageBox.Show("bitmap null.");
                                return;
                            }

                            bitmap.Save(stream, ImageFormat.Bmp);

                            // BitmapImageの作成(キャッシュを切る)
                            BitmapImage bmpImage = new BitmapImage();
                            bmpImage.BeginInit();
                            bmpImage.CacheOption   = BitmapCacheOption.OnLoad;
                            bmpImage.CreateOptions = BitmapCreateOptions.None;
                            bmpImage.StreamSource  = stream;
                            bmpImage.EndInit();
                            bmpImage.Freeze();

                            this.Image005.Source = bmpImage;

                            bmpImage = null;
                        }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #8
0
        /// <summary>
        /// 二値化
        /// </summary>
        private void CreateBinaryImage()
        {
            try
            {
                if (this.FileName == null)
                {
                    this.FileName = this.SelectImageFile();
                }

                using (CvImage orgImage = new CvImage(this.FileName))
                    using (CvImage binImage = CvImgProc.Threshold(orgImage, this.Threshold))
                        using (MemoryStream stream = new MemoryStream())
                        {
                            Bitmap bitmap = binImage.GetImageBmp();

                            if (bitmap == null)
                            {
                                MessageBox.Show("bitmap null.");
                                return;
                            }

                            bitmap.Save(stream, ImageFormat.Bmp);

                            // BitmapImageの作成(キャッシュを切る)
                            BitmapImage bmpImage = new BitmapImage();
                            bmpImage.BeginInit();
                            bmpImage.CacheOption   = BitmapCacheOption.OnLoad;
                            bmpImage.CreateOptions = BitmapCreateOptions.None;
                            bmpImage.StreamSource  = stream;
                            bmpImage.EndInit();
                            bmpImage.Freeze();

                            this.Image004.Source = bmpImage;

                            bmpImage = null;
                        }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                GC.Collect();
            }
        }
コード例 #9
0
        public CvImage ProcessPixels(CvImage input, IMAGE_ALGO_TYPES type)
        {
            CvImage result = null;

            try
            {
                var algorithm = algoList.First(a => a != null && a.GetAlgoType().Any(t => t == type));

                result = algorithm.ProcessImage(input, type);

            }
            catch(Exception ex)
            {

            }

            return result;
        }
コード例 #10
0
        public CvImage ProcessImage(CvImage image, IMAGE_ALGO_TYPES type)
        {
            CvImage result = new CvImage();

            result.bitmap = new Bitmap(image.bitmap.Width, image.bitmap.Height);

            for (int w_ic = 0; w_ic < image.bitmap.Width; w_ic++)
            {
                for (int h_ic = 0; h_ic < image.bitmap.Height; h_ic++)
                {
                    Color pix_color = image.bitmap.GetPixel(w_ic, h_ic);

                    var gray_scale = (int)(pix_color.R * RED_SCALE + pix_color.B * BLUE_SCALE + pix_color.G * GREEN_SCALE);

                    result.bitmap.SetPixel(w_ic, h_ic, Color.FromArgb(pix_color.A, gray_scale, gray_scale, gray_scale));
                }
            }

            return(result);
        }
コード例 #11
0
        public CvImage ProcessImage(CvImage image, IMAGE_ALGO_TYPES type)
        {
            CvImage result = new CvImage();

            result.bitmap = new Bitmap(image.bitmap.Width, image.bitmap.Height);

            for (int w_ic = 0; w_ic < image.bitmap.Width; w_ic++)
            {
                for (int h_ic = 0; h_ic < image.bitmap.Height; h_ic++)
                {
                    Color pix_color = image.bitmap.GetPixel(w_ic, h_ic);

                    var gray_scale = (int) (pix_color.R* RED_SCALE + pix_color.B * BLUE_SCALE + pix_color.G * GREEN_SCALE);

                    result.bitmap.SetPixel(w_ic, h_ic, Color.FromArgb(pix_color.A, gray_scale, gray_scale, gray_scale));
                }

            }

            return result;
        }
コード例 #12
0
        private CvImage DrawCannyEdges(Bitmap sobel_x, Bitmap sobel_y)
        {
            CvImage image = new CvImage();

            int[,] edge_direction = new int[sobel_x.Width, sobel_x.Height];
            int[,] edge_mag = new int[sobel_x.Width, sobel_x.Height];
            int[,] non_max = new int[sobel_x.Width, sobel_x.Height];
            int kernWid = GAUSSIAN_FILTER.MATRIX.GetLength(1);
            int kernHei = GAUSSIAN_FILTER.MATRIX.GetLength(0);
            int kernOffset = (kernWid - 1) / 2;

            for (int wid_ic = kernOffset; wid_ic < sobel_x.Width - kernOffset; wid_ic++)
            {
                for (int hei_ic = kernOffset; hei_ic < sobel_x.Height - kernOffset; hei_ic++)
                {
                    var pixel_y = sobel_y.GetPixel(wid_ic, hei_ic);
                    var pixel_x = sobel_x.GetPixel(wid_ic, hei_ic);
                    var mag_y = PixelMath.pixelMag(pixel_y);
                    var mag_x = PixelMath.pixelMag(pixel_y);
                    var edge_strength = mag_y + mag_x;

                    edge_mag[wid_ic, hei_ic] = (int)edge_strength;
                    non_max[wid_ic, hei_ic] = (int)edge_strength;
                }
            }

            for (int wid_ic = kernWid; wid_ic < sobel_x.Width - kernWid; wid_ic++)
            {
                for(int hei_ic = kernHei; hei_ic < sobel_x.Height - kernHei; hei_ic++)
                {
                    var pixel_y = sobel_y.GetPixel(wid_ic, hei_ic);
                    var pixel_x = sobel_x.GetPixel(wid_ic, hei_ic);
                    var mag_y = PixelMath.pixelMag(pixel_y);
                    var mag_x = PixelMath.pixelMag(pixel_y);
                    var current_mag = edge_mag[wid_ic, hei_ic];
                    double theta = current_mag != 0 ? Math.Atan2(mag_y, mag_x) : 90;

                    //CHO_ACTIVE: clean this up
                    //near horizontal
                    if ((theta > -22.5 && theta <= 22.5) ||
                        (theta > 157.5) ||
                        (theta < -157.5))
                    {
                        if((current_mag < edge_mag[wid_ic,hei_ic + 1]) ||
                            (current_mag < edge_mag[wid_ic, hei_ic - 1]))
                        {
                            non_max[wid_ic, hei_ic] = 0;
                        }

                    }
                    // +45 degree
                    else if ((theta > 22.5 && theta <= 67.5) ||
                             (theta <= -67.5 && theta > -22.5))
                    {
                        if ((current_mag < edge_mag[wid_ic + 1, hei_ic + 1]) ||
                            (current_mag < edge_mag[wid_ic - 1, hei_ic - 1]))
                        {
                            non_max[wid_ic, hei_ic] = 0;
                        }
                    }
                    //vertical
                    else if ((theta > 67.5 && theta <= 112.5) ||
                             (theta <= -67.5 && theta > -112.5))
                    {
                        if ((current_mag < edge_mag[wid_ic + 1, hei_ic]) ||
                            (current_mag < edge_mag[wid_ic - 1, hei_ic]))
                        {
                            non_max[wid_ic, hei_ic] = 0;
                        }
                    }
                    // +135 degree
                    else
                    {
                        if ((current_mag < edge_mag[wid_ic + 1, hei_ic - 1]) ||
                            (current_mag < edge_mag[wid_ic - 1, hei_ic + 1]))
                        {
                            non_max[wid_ic, hei_ic] = 0;
                        }
                    }

                    edge_direction[wid_ic, hei_ic] = (int)theta;
                }
            }

            image.bitmap = TraceCannyEdge(non_max);

            return image;
        }
コード例 #13
0
        public static CvImage Convolve(Filter filter, Bitmap image)
        {
            CvImage result = new CvImage();
            var     kernel = filter.MATRIX;
            var     scale  = filter.factor;
            var     bias   = filter.bias;

            result.bitmap = new Bitmap(image.Width, image.Height);
            int        kernWid = kernel.GetLength(1);
            int        kernHei = kernel.GetLength(0);
            var        tile    = new Rectangle(0, 0, image.Width, image.Height);
            BitmapData srcdata = image.LockBits(tile, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            BitmapData dstdata = result.bitmap.LockBits(tile, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            int srcStride     = srcdata.Stride;
            int dstStride     = dstdata.Stride;
            var bytesPerPixel = srcStride / image.Width;

            // we can't filter pixels without enough surrounding pixels
            int kernOffset = (kernWid - 1) / 2;

            //TODO: does not set non-filtered pixels
            unsafe
            {
                byte *   dstPointer = (byte *)dstdata.Scan0;
                byte *   srcPointer = (byte *)srcdata.Scan0;
                double[] sums       = new double[bytesPerPixel];

                for (int offsetY = kernOffset; offsetY < image.Height - kernOffset; offsetY++)
                {
                    for (int offsetX = kernOffset; offsetX < image.Width - kernOffset; offsetX++)
                    {
                        for (int sum_ic = 0; sum_ic < bytesPerPixel; sum_ic++)
                        {
                            sums[sum_ic] = 0;
                        }

                        var dst_row   = dstPointer + offsetY * srcStride;
                        var dst_pixel = dst_row + offsetX * bytesPerPixel;

                        for (int kernY = -kernOffset; kernY <= kernOffset; kernY++)
                        {
                            var row = srcPointer + (kernY + offsetY) * srcStride;

                            for (int kernX = -kernOffset; kernX <= kernOffset; kernX++)
                            {
                                var    pixel        = row + (kernX + offsetX) * bytesPerPixel;
                                double filter_coeff = (kernel[kernX + kernOffset, kernY + kernOffset]);
                                sums[0] += (((double)pixel[0]) * filter_coeff);
                                sums[1] += (((double)pixel[1]) * filter_coeff);
                                sums[2] += (((double)pixel[2]) * filter_coeff);
                                sums[3]  = pixel[3];
                            }
                        }

                        sums[0] *= scale;
                        sums[1] *= scale;
                        sums[2] *= scale;
                        sums[0] += bias;
                        sums[1] += bias;
                        sums[2] += bias;

                        PixelMath.pixelClamp(ref sums[0]);
                        PixelMath.pixelClamp(ref sums[1]);
                        PixelMath.pixelClamp(ref sums[2]);

                        dst_pixel[0] = (byte)sums[0];
                        dst_pixel[1] = (byte)sums[1];
                        dst_pixel[2] = (byte)sums[2];
                        dst_pixel[3] = (byte)sums[3];
                    }
                }
            }

            result.bitmap.UnlockBits(dstdata);
            image.UnlockBits(srcdata);

            return(result);
        }
コード例 #14
0
 public CvImage ProcessImage(CvImage image, IMAGE_ALGO_TYPES type)
 {
     return MatrixMath.Convolve(FilterTypes.GetType(type), image.bitmap);
 }
コード例 #15
0
        private CvImage DrawCannyEdges(Bitmap sobel_x, Bitmap sobel_y)
        {
            CvImage image = new CvImage();

            int[,] edge_direction = new int[sobel_x.Width, sobel_x.Height];
            int[,] edge_mag       = new int[sobel_x.Width, sobel_x.Height];
            int[,] non_max        = new int[sobel_x.Width, sobel_x.Height];
            int kernWid    = GAUSSIAN_FILTER.MATRIX.GetLength(1);
            int kernHei    = GAUSSIAN_FILTER.MATRIX.GetLength(0);
            int kernOffset = (kernWid - 1) / 2;

            for (int wid_ic = kernOffset; wid_ic < sobel_x.Width - kernOffset; wid_ic++)
            {
                for (int hei_ic = kernOffset; hei_ic < sobel_x.Height - kernOffset; hei_ic++)
                {
                    var pixel_y       = sobel_y.GetPixel(wid_ic, hei_ic);
                    var pixel_x       = sobel_x.GetPixel(wid_ic, hei_ic);
                    var mag_y         = PixelMath.pixelMag(pixel_y);
                    var mag_x         = PixelMath.pixelMag(pixel_y);
                    var edge_strength = mag_y + mag_x;

                    edge_mag[wid_ic, hei_ic] = (int)edge_strength;
                    non_max[wid_ic, hei_ic]  = (int)edge_strength;
                }
            }

            for (int wid_ic = kernWid; wid_ic < sobel_x.Width - kernWid; wid_ic++)
            {
                for (int hei_ic = kernHei; hei_ic < sobel_x.Height - kernHei; hei_ic++)
                {
                    var    pixel_y     = sobel_y.GetPixel(wid_ic, hei_ic);
                    var    pixel_x     = sobel_x.GetPixel(wid_ic, hei_ic);
                    var    mag_y       = PixelMath.pixelMag(pixel_y);
                    var    mag_x       = PixelMath.pixelMag(pixel_y);
                    var    current_mag = edge_mag[wid_ic, hei_ic];
                    double theta       = current_mag != 0 ? Math.Atan2(mag_y, mag_x) : 90;

                    //CHO_ACTIVE: clean this up
                    //near horizontal
                    if ((theta > -22.5 && theta <= 22.5) ||
                        (theta > 157.5) ||
                        (theta < -157.5))
                    {
                        if ((current_mag < edge_mag[wid_ic, hei_ic + 1]) ||
                            (current_mag < edge_mag[wid_ic, hei_ic - 1]))
                        {
                            non_max[wid_ic, hei_ic] = 0;
                        }
                    }
                    // +45 degree
                    else if ((theta > 22.5 && theta <= 67.5) ||
                             (theta <= -67.5 && theta > -22.5))
                    {
                        if ((current_mag < edge_mag[wid_ic + 1, hei_ic + 1]) ||
                            (current_mag < edge_mag[wid_ic - 1, hei_ic - 1]))
                        {
                            non_max[wid_ic, hei_ic] = 0;
                        }
                    }
                    //vertical
                    else if ((theta > 67.5 && theta <= 112.5) ||
                             (theta <= -67.5 && theta > -112.5))
                    {
                        if ((current_mag < edge_mag[wid_ic + 1, hei_ic]) ||
                            (current_mag < edge_mag[wid_ic - 1, hei_ic]))
                        {
                            non_max[wid_ic, hei_ic] = 0;
                        }
                    }
                    // +135 degree
                    else
                    {
                        if ((current_mag < edge_mag[wid_ic + 1, hei_ic - 1]) ||
                            (current_mag < edge_mag[wid_ic - 1, hei_ic + 1]))
                        {
                            non_max[wid_ic, hei_ic] = 0;
                        }
                    }

                    edge_direction[wid_ic, hei_ic] = (int)theta;
                }
            }

            image.bitmap = TraceCannyEdge(non_max);

            return(image);
        }
コード例 #16
0
ファイル: MatrixMath.cs プロジェクト: Cho-Chong/Vision_CSharp
        public static CvImage Convolve(Filter filter, Bitmap image)
        {
            CvImage result = new CvImage();
            var kernel = filter.MATRIX;
            var scale = filter.factor;
            var bias = filter.bias;

            result.bitmap = new Bitmap(image.Width, image.Height);
            int kernWid = kernel.GetLength(1);
            int kernHei = kernel.GetLength(0);
            var tile = new Rectangle(0, 0, image.Width, image.Height);
            BitmapData srcdata = image.LockBits(tile, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            BitmapData dstdata = result.bitmap.LockBits(tile, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            int srcStride = srcdata.Stride;
            int dstStride = dstdata.Stride;
            var bytesPerPixel = srcStride / image.Width;

            // we can't filter pixels without enough surrounding pixels
            int kernOffset = (kernWid - 1) / 2;

            //TODO: does not set non-filtered pixels
            unsafe
            {
                byte* dstPointer = (byte*)dstdata.Scan0;
                byte* srcPointer = (byte*)srcdata.Scan0;
                double[] sums = new double[bytesPerPixel];

                for (int offsetY = kernOffset; offsetY < image.Height - kernOffset; offsetY++)
                {
                    for (int offsetX = kernOffset; offsetX < image.Width - kernOffset; offsetX++)
                    {
                        for (int sum_ic = 0; sum_ic < bytesPerPixel; sum_ic++)
                        {
                            sums[sum_ic] = 0;
                        }

                        var dst_row = dstPointer + offsetY * srcStride;
                        var dst_pixel = dst_row + offsetX * bytesPerPixel;

                        for (int kernY = -kernOffset; kernY <= kernOffset; kernY++)
                        {
                            var row = srcPointer + (kernY + offsetY) * srcStride;

                            for (int kernX = -kernOffset; kernX <= kernOffset; kernX++)
                            {
                                var pixel = row + (kernX + offsetX) * bytesPerPixel;
                                double filter_coeff = (kernel[kernX + kernOffset, kernY + kernOffset]);
                                sums[0] += (((double)pixel[0]) * filter_coeff);
                                sums[1] += (((double)pixel[1]) * filter_coeff);
                                sums[2] += (((double)pixel[2]) * filter_coeff);
                                sums[3] = pixel[3];
                            }
                        }

                        sums[0] *= scale;
                        sums[1] *= scale;
                        sums[2] *= scale;
                        sums[0] += bias;
                        sums[1] += bias;
                        sums[2] += bias;

                        PixelMath.pixelClamp(ref sums[0]);
                        PixelMath.pixelClamp(ref sums[1]);
                        PixelMath.pixelClamp(ref sums[2]);

                        dst_pixel[0] = (byte)sums[0];
                        dst_pixel[1] = (byte)sums[1];
                        dst_pixel[2] = (byte)sums[2];
                        dst_pixel[3] = (byte)sums[3];
                    }
                }
            }

            result.bitmap.UnlockBits(dstdata);
            image.UnlockBits(srcdata);

            return result;
        }
コード例 #17
0
 public CvImage ProcessImage(CvImage image, IMAGE_ALGO_TYPES type)
 {
     return(MatrixMath.Convolve(FilterTypes.GetType(type), image.bitmap));
 }