当前位置:编程学习 > C#/ASP.NET >>

关于c#图像处理的问题

请问有谁知道图像处理中descreen的算法或源代码吗? 或者哪儿有文章介绍方法也行,谢谢了! --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace GriphicProcess
{
    public partial class Form1 : Form
    {
        Bitmap originalBt;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)//打开一个图像文件
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "位图文件(*.bmp)|*.bmp|Jpeg文件(*.jpg)|*.jpg";//判断是否为指定类型的图片文件
            ofd.FilterIndex = 2;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                
                pictureBox1.Image = System.Drawing.Image.FromFile(ofd.FileName.ToString());
                //pictureBox1.Image = System.Drawing.Image.FromFile(ofd.FileName.ToString());
            }

            originalBt = new Bitmap(pictureBox1.Image);//将图片框内的图像赋给一个变量对象


        }

        private void btnFD_Click(object sender, EventArgs e)//图像的浮雕处理
        {
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("错误,没有导入图片!");
                return;
            }
            //声明两个色彩对象
            Color c = new Color();
            Color cc = new Color();
            //声明两个位图的对象,分别用两个pictureBox1的图像进行初始化
            Bitmap bt = new Bitmap(pictureBox1.Image);
            Bitmap bt1 = new Bitmap(pictureBox1.Image);
           
            //声明几个变量用于保存各个像素的RGB值
            int rr, gg, bb, r1, g1, b1, r2, g2, b2;
            //做一个双重循环这样可以获取图像的每一个像素,然后修改各个像素的RGB达到改变图像形态的目的
            for (int i = 0; i < bt1.Width-1; i++)
            {
                for (int j = 0; j < bt1.Height-1; j++)
                {
                    //每次对要改变像素的RGB值进行初始化
                    rr = 0; gg = 0; bb = 0;
                    //通过方法获取像素
                    c = bt.GetPixel(i,j);
                    //获取颜色对象的RGB值分别保存三个变量中
                    r1 = c.R;
                    g1 = c.G;
                    b1 = c.B;

                    cc = bt.GetPixel(i+1,j+1);
                    r2 = cc.R;
                    g2 = cc.G;
                    b2 = cc.B;
                    //通过对图像相邻像素点的像素值分别与相邻像素点的像素值相减之后加上一个常量128
                    rr = r2-r1+128;
                    gg = g2 - g1 + 128;
                    bb = b2 - b1 + 128;
                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    if (bb < 0) bb = 0;
                    if (bb > 255) bb = 255;
                    //生成一个经处理后的颜色对象
                    Color ccc = Color.FromArgb(rr,gg,bb);
                    //设置位图对象在坐标为(i,j)处的新的像素
                    bt1.SetPixel(i,j,ccc);

                }
                //每过一列就刷新一次在屏幕上能看到渐变的动态效果
                pictureBox1.Refresh();
                //把新的图像赋值给pictureBox1
                pictureBox1.Image = bt1;
                
            }
        }
        //以下处理与第一个类似,都是获取每一个像素然后根据每种类型的算法修改对应像素的RGB
        private void btnXH_Click(object sender, EventArgs e)
        {
            //图像的霞虹处理
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("错误,没有导入图片!");
                return;
            }
            Color c = new Color();
            Color cc = new Color();
            Color ccc = new Color();

            //声明两个位图对象并用图片框中的图片初始化它
            Bitmap bt = new Bitmap(pictureBox1.Image);
            Bitmap bt1 = new Bitmap(pictureBox1.Image);
            int r1, r2, r3, rr, g1, g2, g3, gg, b1, b2, b3, bb;
            //逐个扫描原始图片的像素
            for (int i = 0; i < bt1.Width-1; i++)
            {
                for (int j = 0; j < bt1.Height-1; j++)
                {
                    rr = 0; gg = 0; bb = 0;
                    c = bt.GetPixel(i,j);
                    r1 = c.R;
                    g1 = c.G;
                    b1 = c.B;
                    cc = bt.GetPixel(i+1,j);
                    r2 = cc.R;
                    g2 = cc.G;
                    b2 = cc.B;
                    ccc = bt.GetPixel(i, j + 1);
                    r3 = ccc.R;
                    g3 = ccc.G;
                    b3 = ccc.B;
                    //霞虹的RGB值的转换算法
                    rr = (int)(2 * Math.Sqrt((r1 - r2) * (r1 - r2) + (r1 - r3) * (r1 - r3)));
                    gg = (int)(2 * Math.Sqrt((g1 - g2) * (g1 - g2) + (g1 - g3) * (g1 - g3)));
                    bb = (int)(2 * Math.Sqrt((b1 - b2) * (b1 - b2) + (b1 - b3) * (b1 - b3)));
                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    if (bb < 0) bb = 0;
                    if (bb > 255) bb = 255;
                    Color c1 = Color.FromArgb(rr,gg,bb);
                    //把新的像素赋值bt1位图对象在坐标为(i,j)的像素
                    bt1.SetPixel(i,j,c1);
                    
                }
                //每处理一列就进行刷新这样可以动态的显示效果
                pictureBox1.Refresh();
                //把bt1位图对象赋值给图像框
                pictureBox1.Image = bt1;
            }
        } --------------------编程问答-------------------- (接上)
  private void btnRH_Click(object sender, EventArgs e)
        {
            /*图像的锐化就是要显示图像中有关形体的边缘。
             * 所谓形体的边缘就是图像像素点的颜色值发生显著变化的地方,在图像的平淡区,这种颜色值的变化比较平缓,
             * 而在图像的边缘区域这种变化相当明显。也就是说在平缓区,相邻两像素的颜色值的差值较小,而在边缘区域,相邻两像素的颜色值变化抖得多,因而
             * 在边缘区域处理这
             * 个数值可以使突出效果更加突出,而在非边缘区域而使图像变得较暗,即图像的锐化。拉普拉斯模块法
            */
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("错误,没有导入图片!");
                return;
            }
            Color c = new Color();
           
            //声明两个位图对象并用图片框中的图片初始化它
            Bitmap btm = new Bitmap(pictureBox1.Image);
            Bitmap btm1 = new Bitmap(pictureBox1.Image);
            int  rr,  gg,  bb;
            //拉普拉斯模板
            int[] Laplacian ={ -1, -1, -1, -1, 9, -1, -1, -1, -1 };
            //逐个扫描原始图片的像素
            for (int i = 1; i < btm1.Width-1; i++)
            {
                for (int j = 1; j < btm1.Height-1; j++)
                {
                    int Index = 0;
                    rr = 0; gg = 0; bb = 0;
                    //获取位于(i,j)坐标的像素然后提取RGB分量
                    for (int col = -1; col <= 1; col++)
                    {
                        for (int row = -1; row <= 1; row++)
                        {
                            c = btm.GetPixel(i + row, j + col);
                            rr += c.R * Laplacian[Index];
                            gg += c.G * Laplacian[Index];
                            bb += c.B * Laplacian[Index];
                            Index++;
                        }
                    }
                    //处理颜色值溢出
                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    if (bb < 0) bb = 0;
                    if (bb > 255) bb = 255;
                    Color c1 = Color.FromArgb(rr, gg, bb);
                    btm1.SetPixel(i, j, c1);
                 }
                 pictureBox1.Refresh();
                 pictureBox1.Image = btm1;
            }
        }

        private void btngradient_Click(object sender, EventArgs e)
        {
            //该算法利用了Roberts算子进行计算
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("错误,没有导入图片!");
                return;
            }
            //声明九个color对象以备后面用到
            Color c1 = new Color();
            Color c2 = new Color();
            Color c3 = new Color();
            Color c4 = new Color();
            Color c5 = new Color();
            Color c6 = new Color();
            Color c7 = new Color();
            Color c8 = new Color();
            Color c9 = new Color();
            int r1, r2, r3, r4,r5,r6,r7,r8,r9,rr, g1, g2, g3, g4,g5, gg, b1, b2, b3, b4,b5, bb, fxr, fyr, fyg, fxg, fxb, fyb;
            //声明两个位图对象并用图片框中的图片初始化它
            Bitmap btm = new Bitmap(pictureBox1.Image);
            Bitmap btm1 = new Bitmap(pictureBox1.Image);
            if (rbRoberts.Checked == true)
            {
                //逐个扫描原始图片的像素
                for (int i = 0; i < btm1.Width-1; i++)
                {
                    for (int j = 0; j < btm1.Height-1; j++)
                    {
                        //获取临近的四个像素2×2像素块
                        c1 = btm.GetPixel(i, j);
                        c2 = btm.GetPixel(i, j + 1);
                        c3 = btm.GetPixel(i + 1, j);
                        c4 = btm.GetPixel(i + 1, j + 1);
                        /*分别提取其RGB三个分量值然后通过一下公式进行计算
                         * 通过临近像素算出输出的像素g1=f(i,j)-f(i+1,j)
                         * g2=f(i+1,j)-f(i,j+1)
                         * g(i,j)=abs(g1)+abs(g2)
                         */
                        r1 = c1.R;
                        r2 = c2.R;
                        r3 = c3.R;
                        r4 = c4.R;
                        fxr = r1 - r4;
                        fyr = r3 - r2;
                        rr = Math.Abs(fxr) + Math.Abs(fyr) + 128;
                        if (rr < 0) rr = 0;
                        if (rr > 255) rr = 255;
                        g1 = c1.G;
                        g2 = c2.G;
                        g3 = c3.G;
                        g4 = c4.G;
                        fxg = g1 - g4;
                        fyg = g3 - g2;
                        gg = Math.Abs(fxg) + Math.Abs(fyg) + 128;
                        if (gg < 0) gg = 0;
                        if (gg > 255) gg = 255;
                        b1 = c1.B;
                        b2 = c2.B;
                        b3 = c3.B;
                        b4 = c4.B;
                        fxb = b1 - b4;
                        fyb = b3 - b2;
                        bb = Math.Abs(fxb) + Math.Abs(fyb) + 128;
                        if (bb < 0) bb = 0;
                        if (bb > 255) bb = 255;
                        Color color = Color.FromArgb(rr, gg, bb);
                        //把新的像素赋值bt1位图对象在坐标为(i,j)的像素
                        btm1.SetPixel(i, j, color);

                    }
                    pictureBox1.Refresh();
                    //把btm1位图对象赋值给图像框
                    pictureBox1.Image = btm1;
                }
            }
            else if (rbSobel.Checked == true)
            {
                //采用Sobel算子,该算子的算法思想是通过获取要考察点的上下,左右灰度的加权值作为输出从而提取图像的边缘
                int col, row;

                for (int i = 1; i < btm1.Width - 1; i++)
                {
                    for (int j = 1; j < btm1.Height - 1; j++)
                    {
                        c1 = btm.GetPixel(i,j-1);
                        c2 = btm.GetPixel(i-1, j);
                        c3 = btm.GetPixel(i, j);
                        c4 = btm.GetPixel(i+1, j);
                        c5 = btm.GetPixel(i, j + 1);
                        c6 = btm.GetPixel(i-1, j - 1);
                        c7 = btm.GetPixel(i-1, j + 1);
                        c8 = btm.GetPixel(i+1, j - 1);
                        c9 = btm.GetPixel(i+1, j + 1);
                        r1 = c1.R;
                        r2 = c2.R;
                        r3 = c3.R;
                        r4 = c4.R;
                        r5 = c5.R;
                        r6 = c6.R;
                        r7 = c7.R;
                        r8 = c8.R;
                        r9 = c9.R;
                        //设输出为g(i,j),g(i,j)=x+y;利用公式
                        //x = f(i-1,j-1) + 2 * f(i-1,j) + f(i-1,j+1) - f(i+1,j-1) - 2*f(i+1,j) - f(i+1,j+1);
                        //y = f(i-1,j-1) + 2 * f(i,j-1) + f(i+1,j-1) - f(i-1,j+1) - 2*f(i,j+1) - f(i+1,j+1);
                        col = r6 + 2 * r2 + r7 - r8 - 2 * r4 - r9;
                        row = r6 + 2 * r1 + r8 - r7 - 2 * r5 - r9;
                        rr = Math.Abs(col) + Math.Abs(row);
                        if (rr < 0) rr = 0;
                        if (rr > 255) rr = 255;
                        Color color = Color.FromArgb(rr, rr, rr);
                        btm1.SetPixel(i, j,color);
                    }
                    pictureBox1.Refresh();
                    pictureBox1.Image = btm1;
                }

            }


       --------------------编程问答-------------------- else if (rbLPLD.Checked == true)
            {
                //采用拉普拉多算子的四邻域微分算子形式
                int x, y, z;
                //循环获取临近的四个像素
                for (int i = 1; i < btm1.Width - 1; i++)
                {
                    for (int j = 1; j < btm1.Height - 1; j++)
                    {
                        c1 = btm.GetPixel(i, j - 1);
                        c2 = btm.GetPixel(i - 1, j);
                        c3 = btm.GetPixel(i, j);
                        c4 = btm.GetPixel(i + 1, j);
                        c5 = btm.GetPixel(i, j + 1);
                        r1 = c1.R;
                        r2 = c2.R;
                        r3 = c3.R;
                        r4 = c4.R;
                        r5 = c5.R;
                        //该算法思想是根据输入的图像f(i,j),算子输出图像为g(i,j)
                        //g(i,j)=abs(4*f(i,j)-f(i,j-1)-f(i-1,j)-f(i+1,j)-f(i,j+1))
                        x = 4 * r3 - r1 - r2 - r4 - r5;
                        rr = Math.Abs(x) * 5 + 128;
                        
                        if (rr < 0) rr = 0;
                        if (rr > 255) rr = 255;
                        g1 = c1.G;
                        g2 = c2.G;
                        g3 = c3.G;
                        g4 = c4.G;
                        g5 = c5.G;
                        //分别求出g分量的值
                        y = 4 * g3 - g1 - g2 - g4 - g5;
                        gg = Math.Abs(y)*5+128;

                       
                        if (gg < 0) gg= 0;
                        if (gg > 255) gg = 255;
                        b1 = c1.B;
                        b2 = c2.B;
                        b3 = c3.B;
                        b4 = c4.B;
                        b5 = c5.B;
                        //求出b分量的值
                        z= 4 * b3 - b1 - b2 - b4 - b5;
                        bb = Math.Abs(z)*5+128;
                     //溢出判断
                        if (bb < 0) bb = 0;
                        if (bb > 255) bb = 255;
                        Color color = Color.FromArgb(rr,gg, bb);
                        btm1.SetPixel(i, j, color);
                    }
                    pictureBox1.Refresh();
                    pictureBox1.Image = btm1;
                }
            }



        }

        private void btnPZ_Click(object sender, EventArgs e)
        {
            //二值化图像的膨胀处理
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("错误,没有导入图片!");
                return;
            }
            Color c = new Color();
            //声明两个位图对象并用图片框中的图片初始化它
            Bitmap btm = new Bitmap(pictureBox1.Image);
            Bitmap btm1 = new Bitmap(pictureBox1.Image);
            int rr;
            //逐个扫描原始图片的像素
            for (int i = 1; i < btm1.Width - 1;i++ )
            {
                for (int j = 1; j < btm1.Height - 1; j++)
                {
                    //获取位于(i,j)坐标的像素然后提取RGB分量
                    c = btm.GetPixel(i,j);
                    rr = c.R;
                    Color color = Color.FromArgb(rr,rr,rr);
                    btm1.SetPixel(i,j,color);
                    if(rr==0)
                    {
                        //rr=0 设置四邻域的值与该点的值相同达到膨胀的效果
                        btm1.SetPixel(i, j, color);
                        btm1.SetPixel(i, j-1, color);
                        btm1.SetPixel(i+1, j, color);
                        btm1.SetPixel(i, j+1, color);
                        btm1.SetPixel(i - 1, j, color);
                    }
                }
                //每处理一列就进行刷新这样可以动态的显示效果
                pictureBox1.Refresh();
                //把btm1位图对象赋值给图像框
                pictureBox1.Image = btm1;
            }
        }

        private void btnFS_Click(object sender, EventArgs e)
        {
            //对图像进行腐蚀操作
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("错误,没有导入图片!");
                return;
            }
            Color c = new Color();
            //声明两个位图对象并用图片框中的图片初始化它
            
            Bitmap btm1 = new Bitmap(pictureBox1.Image);
            int rr;
            //逐个扫描原始图片的像素
            for (int i = 1; i < btm1.Width - 2; i=i+2)
            {
                for (int j = 1; j < btm1.Height - 2;j=j+2 )
                {
                  
                    //把新的像素赋值bt1位图对象在坐标为(i,j)的像素
                    c = btm1.GetPixel(i, j);
                    rr = c.R;
                    Color color = Color.FromArgb(rr, rr, rr);
                    btm1.SetPixel(i, j, color);
                    if (rr == 255)
                    {
                       
                        //把新的像素赋值bt1位图对象在坐标为(i,j)的像素的四临域的图像值赋值为该点的值
                        btm1.SetPixel(i, j, color);
                        btm1.SetPixel(i, j - 1, color);
                        btm1.SetPixel(i + 1, j, color);
                        btm1.SetPixel(i, j + 1, color);
                        btm1.SetPixel(i - 1, j, color);
                    }
                }
                //每处理一列就进行刷新这样可以动态的显示效果
                pictureBox1.Refresh();
                //把bt1位图对象赋值给图像框
                pictureBox1.Image = btm1;
            }
        }
 private void btnEXIT_Click(object sender, EventArgs e)
        {
            //退出程序
            Application.Exit();
        }

        private void btnHD_Click(object sender, EventArgs e)
        {
            //图像的灰度化
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("错误,没有导入图片!");
                return;
            }
            Bitmap bt = new Bitmap(pictureBox1.Image);
            Bitmap bt1 = new Bitmap(pictureBox1.Image);
            Color color = new Color();
        
            for (int i = 0; i < bt.Width; i++)
            {
                for (int j = 0; j < bt.Height; j++)
                {
                    color = bt.GetPixel(i, j);
                     //根据GRB的不同的权值生成新的灰度值
                    int n = (int)((color.G * 59 + color.R * 30 + color.B * 11) / 100);
                    bt1.SetPixel(i,j,Color.FromArgb(n,n,n));
                }
                pictureBox1.Refresh();
                pictureBox1.Image = bt1;
            }



        }
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,