首页 > 解决方案 > 位图到二维数组映射错误

问题描述

所以我有一个与相机的连接,但我在代码中的多个位置提取数据。数据是指当前帧。我的计划是在(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;

    }

谁能帮忙?

标签: c#bitmapmapping

解决方案


这是很多代码,没有一个可行的例子,我只能提供一个可能有帮助的猜测。如果你能展示你是如何调用这些方法的,而不是冗长的解释提供一个工作示例,那就更好了。但除此之外:

我认为您可能会在 GetSnapshot 方法中对 global_x 和 global_y 变量进行初始分配。你提到你的坐标最终是负面的,我认为你可能需要重新考虑分配

global_x = (int)(videoSourcePlayer.imgWidth - videoSourcePlayer.imagex);

我不确定你在 global_x 中真正想要什么,但后来你将它用作循环限制,并且通过这个分配,我看不出它会如何变得积极。


推荐阅读