简体   繁体   English

Aforge中的treshould过滤器似乎无法正常工作

[英]treshould filter in Aforge doesn't seem to work properly

hope you all doing well. 希望大家一切都好。 I did write a bit of codes in C# using Aforge library. 我确实使用Aforge库在C#中编写了一些代码。 I wanted to crop my main image captured from webcam so as to have a nice ROI. 我想裁剪从网络摄像头捕获的主图像,以便获得不错的投资回报率。 When I use threshold value of 0 everything should be in white pixels (total of lets say 26880 pixels) but it seems that I have some black pixels (578 pixels) within my cropped image. 当我使用阈值0时,所有内容都应以白色像素(总共可以说26880像素)为单位,但似乎在裁剪后的图像中有一些黑色像素(578像素)。 any idea of what may caused it? 知道是什么原因引起的吗? when I don't crop my image everything is fine. 当我不修剪图像时,一切都很好。

            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!

the BitmapToByteArray method is as follow: BitmapToByteArray方法如下:

 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;

    }

The number of bytes for each row of the Bitmap will be enforced to be a multiple of 4. If roi width * bytes per pixel is not a multiple of 4, you will have padding bytes at the end of each row. 位图每一行的字节数将强制为4的倍数。如果roi width * bytes per pixel不是4的倍数,则每行末尾将有填充字节。

They will not be thresholded as they aren't really part of the Bitmap, so their value may be 0. Your BitmapToByteArray method might not be padding-aware and read every byte. 它们将不会受到阈值限制,因为它们实际上不是位图的一部分,因此它们的值可能为0。您的BitmapToByteArray方法可能不知道填充并读取每个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM