【1】MATLAB isfield的C移植实现

之前做过imfilter的代码移植,由于移植中遇到的核 (nx1,并且n 很大) 的特殊,无论是通过filter2D+flip还是自制滤波引擎,所得到的结果都存在一定差距,这让我第一次感觉到了MATLAB和opencv(c++)的差别。

【1】第二个任务相对比较简单,主要面对的是isfield的移植。

                                 isfield的调用格式是:

                                                tf = isfield(S, 'fieldname')                                       

提到isfield,就顺便提一下MATLAB中的结构体,

                                  struct的使用格式是:

                   s = sturct('field1',values1,'field2',values2,…);

其中的'field_',经过实践,个人理解其实是成员名,而后面紧跟的value_,其实就是成员的存储变量。在这个基础对于isfield也就不难理解了:

               它是判断结构体S中是否存在成员fieldname,若存在返回1,若不存在返回0.


【2】在网上查了很多方法,最终还是选择使用__if_exists__if_not_exists来实现函数功能。

             具体语法:

      __if_exists ( identifier ) {   
      statements  
       };  

identitier即是要测试存在性的标示符;statements即是存在是要执行的语句。(__if_not_exists则是判断不存在时及其要执行的语句

【3】关于__if_exists和__if_not_exists的具体用法,网上可以查到,这里仅仅介绍对struct结构体的使用。

程序具体代码如下:

#include <iostream>
#include < opencv2/highgui/highgui.hpp >
#include "writeMat.h"


using namespace std ;
using namespace cv;


bool compareFeature(float f1 ,float f2,int type,struct Paras);
struct Paras { float th1;float alpha;};

void main()
{
	//参数
	float f1 = 0.2;
	float f2 = 0.2;
	int type = 1;  

	Paras paras;
	//结构体成员初值
    paras.th1 = 0.1;
	paras.alpha = 0.1;

	bool label;
	label = compareFeature(f1 ,f2 ,type ,paras );
	cout<< "label"<<"="<<label<<endl;
	
	waitKey(0);
	return ;	
}

bool compareFeature(float f1 ,float f2 ,int type, Paras paras)
{
	bool label=0;
	float th1,alpha,th4r;
	if(type == 1){
		__if_exists (Paras::th1){
			  __if_exists (Paras::alpha){
					label = f1>paras.th1 & f2*paras.alpha < paras.th1; //th1,alpha都存在
					return label;
			  }
			  __if_not_exists (Paras::alpha){
					alpha = 0.8;
					label = f1>paras.th1 & f2*alpha < paras.th1;       //th1存在,alpha不存在
					return label;
			  }
		}
		__if_not_exists (Paras::thl){
			  th1=0.1;
			  __if_not_exists (Paras::alpha){
					alpha=0.8;
					label = f1>th1 & f2*alpha < th1;        //th1,alpha都不存在
					return label;
			  }
			  __if_exists (Paras::alpha){
					label = f1>th1 & f2*paras.alpha < th1; //th1存在,alpha不存在
					return label;
			  }
		}
	}
	
}


在使用过程中,一定要注意: __if_exists和 __if_not_exists括号中要用结构体类型(如程序中Para::th1)而不是一个结构体实体对象(para::th1)



猜你喜欢

转载自blog.csdn.net/A_Jia_17/article/details/62039262
今日推荐