c# - 位图到二维数组映射错误
问题描述
所以我有一个与相机的连接,但我在代码中的多个位置提取数据。数据是指当前帧。我的计划是在(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;
}
谁能帮忙?
解决方案
这是很多代码,没有一个可行的例子,我只能提供一个可能有帮助的猜测。如果你能展示你是如何调用这些方法的,而不是冗长的解释提供一个工作示例,那就更好了。但除此之外:
我认为您可能会在 GetSnapshot 方法中对 global_x 和 global_y 变量进行初始分配。你提到你的坐标最终是负面的,我认为你可能需要重新考虑分配
global_x = (int)(videoSourcePlayer.imgWidth - videoSourcePlayer.imagex);
我不确定你在 global_x 中真正想要什么,但后来你将它用作循环限制,并且通过这个分配,我看不出它会如何变得积极。
推荐阅读
- winforms - DropDownList 的默认文本
- mongodb - mongodb中的内部数组总数
- c++ - 使用表达式模板在矩阵中重载运算符
- java - javax.naming.NameNotFoundException:在 WebSphere Liberty 上
- java - 打开蓝牙设置时出现错误
- multithreading - 如何运行 Vertx API 异步?
- sql-server - SQL Server - 如何删除总和为零的行对
- python - 如何关闭 LED 灯条
- node.js - 为什么socket.io在同一台电脑上使用两个不同的浏览器不起作用(我无法连接或页面仍然永远等待)
- sql - 如何使用数据列表更新数据库表