public Bgra32Image(Bgra32Image rhs)
        {
            if (rhs == this)
                return;

            this.imgInfo = rhs.imgInfo;
            this.imgPixels = rhs.imgPixels.Clone() as byte[];
        }
        private void LoadImg(BitmapSource img)
        {
            /*
             * 笔记:
             *
             * 最开始尝试使用 System.drawing 实现转换为 byte,想了想还是算了(各种转化),这些是当时的资料
             * 参考资料:
             * [Convert a bitmap into a byte array](http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array?lq=1)
             * [System.drawing namespace not found under console application](http://stackoverflow.com/questions/8553136/system-drawing-namespace-not-found-under-console-application)
             *
             * 最后决定还是直接转换 BitmapImage 到 byte[]
             *
             * 然后翻了一下这个 [BitmapImage to byte[]](http://stackoverflow.com/questions/6597676/bitmapimage-to-byte)
             * (虽然最后并没有用上,但是知道了如何转换 BitmapImage 的格式)
             *
             * 最后,参考资料:
             * [Finding specific pixel colors of a BitmapImage](http://stackoverflow.com/questions/1176910/finding-specific-pixel-colors-of-a-bitmapimage)
             * 这里强行把图片换成了 PixelFormats.Bgra32,同时使用 CopyPixels 获取各个像素信息到 byte[] 中,
             * 这样就可以交给外部方法处理了。
             */

            /* 保存 Bgra32 版本的像素 */
            bgraImgSrc = new Bgra32Image(img);
            //BgraImgNow = new Bgra32Image(BgraImgSrc);

            /* 保存 8bit 灰度图的版本 */
            gray8ImgSrc = new Gray8Image(img);

            /* 根据图片大小,确定是否实时计算 */
            realtimeProcessing = (img.PixelWidth * img.PixelHeight) < (imgWidMax * imgHgtMax);

            /* 确定采样的上限值 */
            sliderSampling.Maximum = Math.Max(img.PixelWidth, img.PixelHeight);

            /* 调整窗口大小 */
            ResizeForImg(this.imgSrc);

            /* 显示原图像 */
            OriginImg(this.imgSrc);

            /* 启用 保存 按钮 */
            try {
                if (ImgFunc.is_loaded())
                {
                    EnbaleComponent();
                }

                /* 刷新直方图 */
                UpdateHistogram();
            } catch (Exception e) {
                MessageBox.Show(
                    e.Message,
                    "加载 ImgFuncLib 时发生错误",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                    );
            }
        }
