簡體   English   中英

Aforge中的treshould過濾器似乎無法正常工作

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

希望大家一切都好。 我確實使用Aforge庫在C#中編寫了一些代碼。 我想裁剪從網絡攝像頭捕獲的主圖像,以便獲得不錯的投資回報率。 當我使用閾值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!

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;

    }

位圖每一行的字節數將強制為4的倍數。如果roi width * bytes per pixel不是4的倍數,則每行末尾將有填充字節。

它們將不會受到閾值限制,因為它們實際上不是位圖的一部分,因此它們的值可能為0。您的BitmapToByteArray方法可能不知道填充並讀取每個字節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM