汉明距离的计算代码

#include "stdafx.h"
#include <string.h>

struct HammingLUT
{
	typedef unsigned char ElementType;
	typedef int ResultType;
	ResultType operator()(const unsigned char* a, const unsigned char* b, int size) const
	{
		static const int popCountTable[] =
		{
			0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
			1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
			1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
			2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
			1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
			2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
			2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
			3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
		};
		ResultType result = 0;
		for (int i = 0; i < size; i++) {
			result += popCountTable[a[i] ^ b[i]];
		}
		return result;
	}
};

#include "math.h"

int main()
{

	HammingLUT lut;

	unsigned char a[] = { "10101010" };
	unsigned char b[] = { "11111111" };
	unsigned char hedistance = lut((a), (b), 8);

	float wgt = exp(-float(hedistance)*float(hedistance) / (26 * 26)); //相似度

    return 0;
}

下面是一个使用PCL库计算二进制描述子相似度的示例代码。该代码使用ORB算法提取图像关键点,并计算二进制描述子。然后使用PCL库中的`pcl::common::getL1HammingDistance`函数计算描述子之间的汉明距离,并将距离转换为相似度。最后,根据相似度进行匹配点对的筛选和去除错误匹配。 ```c++ #include <iostream> #include <pcl/features/normal_3d.h> #include <pcl/features/shot_omp.h> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/visualization/pcl_visualizer.h> #include <pcl/common/transforms.h> #include <pcl/common/common.h> #include <pcl/common/histogram.h> using namespace std; using namespace pcl; int main(int argc, char **argv) { // 读取两张图像 Mat image1 = imread("image1.png", IMREAD_GRAYSCALE); Mat image2 = imread("image2.png", IMREAD_GRAYSCALE); // 创建ORB对象并提取关键点和二进制描述子 ORB orb; vector<KeyPoint> keypoints1, keypoints2; Mat descriptors1, descriptors2; orb(image1, noArray(), keypoints1, descriptors1); orb(image2, noArray(), keypoints2, descriptors2); // 将描述子转换为PCL库中的Histogram类型 pcl::Histogram<256> hist1, hist2; for (int i = 0; i < descriptors1.rows; i++) { hist1.histogram[descriptors1.at<uchar>(i)]++; } for (int i = 0; i < descriptors2.rows; i++) { hist2.histogram[descriptors2.at<uchar>(i)]++; } // 计算两个描述子之间的汉明距离 int hamming_distance = pcl::common::getL1HammingDistance(hist1, hist2); // 将汉明距离转换为相似度 double similarity = exp(-hamming_distance / 10.0); // 根据相似度进行匹配点对的筛选和去除错误匹配 // ... return 0; } ``` 需要注意的是,在实际应用中,可能需要对描述子之间的距离和相似度进行归一化或加权,以便更好地反映它们之间的关系。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值