Exemple #3
0
        public Bgra32Image(Bgra32Image rhs)
        {
            if (rhs == this)
            {
                return;
            }

            this.imgInfo   = rhs.imgInfo;
            this.imgPixels = rhs.imgPixels.Clone() as byte[];
        }
        private void comboBox_Filter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.Source == comboBox_Filter)
            {
                switch (comboBox_Filter.SelectedIndex)
                {
                case 0: { filter_matrix = new double[, ] {
                              { 0, -1, 0 },
                              { -1, 4, -1 },
                              { 0, -1, 0 },
                          }; break; }

                case 1: { filter_matrix = new double[, ] {
                              { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 },
                              { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 },
                              { 1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 },
                          }; break; }

                case 2: { filter_matrix = new double[, ] {
                              { 1 / 10.0, 1 / 10.0, 1 / 10.0 },
                              { 1 / 10.0, 2 / 10.0, 1 / 10.0 },
                              { 1 / 10.0, 1 / 10.0, 1 / 10.0 },
                          }; break; }

                case 3: { filter_matrix = new double[, ] {
                              { 1 / 16.0, 1 / 8.0, 1 / 16.0 },
                              { 1 / 8.0, 1 / 4.0, 1 / 8.0 },
                              { 1 / 16.0, 1 / 8.0, 1 / 16.0 },
                          }; break; }

                case 4: { filter_matrix = new double[, ] {
                              { 0, -1, 0 },
                              { -1, 5, -1 },
                              { 0, -1, 0 },
                          }; break; }

                case 5: { filter_matrix = new double[, ] {
                              { -1, -1, -1 },
                              { -1, 9, -1 },
                              { -1, -1, -1 },
                          }; break; }

                case 6: { filter_matrix = new double[, ] {
                              { 1, -2, 1 },
                              { -2, 5, -2 },
                              { 1, -2, 1 },
                          }; break; }

                case 7: { filter_matrix = new double[, ] {
                              { 0, 0, 0 },
                              { -1, 1, 0 },
                              { 0, 0, 0 },
                          }; break; }

                case 8: { filter_matrix = new double[, ] {
                              { 0, -1, 0 },
                              { 0, 1, 0 },
                              { 0, 0, 0 },
                          }; break; }

                case 9: { filter_matrix = new double[, ] {
                              { -1, 0, 0 },
                              { 0, 1, 0 },
                              { 0, 0, 0 },
                          }; break; }

                case 10: { filter_matrix = new double[, ] {
                               { -1, -1, -1, -1, -1 },
                               { 0, 0, 0, 0, 0 },
                               { 1, 1, 1, 1, 1 },
                           }; break; }

                case 11: { filter_matrix = new double[, ] {
                               { -1, 0, 1 },
                               { -1, 0, 1 },
                               { -1, 0, 1 },
                               { -1, 0, 1 },
                               { -1, 0, 1 },
                           }; break; }

                case 12: { filter_matrix = new double[, ] {
                               { -1, 0, -1 },
                               { 0, 4, 0 },
                               { -1, 0, -1 },
                           }; break; }

                case 13: { filter_matrix = new double[, ] {
                               { -1, -1, -1 },
                               { -1, 8, -1 },
                               { -1, -1, -1 },
                           }; break; }

                case 14: { filter_matrix = new double[, ] {
                               { -1, -1, -1 },
                               { -1, 9, -1 },
                               { -1, -1, -1 },
                           }; break; }

                case 15: { filter_matrix = new double[, ] {
                               { 1, -2, -1 },
                               { -2, 4, -2 },
                               { 1, -2, -1 },
                           }; break; }

                default: break;
                }

                datagrid_FilterMatrix.ItemsSource = ImgFunc.ConvertArray2DToDataTable(filter_matrix).AsDataView();
                if (realtimeProcessing)
                {
                    buttonFilterCalc_Click(null, null);
                }
            }
            else if (e.Source == comboBox_FilterOther)
            {
                BitmapSource img = null;
                switch (comboBox_FilterOther.SelectedIndex)
                {
                case 0: img = new Bgra32Image(this.bgraImgSrc).Filter("median").ToBitmapSource(); break;

                case 1: img = new Bgra32Image(this.bgraImgSrc).Filter("roberts").ToBitmapSource(); break;

                case 2: img = new Bgra32Image(this.bgraImgSrc).Filter("sobel").ToBitmapSource(); break;
                }
                if (img != null)
                {
                    this.imageView.Source = this.imgRes = img;
                }
            }
        }
        private void LoadImg(BitmapSource img)
        {
            /*
             * 笔记:
             *
             * 最开始尝试使用 System.drawing 实现转换为 byte,想了想还是算了(各种转化),这些是当时的资料
             * 参考资料:
             * [Convert a bitmap into a byte array](http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array?lq=1)
             * [System.drawing namespace not found under console application](http://stackoverflow.com/questions/8553136/system-drawing-namespace-not-found-under-console-application)
             *
             * 最后决定还是直接转换 BitmapImage 到 byte[]
             *
             * 然后翻了一下这个 [BitmapImage to byte[]](http://stackoverflow.com/questions/6597676/bitmapimage-to-byte)
             * (虽然最后并没有用上,但是知道了如何转换 BitmapImage 的格式)
             *
             * 最后,参考资料:
             * [Finding specific pixel colors of a BitmapImage](http://stackoverflow.com/questions/1176910/finding-specific-pixel-colors-of-a-bitmapimage)
             * 这里强行把图片换成了 PixelFormats.Bgra32,同时使用 CopyPixels 获取各个像素信息到 byte[] 中,
             * 这样就可以交给外部方法处理了。
             */

            /* 保存 Bgra32 版本的像素 */
            bgraImgSrc = new Bgra32Image(img);
            //BgraImgNow = new Bgra32Image(BgraImgSrc);

            /* 保存 8bit 灰度图的版本 */
            gray8ImgSrc = new Gray8Image(img);

            /* 根据图片大小,确定是否实时计算 */
            realtimeProcessing = (img.PixelWidth * img.PixelHeight) < (imgWidMax * imgHgtMax);

            /* 确定采样的上限值 */
            sliderSampling.Maximum = Math.Max(img.PixelWidth, img.PixelHeight);

            /* 调整窗口大小 */
            ResizeForImg(this.imgSrc);

            /* 显示原图像 */
            OriginImg(this.imgSrc);

            /* 启用 保存 按钮 */
            try {
                if(ImgFunc.is_loaded())
                    EnbaleComponent();

                /* 刷新直方图 */
                UpdateHistogram();
            } catch(Exception e) {
                MessageBox.Show(
                    e.Message,
                    "加载 ImgFuncLib 时发生错误",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error
                    );
            }
        }
        private void comboBox_Filter_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(e.Source == comboBox_Filter) {
                switch(comboBox_Filter.SelectedIndex) {
                case 0: { filter_matrix = new double[,] {
                    {  0, -1,  0 },
                    { -1,  4, -1 },
                    {  0, -1,  0 },
                }; break; }
                case 1: { filter_matrix = new double[,] {
                    {1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 },
                    {1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 },
                    {1.0 / 9.0, 1.0 / 9.0, 1.0 / 9.0 },
                }; break; }
                case 2: {filter_matrix = new double[,] {
                    {1 / 10.0, 1 / 10.0, 1 / 10.0 },
                    {1 / 10.0, 2 / 10.0, 1 / 10.0 },
                    {1 / 10.0, 1 / 10.0, 1 / 10.0 },
                }; break;}
                case 3: {filter_matrix = new double[,] {
                    {1 / 16.0, 1 / 8.0, 1 / 16.0 },
                    {1 /  8.0, 1 / 4.0, 1 /  8.0 },
                    {1 / 16.0, 1 / 8.0, 1 / 16.0 },
                }; break;}
                case 4: {filter_matrix = new double[,] {
                    {  0, -1,  0 },
                    { -1,  5, -1 },
                    {  0, -1,  0 },
                }; break;}
                case 5: {filter_matrix = new double[,] {
                    { -1, -1, -1 },
                    { -1,  9, -1 },
                    { -1, -1, -1 },
                }; break;}
                case 6: {filter_matrix = new double[,] {
                    {  1, -2,  1 },
                    { -2,  5, -2 },
                    {  1, -2,  1 },
                }; break;}
                case 7: {filter_matrix = new double[,] {
                    {  0,  0,  0 },
                    { -1,  1,  0 },
                    {  0,  0,  0 },
                }; break;}
                case 8: {filter_matrix = new double[,] {
                    {  0, -1,  0 },
                    {  0,  1,  0 },
                    {  0,  0,  0 },
                }; break;}
                case 9: {filter_matrix = new double[,] {
                    { -1,  0,  0 },
                    {  0,  1,  0 },
                    {  0,  0,  0 },
                }; break;}
                case 10: {filter_matrix = new double[,] {
                    { -1, -1, -1, -1, -1 },
                    {  0,  0,  0,  0,  0 },
                    {  1,  1,  1,  1,  1 },
                }; break;}
                case 11: {filter_matrix = new double[,] {
                    { -1, 0, 1 },
                    { -1, 0, 1 },
                    { -1, 0, 1 },
                    { -1, 0, 1 },
                    { -1, 0, 1 },
                }; break;}
                case 12: {filter_matrix = new double[,] {
                    { -1,  0, -1 },
                    {  0,  4,  0 },
                    { -1,  0, -1 },
                }; break;}
                case 13: {filter_matrix = new double[,] {
                    { -1, -1, -1 },
                    { -1,  8, -1 },
                    { -1, -1, -1 },
                }; break;}
                case 14: {filter_matrix = new double[,] {
                    { -1, -1, -1 },
                    { -1,  9, -1 },
                    { -1, -1, -1 },
                }; break;}
                case 15: {filter_matrix = new double[,] {
                    {  1, -2, -1 },
                    { -2,  4, -2 },
                    {  1, -2, -1 },
                }; break;}
                default: break;
                }

                datagrid_FilterMatrix.ItemsSource = ImgFunc.ConvertArray2DToDataTable(filter_matrix).AsDataView();
                if(realtimeProcessing)
                    buttonFilterCalc_Click(null, null);
            } else if(e.Source == comboBox_FilterOther) {
                BitmapSource img = null;
                switch(comboBox_FilterOther.SelectedIndex) {
                case 0: img = new Bgra32Image(this.bgraImgSrc).Filter("median").ToBitmapSource(); break;
                case 1: img = new Bgra32Image(this.bgraImgSrc).Filter("roberts").ToBitmapSource(); break;
                case 2: img = new Bgra32Image(this.bgraImgSrc).Filter("sobel").ToBitmapSource(); break;
                }
                if(img != null)
                    this.imageView.Source = this.imgRes = img;
            }
        }