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

图像处理指针调用怎么出现问题?

大神,帮我看看这段代码出现什么问题。
谁能帮我看看c#做的这段代码有什么问题吗
                   IntPtr ptrinput = bitinputData.Scan0;
                    IntPtr ptroutput = bitoutputData.Scan0;
                    int bytes = bitinputData.Height * bitinputData.Stride;
                    byte[] grayValue = new byte[bytes];
                    byte[] tempValue = new byte[bytes];
                    System.Runtime.InteropServices.Marshal.Copy(ptrinput, grayValue, 0, bytes);
        
                    for (int i = 0; i < bitinput.Height-1; i++)
                    {
                        for (int j = 0; j < bitinput.Width-1; j++)
                            tempValue[i * bitinputData.Stride + j] = grayValue[i * bitinputData.Stride + j];
                    }

为什么显示的图像是几个小的图像。而且这个循环是否有问题。书上都是这样的,为什么我一直得不了正确图像。图像使用的是经过变化后的灰度图像。 图像处理 C# --------------------编程问答-------------------- 能否发相对完整点的代码上来 --------------------编程问答--------------------    private void scoring_Click(object sender, EventArgs e)
        {
            Bitmap bitinput1;
            string bitNamefile;
            OpenFileDialog OpenDig = new OpenFileDialog();

            if (OpenDig.ShowDialog() == DialogResult.OK)
            {
                bitNamefile = OpenDig.FileName;
               bitinput1 = (Bitmap)Image.FromFile(bitNamefile);
               Bitmap bitinput = new Bitmap(bitinput1.Width, bitinput1.Height, PixelFormat.Format8bppIndexed);
               bitinput = RGB(bitinput1);
             //  pictureBox1.Image = bitinput1;
               Bitmap bitoutput = new Bitmap(bitinput.Width, bitinput.Height);
               BitmapData bitinputData = bitinput.LockBits(new Rectangle(0, 0, bitinput.Width, bitinput.Height), ImageLockMode.ReadWrite, bitinput.PixelFormat);
               BitmapData bitoutputData = bitoutput.LockBits(new Rectangle(0, 0, bitoutput.Width, bitoutput.Height), ImageLockMode.ReadWrite, bitoutput.PixelFormat);
               unsafe
               {
                   IntPtr ptrinput = bitinputData.Scan0;
                    IntPtr ptroutput = bitoutputData.Scan0;
                    int bytes = bitinputData.Height * bitinputData.Stride;
                    byte[] grayValue = new byte[bytes];
                    byte[] tempValue = new byte[bytes];
                    System.Runtime.InteropServices.Marshal.Copy(ptrinput, grayValue, 0, bytes);
        
                    for (int i = 0; i < bitinput.Height-1; i++)
                    {
                        for (int j =3; j < (bitinput.Width-1)*3; j+=3)
                            tempValue[i * bitinputData.Stride + j+3] = grayValue[i * bitinputData.Stride + j+3];
                    }

                    //   middlefilter(ptrinput, bitinput.Width, bitinput.Height, bitinputData.Stride);

                    //Sobel(ptrinput, bitinput.Width, bitinput.Height, bitinputData.Stride);


                    System.Runtime.InteropServices.Marshal.Copy(tempValue, 0,ptroutput, bytes);

                }

               bitinput.UnlockBits(bitinputData);
               bitoutput.UnlockBits(bitoutputData);
               pictureBox1.Image = bitoutput;
            }
        } --------------------编程问答-------------------- tempValue[i * bitinputData.Stride + j] = grayValue[i * bitinputData.Stride + j];这个错了,应该是j*3 --------------------编程问答-------------------- tempValue[i * bitinputData.Stride + j*3] = grayValue[i * bitinputData.Stride + j*3];
tempValue[i * bitinputData.Stride + j*3+1] = grayValue[i * bitinputData.Stride + j*3+1];
tempValue[i * bitinputData.Stride + j*3+2] = grayValue[i * bitinputData.Stride + j*3+2]; --------------------编程问答-------------------- 给你个博客网址,很不错的http://dongtingyueh.blog.163.com/blog/#m=0&t=1&c=fks_084065083084089066081081087095080080080074080083085 --------------------编程问答--------------------
引用 4 楼 Trent1985 的回复:
tempValue[i * bitinputData.Stride + j*3] = grayValue[i * bitinputData.Stride + j*3];
tempValue[i * bitinputData.Stride + j*3+1] = grayValue[i * bitinputData.Stride + j*3+1];
tempValue[i * bitinputData.Stride + j*3+2] = grayValue[i * bitinputData.Stride + j*3+2];

