如何扭转RectangleF到别人脸哈希
-
18-09-2019 - |
题
这里是为了什么别的商店,作为一散列。它们存储这样的:
faces=rect64(54391dc9b6a76c2b),4cd643f64b715489
[DSC_2289.jpg]
faces=rect64(1680000a5c26c82),76bc8d8d518750bc
信息网这样说:
数包裹在rect64()为64位进制数字。
- 打破这四个16位数字。
- 通过将每个最大unsigned16位的数量(65535)和你就会有四个数字在0和1之间。
- 四个数字剩下的给你的相对坐标面临的矩形:(左,上,右,底部)。
- 如果你想要结束了绝对坐标、多左右的图像的宽度和顶部和底部通过图像的高度。
所以我的代码变成RectangleF正常工作(只保持相对坐标):
public static RectangleF GetRectangle(string hashstr)
{
UInt64 hash = UInt64.Parse(hashstr, System.Globalization.NumberStyles.HexNumber);
byte[] bytes = BitConverter.GetBytes(hash);
UInt16 l16 = BitConverter.ToUInt16(bytes, 6);
UInt16 t16 = BitConverter.ToUInt16(bytes, 4);
UInt16 r16 = BitConverter.ToUInt16(bytes, 2);
UInt16 b16 = BitConverter.ToUInt16(bytes, 0);
float left = l16 / 65535.0F;
float top = t16 / 65535.0F;
float right = r16 / 65535.0F;
float bottom = b16 / 65535.0F;
return new RectangleF(left, top, right - left, bottom - top);
}
现在我有一个RectangleF我想把它回到哈希上面提到的。我可不想出解决办法。它看起来像中使用2个字节,包括精密,但是一个漂浮在C#是个8字,甚至BitConverter.ToSingle是4个字节。
任何帮助的赞赏。
编辑:这里是什么我现在
public static string HashFromRectangle(RectangleCoordinates rect)
{
Console.WriteLine("{0} {1} {2} {3}", rect.Left, rect.Top, rect.Right, rect.Bottom);
UInt16 left = Convert.ToUInt16((float)rect.Left * 65535.0F);
UInt16 top = Convert.ToUInt16((float)rect.Top * 65535.0F);
UInt16 right = Convert.ToUInt16((float)rect.Right * 65535.0F);
UInt16 bottom = Convert.ToUInt16((float)rect.Bottom * 65535.0F);
byte[] lb = BitConverter.GetBytes(left);
byte[] tb = BitConverter.GetBytes(top);
byte[] rb = BitConverter.GetBytes(right);
byte[] bb = BitConverter.GetBytes(bottom);
byte[] barray = new byte[8];
barray[0] = lb[0];
barray[1] = lb[1];
barray[2] = tb[0];
barray[3] = tb[1];
barray[4] = rb[0];
barray[5] = rb[1];
barray[6] = bb[0];
barray[7] = bb[1];
return BitConverter.ToString(barray).Replace("-", "").ToLower();
}
解决方案
你目前的代码的更换字节的每一个协调。这是因为BitConverter给你的字节在little endian顺序(即第一个字节数是最不重要的字节)。交换在你任务如下让码和重新编码给回原来的散列。
barray[0] = lb[1];
barray[1] = lb[0];
barray[2] = tb[1];
barray[3] = tb[0];
barray[4] = rb[1];
barray[5] = rb[0];
barray[6] = bb[1];
barray[7] = bb[0];
这就是说,我认为这是更清楚地执行转换,使用简单的乘以及增加。你可以做一个类似的事情,与解散串如果你阅读它作为一个单一的 ulong
减/鸿沟。例如用于编码:
public static ushort ToUShort(double coordinate)
{
double ratio = Math.Max(0, Math.Min(Math.Round(coordinate * 65535), 65535));
return (ushort)ratio;
}
public static string HashFromRectangle(Rect rect)
{
ulong left = ToUShort(rect.Left);
ulong top = ToUShort(rect.Top);
ulong right = ToUShort(rect.Right);
ulong bottom = ToUShort(rect.Bottom);
ulong hash = (((left * 65536) + top) * 65536 + right) * 65536 + bottom;
return hash.ToString("x");
}
其他提示
看起来你需要拿出浮动类型,从HashFromRectangle(rect),像这样:
UInt16 left = (UInt16)( rect.Left * 65535.0F);
UInt16 top =(UInt16) (rect.Top * 65535.0F);
UInt16 right = (UInt16) (rect.Right * 65535.0F);
UInt16 bottom = (UInt16) (rect.Bottom * 65535.0F);
此外,它可能是更具可读性,使用这个用于填充阵列:
Array.Copy(lb, 0, barray, 0, 2);
Array.Copy(tb, 0, barray, 2, 2);
Array.Copy(rb, 0, barray, 4, 2);
Array.Copy(bb, 0, barray, 6, 2);
如果让我知道这工作!
Aaron
不隶属于 StackOverflow