C# 在directshow上创建了一个UYVY图像,它已损坏,我不知道为什么

C# 在directshow上创建了一个UYVY图像,它已损坏,我不知道为什么,c#,directshow,directshow.net,C#,Directshow,Directshow.net,我有一个摄像头的UYVY数据缓冲区,我正在使用GSSF Directshow.net过滤器将缓冲区推送到图形中 目前的图表是 GSSF -> YUV Transform -> AVI Splitter -> Video Renderer 图表正确地计算了颜色并正确地显示了它,但是图像中有一些条不应该在那里,我不确定它们来自哪里。盯着这张照片看会伤我的眼睛 此函数获取UYVY缓冲区(mainbyte)并将其复制到整数数组中 unsafe public void

我有一个摄像头的UYVY数据缓冲区,我正在使用GSSF Directshow.net过滤器将缓冲区推送到图形中

目前的图表是

GSSF -> YUV Transform -> AVI Splitter -> Video Renderer
图表正确地计算了颜色并正确地显示了它,但是图像中有一些条不应该在那里,我不确定它们来自哪里。盯着这张照片看会伤我的眼睛

此函数获取UYVY缓冲区(mainbyte)并将其复制到整数数组中

        unsafe public void ImageFromPixels__()
    {
        byte[] x = mainbyte;
        long fff = 720 * 1280;
        mainptr = new IntPtr(fff);
        for (int p = 0; p < 720 * 1280; p++)
        {
            U = (x[p * 4 + 0]);

            Y = (x[p * 4 + 1]);
            V = (x[p * 4 + 2]);
            Y2 = (x[p * 4 + 3]);

           // int one = V << 16 | Y << 8 | U;
           // int two = V << 16 | Y2 << 8 | U;
            int one = Y2 << 24 | V << 16 | Y << 8 | U;
           // mainint[p * 2 + 0] = one;
           // mainint[p * 2 + 1] = two;
            mainint[p] = one;

        }

        m_FPS = UNIT / 20;
        m_b = 211;
        m_g = 197;
    }
更新 稍微改变一下

        override unsafe public int GetImage(int iFrameNumber, IntPtr ip, int iSize, out int iRead)
    {
        int hr = 0;

        if (iFrameNumber>-1)
        {
            if (iFrameNumber < MAXFRAMES)
            {
                ImageFromPixels_(20, mainbyte);

                m_g += 3;
                m_b += 7;
                int* bp = (int*)ip.ToPointer();
                Random k = new Random();
                StreamReader s = new StreamReader("jpegFile.txt");
                for (int f = 0; f < 720; f++)
                {
                    for (int x = 0; x < (1280); x += 1)
                    {
                        *(bp + (f * 1280) + x) = mainint[f * 1280 + x];
                    }
                }
            }
            else
            {
                hr = 1; // End of stream
            }
        }
//

不安全的公共void ImageFromPixels_u2;(长FPS,字节[]x)
{
长fff=720*1280*3;
mainptr=新的IntPtr(fff);
对于(int p=0;p<720*640;p++)
{
U=(x[p*4+0]);
Y=(x[p*4+1]);
V=(x[p*4+2]);
Y2=(x[p*4+3]);

int one=Y2你的循环是如何达到(720*1280)的,这是图片中每个像素的一次迭代,但在循环中你在做Y和Y2。它看起来不正确。

指针数学很糟糕。更改了我的代码

for (int f = 0; f < 720; f++)
{
    for (int x = 0; x < (1280); x += 1)
    {
        *(bp + (f * 1280) + x) = mainbyte[f * 1280 + x];
    }
}
将BPP从32更改为16,现在可以顺利工作


这是真的。我来看看你在ImageFromPixels(也更新了)中所做的事情对我来说没有意义。你正在逐字节读取4字节的宏像素,然后你以基本相同的顺序将它们合并在一起并放回…所以基本上你只是复制数据而不更改它,以一种奇怪的方式。我从一个字节获取信息[]数组并将其粘贴到int[]数组中
        override unsafe public int GetImage(int iFrameNumber, IntPtr ip, int iSize, out int iRead)
    {
        int hr = 0;

        if (iFrameNumber>-1)
        {
            if (iFrameNumber < MAXFRAMES)
            {
                ImageFromPixels_(20, mainbyte);

                m_g += 3;
                m_b += 7;
                int* bp = (int*)ip.ToPointer();
                Random k = new Random();
                StreamReader s = new StreamReader("jpegFile.txt");
                for (int f = 0; f < 720; f++)
                {
                    for (int x = 0; x < (1280); x += 1)
                    {
                        *(bp + (f * 1280) + x) = mainint[f * 1280 + x];
                    }
                }
            }
            else
            {
                hr = 1; // End of stream
            }
        }
        // Build a BitmapInfo struct using the parms from the file
        bmi.Size = Marshal.SizeOf(typeof(BitmapInfoHeader));
        bmi.Width = WIDTH;
        bmi.Height = HEIGHT * -1;
        bmi.Planes = 1;
        bmi.BitCount = BPP;
        bmi.Compression = 0x59565955;
        bmi.ImageSize = (bmi.BitCount / 8) * bmi.Width * bmi.Height;
        bmi.XPelsPerMeter = 0;
        bmi.YPelsPerMeter = 0;
        bmi.ClrUsed = 0;
        bmi.ClrImportant = 0;

        int hr = psc.SetMediaTypeFromBitmap(bmi, m_FPS);
        DsError.ThrowExceptionForHR(hr);
    }
        unsafe public void ImageFromPixels_(long FPS, byte[] x)
    {

        long fff = 720 * 1280 * 3;
        mainptr = new IntPtr(fff);
        for (int p = 0; p < 720 * 640; p++)
        {
            U = (x[ p * 4 + 0]);

            Y = (x[p * 4 + 1]);
            V = (x[p * 4 + 2]);
            Y2 = (x[p * 4 + 3]);

            int one = Y2 << 24 | V << 16 | Y << 8 | U;
            //int one = V << 16 | Y << 8 | U;
            //int two = V << 16 | Y2 << 8 | U;
            //mainint[p * 2 + 0] = one;
            //mainint[p * 2 + 1] = two;
            mainint[p] = one;

        }

        m_FPS = UNIT / FPS;
        m_b = 211;
        m_g = 197;
    }
for (int f = 0; f < 720; f++)
{
    for (int x = 0; x < (1280); x += 1)
    {
        *(bp + (f * 1280) + x) = mainbyte[f * 1280 + x];
    }
}
Marshal.Copy(mainbyte, 0, ip, 1280*720);