你这个不对的,它的图片是一个8位位图,像素值是一个256调色板索引 --------------------编程问答-------------------- 我使用的是经过RGB转换成灰度的图像,那现在的图像应该是8位位图了,那我使用还需要*3吗 --------------------编程问答-------------------- 这个是我的完整程序,是进行边缘提取,在进行soble算法时出现问题,图像无法生成。
  private void scoring_Click(object sender, EventArgs e)
        {
            Bitmap bitinput1;
            string bitNamefile;
            OpenFileDialog OpenDig = new OpenFileDialog();

            if (OpenDig.ShowDialog() == DialogResult.OK)
            {
                bitNamefile = OpenDig.FileName;
               bitinput1= (Bitmap)Image.FromFile(bitNamefile);
               Bitmap bitinput = new Bitmap(bitinput1.Width, bitinput1.Height, PixelFormat.Format8bppIndexed);
                
             bitinput = RGB(bitinput1);
             //if (bitinput.PixelFormat == PixelFormat.Format8bppIndexed)
             //{
             //    pictureBox1.Image = bitinput;
             //}
             //else
             //{
             //    MessageBox.Show("不是8位的");
             //}
               Bitmap bitoutput = new Bitmap(bitinput.Width, bitinput.Height);
               BitmapData bitinputData = bitinput.LockBits(new Rectangle(0, 0, bitinput.Width, bitinput.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
               BitmapData bitoutputData = bitoutput.LockBits(new Rectangle(0, 0, bitoutput.Width, bitoutput.Height), ImageLockMode.ReadWrite, bitoutput.PixelFormat);
               unsafe
               {
                   IntPtr ptrinput = bitinputData.Scan0;
                    IntPtr ptroutput = bitoutputData.Scan0;
                    int bytes = bitinputData.Height * bitinputData.Stride;
                    byte[] grayValue = new byte[bytes];
                    byte[] tempValue = new byte[bytes];
                    System.Runtime.InteropServices.Marshal.Copy(ptrinput, grayValue, 0, bytes);
                    tempValue= middlefilter(grayValue, bitinput.Width, bitinput.Height, bitinputData.Stride);
                    grayValue.Clone();
                       
                    grayValue=Sobel(tempValue, bitinput.Width, bitinput.Height, bitinputData.Stride);


                    System.Runtime.InteropServices.Marshal.Copy(grayValue, 0,ptrinput, bytes);

               }

               bitinput.UnlockBits(bitinputData);
               bitoutput.UnlockBits(bitoutputData);
               pictureBox1.Image = bitinput;
            }
        }
        private unsafe byte[] Sobel(byte[] grayValue, int width, int height, int stride)                                                         //sobel算法
        {
            double gradX, gradY;
            int grad;
            double teer1, teer2, teer3, teer4, teer5, teer6, teer7, teer8, teer9;
            for (int i = 1; i < height -1; i++)
            {
                for (int j = 1; j < width - 1; j++)
                {
                    teer1 = Convert.ToDouble(grayValue[(i - 1) * stride + (j - 1)]);
                    teer2 = Convert.ToDouble(grayValue[(i - 1) * stride + (j)]);
                    teer3 = Convert.ToDouble(grayValue[(i-1 ) * stride + (j +1)]);
                    teer4 = Convert.ToDouble(grayValue[(i ) * stride + (j -1)]);
                    teer5 = Convert.ToDouble(grayValue[(i ) * stride + (j)]);
                    teer6 = Convert.ToDouble(grayValue[(i ) * stride + (j + 1)]);
                    teer7 = Convert.ToDouble(grayValue[(i +1) * stride + (j - 1)]);
                    teer8 = Convert.ToDouble(grayValue[(i+1) * stride + (j)]);
                    teer9 = Convert.ToDouble(grayValue[(i +1) * stride + (j +1)]);
                    gradX = (-teer1 + teer3 - 2 * teer4 + 2 * teer6 - teer7 + teer9);
                    gradY = (teer1 + 2 * teer2 + teer3 - teer7 - 2 * teer8 - teer9);
            
                    grad = (int)System.Math.Sqrt((gradX * gradX) +(gradY * gradY));
                   // if (grad > 255)
                        grayValue[i * stride + j] =(byte)grad;
                  //if(grad<0)
                  //    grayValue[i * stride + j] = 0;
                }
            }
            return grayValue;
        }
        private unsafe byte[]  middlefilter(byte[] grayValue, int width, int height,int stride)                       //中值滤波
        {

           
            for (int i = 1; i < height-1; i++)
            {
                for (int j = 1; j < width-1; j++)
                {
                    byte[] Sort = new byte[]{ grayValue[(i-1)*stride+(j-1)],
                                             grayValue[(i-1)*stride+j],
                                             grayValue[(i-1)*stride+(j+1)],
                                             grayValue[i*stride+(j-1)],
                                             grayValue[i*stride+j],
                                             grayValue[i*stride+(j+1)],
                                             grayValue[(i+1)*stride+(j-1)],
                                             grayValue[(i+1)*stride+j],
                                             grayValue[(i+1)*stride+(j+1)]};
                    Sort1(Sort);
                    grayValue[i * stride + j] = Sort[4];
                    Sort.Clone();
                }
          
              

            }
            return grayValue;
          

        }
        private void Sort1(byte[] Sort)
        {
            
            int min;
           
            for (int i = 0; i < Sort.Length; i++)
            {
                min = i;
                for (int j = i + 1; j < Sort.Length; j++)
                {
                    if (Sort[j] < Sort[min])
                        min = j;
                }
                byte t = Sort[min];
                Sort[min] = Sort[i];
                Sort[i] = t;
            }
        

        }

        class clsColorPalette
        {
            public static ColorPalette pal;
        }
        public static unsafe Bitmap RGB(Bitmap bmInput)//rgb转换成gray
        {
            try
            {
                if (bmInput == null) return null;
                if (bmInput.PixelFormat == PixelFormat.Format8bppIndexed) return bmInput;

                Bitmap bmOutput = new Bitmap(bmInput.Width, bmInput.Height, PixelFormat.Format8bppIndexed);

                if (clsColorPalette.pal == null)    //获得系统调色板
                {
                    Bitmap monoBitmap = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                    clsColorPalette.pal = monoBitmap.Palette;

                    for (int i = 0; i < 256; i++)
                        clsColorPalette.pal.Entries[i] = Color.FromArgb(i, i, i);
                }

                bmOutput.Palette = clsColorPalette.pal;

                BitmapData bmInputData = bmInput.LockBits(new Rectangle(0, 0, bmInput.Width, bmInput.Height), ImageLockMode.ReadWrite, bmInput.PixelFormat);
                BitmapData bmOutputData = bmOutput.LockBits(new Rectangle(0, 0, bmOutput.Width, bmOutput.Height), ImageLockMode.ReadWrite, bmOutput.PixelFormat);
                Byte* ptrInput = (Byte*)bmInputData.Scan0;
                Byte* ptrOutput = (Byte*)bmOutputData.Scan0;

                int PixelFormatFactor = 1;
                if (bmInput.PixelFormat == PixelFormat.Format24bppRgb) PixelFormatFactor = 3;
                if (bmInput.PixelFormat == PixelFormat.Format32bppRgb) PixelFormatFactor = 4;
                if (bmInput.PixelFormat == PixelFormat.Format32bppArgb) PixelFormatFactor = 4;

                int length = bmInput.Width * PixelFormatFactor;
                int nOffset = bmInputData.Stride - bmInput.Width * PixelFormatFactor;
                byte red, green, blue;
                byte* p = ptrInput;
                for (int col = 0; col < bmInput.Height; col++)
                {
                    for (int row = 0; row < bmInput.Width; row++)
                    {
                        blue = p[0];
                        green = p[1];
                        red = p[2];

                        byte b = (byte)(0.299 * red + 0.587 * green + 0.114 * blue);
                        *ptrOutput = b;

                        p += PixelFormatFactor;
                        ptrOutput++;
                    }
                    p += nOffset;
                    ptrOutput += bmOutputData.Stride - bmOutput.Width;
                }
                bmInput.UnlockBits(bmInputData);
                bmOutput.UnlockBits(bmOutputData);
                return bmOutput;
            }
            catch (Exception ex)
            {
                return null;
            }
        } --------------------编程问答--------------------
引用 7 楼 lvhengli222 的回复:
我使用的是经过RGB转换成灰度的图像,那现在的图像应该是8位位图了,那我使用还需要*3吗

你现在的需求我还是不怎么清楚,你这个是复制图像还是什么? --------------------编程问答-------------------- 我下边这个程序是进行sobel边缘提取,最上面的图像就是复制图像。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,