unity图片相似度识别

public static SimilarPhoto _Instance;
/ <summary>
/ 需要被对比的哈希值
/ </summary>
//public string ContrastValue;
/ <summary>
/ 服务器图片
/ </summary>
//public List<Texture2D> ServerImage = new List<Texture2D>();
public List<string> SimilarPhotoHash = new List<string>();
//public Button btn;
void Awake()
{
_Instance = this;
//DCT变换
//float[,] A = creatDCTMatrix(size);
//float[,] Aa = Transpose(A);
//float[,] DCT = Multiply(Multiply(A, ima1F), Aa);
//texS = (Texture2D)Resources.Load("aruco_sample1");
//for (int i = 0; i < ServerImage.Count; i++)
//{
// StartUpLoadImageData(ServerImage[i], 10);
//}

//for (int i = 0; i < NewHash.Count; i++)
//{
// CountSimilarity(ContrastValue, NewHash[i]);
//}
}

/// <summary>
/// 开始对比并计算哈希值
/// </summary>
/// <param name="tex"></param>
/// <param name="size"></param>
public void StartUpLoadImageData(Texture2D tex, int size)
{
image2F(Tex2Gray(ReduceSize(tex, size)));
}
/// <summary>
/// 压缩图片
/// </summary>
/// <param name="tex"></param>
/// <param name="size"></param>
/// <returns></returns>
Texture2D ReduceSize(Texture2D tex, int size)
{
if (tex == null || size <= 0)
{
Debug.Log("图片错误");
return null;
}
Texture2D newTexture = new Texture2D(size, size, TextureFormat.RGB24, false);
float ratioX = tex.width / size;
float ratioY = tex.height / size;
Color color;
for (int i = 0; i < newTexture.height; i++)
{
for (int j = 0; j < newTexture.width; j++)
{
color = tex.GetPixel(Mathf.RoundToInt(j * ratioX), Mathf.RoundToInt(i * ratioY));
newTexture.SetPixel(j, i, color);
}
}
return newTexture;

}
/// <summary>
/// 转灰度
/// </summary>
/// <param name="tex"></param>
/// <returns></returns>
Texture2D Tex2Gray(Texture2D tex)
{
Color color;
for (int i = 0; i < tex.height; i++)
{
for (int j = 0; j < tex.width; j++)
{
color = tex.GetPixel(j, i);
float gray = (color.r * 30 + color.b * 59 + color.b * 11) / 100;
tex.SetPixel(j, i, new Color(gray, gray, gray));
}
}
return tex;
}
/// <summary>
/// 图片转矩阵
/// </summary>
/// <param name="tex"></param>
/// <returns></returns>
float[,] image2F(Texture2D tex)
{
int size = tex.width;
float[,] f = new float[size, size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
f[i, j] = tex.GetPixel(i, j).r;
}
}
float[,] A = creatDCTMatrix(size);
float[,] Aa = Transpose(A);
float[,] DCT = Multiply(Multiply(A, f), Aa);
getHash(DCT, averageDCT(DCT));

return f;
}
/// <summary>
/// 计算DCT矩阵
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
float[,] creatDCTMatrix(int size)
{
//Debug.Log((float)Mathf.Cos(Mathf.PI));
float[,] ret = new float[size, size];
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
float angle = ((y + 0.5f) * Mathf.PI * x / size);
ret[x, y] = cfunc(x, size) * (float)Mathf.Cos(angle);
}
}
return ret;
}
float cfunc(int n, int size)
{
if (n == 0)
{
return Mathf.Sqrt(1f / size);
}
else
{
return Mathf.Sqrt(2f / size);
}

}
/// <summary>
/// 矩阵转置
/// </summary>
/// <param name="C"></param>
/// <returns></returns>
float[,] Transpose(float[,] C)
{
int size = C.GetLength(0);
float[,] ret = new float[size, size];
for (var x = 0; x < size; x++)
{
for (var y = 0; y < size; y++)
{
ret[y, x] = C[x, y];
}
}
return ret;
}
/// <summary>
/// 矩阵相乘
/// </summary>
/// <param name="C1"></param>
/// <param name="C2"></param>
/// <returns></returns>
float[,] Multiply(float[,] C1, float[,] C2)
{
int size = C1.GetLength(0);
float[,] ret = new float[size, size];
for (var y = 0; y < size; y++)
{
for (var x = 0; x < size; x++)
{
float sum1 = 0;
for (int k = 0; k < size; k++)
{
sum1 += C1[x, k] * C2[k, y];
}
ret[x, y] = sum1;
}
}
return ret;
}
/// <summary>
/// DCT均值
/// </summary>
/// <param name="dct"></param>
/// <returns></returns>
float averageDCT(float[,] dct)
{
int size = dct.GetLength(0);
float aver = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
aver += dct[i, j];
}
}
return aver / (size * size);
}
/// <summary>
/// 获取当前图片感知哈希值
/// </summary>
string getHash(float[,] dct, float aver)
{
string hash = string.Empty;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
hash += (dct[i, j] >= aver ? "1" : "0");
}
}
// Debug.Log("哈希值添加中");
SimilarPhotoHash.Add(hash);
return hash;
}

/// <summary>
/// 计算两图片哈希值的汉明距离
/// </summary>
/// <param name="hash1"></param>
/// <param name="hash2"></param>
/// <returns></returns>
public float computeDistance(string hash1, string hash2)
{
float dis = 0;
for (int i = 0; i < hash1.Length; i++)
{
if (hash1[i] == hash2[i])
{
dis++;
}
}
// Debug.Log(dis / hash1.Length);
return dis / hash1.Length;
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图片识别Unity中有两种方式。第一种方式是把图片上传到数据库中,然后进行下载图片包,导入到Unity。第二种方式是直接在Unity中将需要识别图片拖到对应位置。 在进行图片识别之前,我们需要了解Vuforia的识别机制,这样可以帮助我们选择适合做为识别图片图片。Vuforia是通过检测自然特征点进行匹配的,也就是说只有实时检测的图像的特征点与数据库中的模板图片或选择的贴图图片的特征点一致时才能成功识别。 如果想基于开源算法进行人脸识别Unity的版本要求在2020以上。可以基于单张的图片进行人脸坐标识别,也可以通过接入摄像头进行实时的人脸识别。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Unity-Vuforia篇-图片识别](https://blog.csdn.net/qq_30868065/article/details/121899119)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Unity人脸识别SDK](https://download.csdn.net/download/weixin_41978284/85141008)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值