海康体积相机输出长宽数据对调的纠正方法
说明:适用于全系列体积相机通过SDK获取长宽高数据出现长宽数据与实际包裹长宽对调的情况。
为保证文章的连贯性,直接讲述结论以及纠正方法。
-
涉及的特征
- 获取到的长宽高数据,总是 “长度数值>宽度数值”;
- 获取到的包裹四个顶点坐标顺序:X坐标值最小的为坐标点为[0],按顺时针方向依次[0][1][2][3];(参考图如下,得出结论过程在文章末尾 附2)
- 线段[0][1]和[0][3]的长度即可表示矩形的两条边长,即长宽。
-
纠正方法实现
基本逻辑:
/*判断包裹长宽是否对调的过程
* 1.找出长边的端点是[0][1]还是[0][3]
* 2.判断长边与X轴的夹角,(+45°至-45°代表需要对调输出的长宽数据)*/
//OK..方法有了,下面具体实现
关键代码实现:
//求【0】【1】和【0】【3】的距离
float dx = stResultInfo.stVolumeInfo.vertex_pnts[1].fX - stResultInfo.stVolumeInfo.vertex_pnts[0].fX;
float dy = stResultInfo.stVolumeInfo.vertex_pnts[1].fY - stResultInfo.stVolumeInfo.vertex_pnts[0].fY;
double length01 = Math.Sqrt(Math.Abs(dx)* Math.Abs(dx) + Math.Abs(dy)* Math.Abs(dy));
dx = stResultInfo.stVolumeInfo.vertex_pnts[3].fX - stResultInfo.stVolumeInfo.vertex_pnts[0].fX;
dy = stResultInfo.stVolumeInfo.vertex_pnts[3].fY - stResultInfo.stVolumeInfo.vertex_pnts[0].fY;
double length03 = Math.Sqrt(Math.Abs(dx) * Math.Abs(dx) + Math.Abs(dy) * Math.Abs(dy));
listBox1.Items.Add("长度([0][1]):" + length01);
listBox1.Items.Add("长度([0][3]):" + length03);
//判断长边
if (length01 > length03)//【0】【1】是长边
{
//求长边与X轴夹角
var angle= Math.Atan2((stResultInfo.stVolumeInfo.vertex_pnts[1].fY - stResultInfo.stVolumeInfo.vertex_pnts[0].fY), (stResultInfo.stVolumeInfo.vertex_pnts[1].fX - stResultInfo.stVolumeInfo.vertex_pnts[0].fX)) * (180 / Math.PI);
listBox1.Items.Add("角度:" + angle);
if (-45 < angle && angle < 45)
{
listBox1.Items.Add("需要对调");
}
else
{
listBox1.Items.Add("不需要对调");
}
}
else//【0】【3】是长边
{
var angle = Math.Atan2((stResultInfo.stVolumeInfo.vertex_pnts[3].fY - stResultInfo.stVolumeInfo.vertex_pnts[0].fY), (stResultInfo.stVolumeInfo.vertex_pnts[3].fX - stResultInfo.stVolumeInfo.vertex_pnts[0].fX)) * (180 / Math.PI);
listBox1.Items.Add("角度:" + angle);
if (-45 < angle && angle < 45)
{
listBox1.Items.Add("需要对调");
}
else
{
listBox1.Items.Add("不需要对调");
}
}
(为了文章连贯,效果图见文章尾部附1。)
为了便于使用,对该方法进行封装,在原有基础上只需要引用GetRealWH.dll,调用如下方法,传入获取到的长度、宽度、高度数据以及位置坐标信息即可自动判断并纠正为真实数据。
相关文件:获取该文件
附1:纠正判断效果
附2:输出体积数据中包裹四个顶点坐标的顺序特征发现过程
体积输出坐标顺序:X值最小的为坐标[0],按顺时针方向依次[0][1][2][3];
相关资源:获取该文件