• 医学图像匹配,c++计算归一化互相关


    在这里插入图片描述

    头文件

    class myClass
    {
    public:
    	/// 
    	/// 计算归一化互相关
    	/// 
    	/// 图像1的像素值
    	/// 图像2的像素值
    	/// 归一化互相关系数
    	void cal_NCC(std::vector<double> vec_1, std::vector<double> vec_2, double& NCC);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    源文件

    void myClass::cal_NCC(
    	std::vector<double> vec_1, 
    	std::vector<double> vec_2,
    	double& NCC)
    {
    	#pragma region 计算图像1的均值,图像2的均值
    	double avg_sum1 = 0.0, avg_sum2 = 0.0;
    	int size = vec_1.size();
    	for (int i = 0; i < size; i++) {
    		avg_sum1 += vec_1[i];
    		avg_sum2 += vec_2[i];
    	}
    	double avg1 = avg_sum1 / size, avg2 = avg_sum2 / size;
    	#pragma endregion
    	
    	#pragma region 图像I1和I2的方差
    	double variance_sum1 = 0.0;
    	double variance_sum2 = 0.0;
    	for (int i = 0; i < size; i++) {
    		variance_sum1 += pow((vec_1[i] - avg1),2);
    		variance_sum2 += pow((vec_2[i] - avg2),2);
    	}
    	
    	double variance_1 = variance_sum1 / size, variance_2 = variance_sum2 / size;
    	#pragma endregion
    
    	#pragma region 计算I1 and I2的协方差
    	double covrian_sum = 0.0;
    	for (int i = 0; i < size; i++) {
    		covrian_sum += ((vec_1[i] - avg1) * (vec_2[i] - avg2));
    	}
    	double covrian = covrian_sum / size;
    	
    	#pragma endregion
    
    	#pragma region 计算NCC
    	NCC = covrian / sqrt(variance_1 * variance_2);
    
    	#pragma endregion
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    主函数

    int main() {
    	myClass my_obj;
    	string fixed_image_path = "../BrainProtonDensitySliceBorder20.png";
    	string moving_image_path = "../BrainProtonDensitySliceShifted13x17y.png";
    	FixedImageType::Pointer fixed_image, moving_image;
    	my_obj.read_png(fixed_image_path, fixed_image);
    	my_obj.read_png(moving_image_path, moving_image);
    
    	FixedImageType::SizeType org_size_1 = fixed_image->GetLargestPossibleRegion().GetSize();
    	cout << org_size_1 << endl;
    	float* fixed_image_buffer = fixed_image->GetBufferPointer();
    	MovingImageType::SizeType org_size_2 = moving_image->GetLargestPossibleRegion().GetSize();
    	cout << org_size_2 << endl;
    	float* moving_image_buffer = moving_image->GetBufferPointer();
    	
    	vector<double> fixed_array, moving_array;
    	for (int i = 0; i < org_size_1[0] * org_size_1[1];i+=1) {
    		fixed_array.push_back(fixed_image_buffer[i]);
    		moving_array.push_back(moving_image_buffer[i]);
    	}
    	
    	double ncc = 0.0;
    	my_obj.cal_NCC(fixed_array, moving_array,ncc);
    	cout << "Ncc = " << ncc << endl;
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    结果

    在这里插入图片描述

  • 相关阅读:
    合并石子问题-区间dp总结
    图论思想与UML应用 (上 )开放封闭原则
    学术特稿 | 著名书法家项国就:中国古代书法章草美学展现的形式分析
    【Godot】数据响应的方式执行功能
    微服务整合Seata1.5.2+Nacos2.2.1+SpringBoot
    通往优秀软件架构师之路:掌握技术核心,修炼基础原理【文中送书,十本任选】
    Windows定时任务实现开机自启动
    空间金字塔池化Spatial Pyramid Pooling
    textarea标签改写为富文本框编辑器KindEditor
    浅析 vSAN 磁盘组架构和缓存盘的“消亡”
  • 原文地址:https://blog.csdn.net/sdhdsf132452/article/details/126786177