位图到二维数组的映射错误

时间:2019-07-24 16:24:31

标签: c# bitmap mapping

所以我与一台相机有一个连接,但是我在代码中的多个位置提取数据。数据是指当前帧。 我的计划是在(Aforge)videoSourcePlayer上显示8比例版本,以便用户可以放大和缩小并在所需区域周围绘制一个框,然后将其映射到12字节数据的二维数组。 我在映射时遇到问题,以某种方式在videoSourcePlayer上绘制框时,二维数组中对应的像素位置不相同,我不确定为什么。 此外,当我进行放大时,x和y值变为负数,并且我不知道如何将其映射回二维数组。 现在,此代码适用于我将所有内容作为位图进行操作

 private Bitmap Andrew()
    {
        Bitmap tempBitmap = new Bitmap(videoSourcePlayer.Width, videoSourcePlayer.Height);
        Graphics.FromImage(tempBitmap);
        Graphics g = Graphics.FromImage(tempBitmap);
        Bitmap originalBmp = videoSourcePlayer.GetCurrentVideoFrame();
      while (originalBmp == null)
        {
            originalBmp = videoSourcePlayer.GetCurrentVideoFrame();
        }
        g.DrawImage(originalBmp, videoSourcePlayer.imagex, videoSourcePlayer.imagey, videoSourcePlayer.imgWidth, videoSourcePlayer.imgHeight);

        return tempBitmap;
    }

但是当我尝试执行二维数组时,我的位置关闭了。

同样,在放大和缩小时,我也不知道如何处理负数。

这是我的代码:

    public double[,] GetSnapshot()
    {
        global_x = 0;
        global_y = 0;
        int rawImageSize = DetermineRawImageSize();
        byte[] temp = new byte[rawImageSize];

        float[] parameters = new float[1];
        parameters[0] = 1.5F;
        transfer.bits = new byte[getBufferSize()];
        FrameDescriptor frameDesc = new FrameDescriptor();
        //gets frame from camera
        ReturnCode rc = Api.GetNextFrame(m_hCamera, transfer.bits.Length, transfer.bits, ref frameDesc);
        int i4 = 0;
        int value2 = 0;
        global_y = (int)(videoSourcePlayer.imgHeight - videoSourcePlayer.imagey);
        global_x = (int)(videoSourcePlayer.imgWidth - videoSourcePlayer.imagex);
        double[,] returndata = new double[global_x, global_y];

        for (int x = (int)videoSourcePlayer.imagex; x < global_x; x++)
        {
          for (int y = (int)videoSourcePlayer.imagey; y < global_y; y++ )
            {
                int value = ((transfer.bits[i4] << 8) + transfer.bits[i4 + 1]);
                value2 = (value >> 4);
                returndata[x,y]= value2;
                i4 = i4 + 2;
            }
        }
        return returndata;
    }

但是当我尝试将其映射到视频播放器时,用户在视频播放器上绘制了一个蓝色框并获取该框的像素位置,则映射不匹配:

    double zoomingetback(int i2)
    {
        double answer = 0D;
        double l = 0D;
        double tempD = 0D;
        double[,] data = GetSnapshot();

            for (int x = stIRList[i2].X; x <= endIRList[i2].X; x++)
            {
                for (int y = stIRList[i2].Y; y <= endIRList[i2].Y; y++)
                {
                tempD = data[x, y];
                answer = answer + tempD;
                    l++;
                }
            }


        answer = answer / l;
        return answer;

    }

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这是很多代码,没有一个可行的示例,我只能提供一个可能有用的猜测。如果您可以说明如何调用这些方法,则最好不做冗长的解释,而是提供一个有效的示例。除此之外:

我认为您可能没有在GetSnapshot方法中对global_x和global_y变量进行初始分配。您提到您的座标最终被否定了,我认为您可能需要重新考虑

的分配
global_x = (int)(videoSourcePlayer.imgWidth - videoSourcePlayer.imagex);

我不确定您在global_x中真正想要什么,但是稍后您将其用作循环限制,并且在进行此分配后,我看不出它怎么会变成正数。