Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Tree应在一个大的过滤器中过滤';似乎工作不正常_C#_Aforge_Threshold - Fatal编程技术网

C# Tree应在一个大的过滤器中过滤';似乎工作不正常

C# Tree应在一个大的过滤器中过滤';似乎工作不正常,c#,aforge,threshold,C#,Aforge,Threshold,希望你们一切顺利。我确实使用一个RGE库用C#写了一些代码。我想裁剪从网络摄像头拍摄的主图像,以获得良好的ROI。当我使用阈值0时,所有内容都应该是白色像素(总像素为26880像素),但在裁剪后的图像中似乎有一些黑色像素(578像素)。知道是什么引起的吗?当我不裁剪我的图像时,一切都很好 Bitmap img = (Bitmap)eventArgs.Frame.Clone(); Bitmap bmp = new Bitmap(x2box, y2b

希望你们一切顺利。我确实使用一个RGE库用C#写了一些代码。我想裁剪从网络摄像头拍摄的主图像,以获得良好的ROI。当我使用阈值0时,所有内容都应该是白色像素(总像素为26880像素),但在裁剪后的图像中似乎有一些黑色像素(578像素)。知道是什么引起的吗?当我不裁剪我的图像时,一切都很好

            Bitmap img = (Bitmap)eventArgs.Frame.Clone(); 
            Bitmap bmp = new Bitmap(x2box, y2box); 
            bmp = img.Clone(new Rectangle(x1box, y1box, x2box, y2box), eventArgs.Frame.PixelFormat); 
            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721); 
            Bitmap img1 = filter.Apply(bmp);
            Threshold tresh = new Threshold((int)tresh1);      // tresh1 is 0-255 but is set to zero here
            tresh.ApplyInPlace(img1);   
            int iterator = 1; int xrow = 0;      // here i use these constant to calculate location of the pixels
            byte[] arraybyte = BitmapToByteArray(img1);      
            for (int i = 0; i < arraybyte.Length; i++)
            {
                if (i - iterator * img1.Width == 0)
                {
                    xrow++;
                    iterator++;
                }
                if (arraybyte[i] == 0) // if pixel is black
                {
                    X_val.Add(i - xrow * img1.Width);
                    Y_val.Add(iterator);
                }
            }

            for (int i = 0; i < X_val.Count; i++)
            {
                YAve += Y_val[i];
                XAve += X_val[i];
            }
            MessageBox.Show(X_val.Count.ToString()); // shows non-zero value!

位图每行的字节数将强制为4的倍数。如果
roi width*每像素字节数
不是4的倍数,则每行末尾都有填充字节


它们不会被设置阈值,因为它们实际上不是位图的一部分,所以它们的值可能为0。您的BitmapToByteArray方法可能不知道填充并读取每个字节。

谢谢,我认为这是问题的根源,因为剪切前图像的宽度是4的倍数。一个选项是只让用户选择4的倍数,这可能不是最好的选项。有什么建议可以让我知道我使用的填充方法吗?我将更新上述代码以包含该方法。我想投票支持你的答案,但我仍然需要更多的声誉:P@maziarderakhshandeh:您可以在每行上循环,只复制
width*byte/pixel
字节,而不是
stride
。您可能必须使用不安全的代码。另一种方法是改变你的阅读循环。在行和行内循环,只考虑到“代码>宽度*字节/像素< /代码>字节。
 public static byte[] BitmapToByteArray(Bitmap bitmap)
    {

        BitmapData bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
        int numbytes = bmpdata.Stride * bitmap.Height;
        byte[] bytedata = new byte[numbytes];
        IntPtr ptr = bmpdata.Scan0;
        Marshal.Copy(ptr, bytedata, 0, numbytes);
        bitmap.UnlockBits(bmpdata);
        return bytedata;

    }