📄 testtr.m
字号:
% This function tests properties of a functional.% It uses functional.cpp.% Re-compile if a new functional appeared:%% !g++ functional.cpp -o functional -O3%% testTr(fnlNo,typ,tr,P,imagefile)% fnlNo - functional number% typ = 0:(for 0..255) or 2:(for double)% tr - text file with a test trace "tr" (ie just an array),% this can be created by the user, or % - can be a number N, then "tr" is read from trArray<N>.txt% P>0 - show graphs of modified "tr", P=0 - do not show graphs; % default P=1% % You also can use the 4th argument, but some preparations are needed:% imagefile - will show trace transform of this imagefile% extension of the image file should be 3 symbols exactly% this imagefile should be in the currend directory% Directoury Outinfo/TrMatrs should exist% if a functional is changed, compile %% !g++ tt.cpp -o tt -O3%% example: testTr(33,2,1,0,'a0.pgm')function testTr(fnlNo,typ,tr,P,imagefile)if nargin == 5 sfnlNo=num2str(fnlNo);zzz='000';zzz(end-length(sfnlNo)+1:end)=sfnlNo; eval(['!tt -I ' imagefile ' -T ' zzz ' -wTtxt']) imagefile1=imagefile(1:end-4);%we suppose that extension is .??? M=textread(['Outinfo/TrMatrs/' imagefile1 'T' zzz '.txt'])'; figim(M); title(['fnl No. ' num2str(fnlNo) ' image:' imagefile]);pause(0.01)endif nargin<4, P=1;end% P>0 show pictures, P=0 do not show picturesif nargin<3 load trArray1; % -this should provide tr if nargin<2 typ=2; % default = 2 = double endendif length(tr)<2 eval(['load trArray' num2str(tr)])%another trArrayendif typ==0,tr(tr<=0)=0;tr(tr>=255)=255;tr=round(tr);end%make it unsigned intn=length(tr);%%%%%%%%%Reading attributes:[attName,attIsfnl,attKind,attGray,attBack,attSack,... attHasKappa,attKappa,attHasLambda,attLambda,attBlank,attIsInteger]... =arrtibutesFunctional(fnlNo)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%if P,o,plot(tr,'*-'); end%check attKappa: contraction at origin (using fact lbeg=0)ar=[1 1.1:0.8:5];fval=zeros(size(ar));Co=zeros(size(ar));N=0;for a=arAA=interp1(tr,a*(0:n-1)+1,'v5cubic');%v5 means without extrapolationAA(isnan(AA))=0;if typ==0,AA(AA<=0)=0;AA(AA>=255)=255;AA=round(AA);end%make it unsigned intif P,plot(AA,'c');title(num2str(a));pause(0.01);end%compute the functional on AA:fnlvalue=computeFunctional(fnlNo,typ,AA);N=N+1;fval(N)=fnlvalue(1);Co(N)=fval(N)/fval(1);endKappa=log(Co(2:N))./log(ar(2:N));suggestedkappa=mean(Kappa);%keyboard%kkkkkkkkkkkkkkkkkkkkdisp(['suggested kappa = ' num2str(suggestedkappa) ... ' with std = ' num2str(std(Kappa))])disp([' Existing kappa '])if ~attHasKappa, disp(' Has no kappa'),else disp([' ' num2str(attKappa)])endif P,plot(tr,'*-'); end%check attLabmda:ar=[1:-0.1:0.5];fval=zeros(size(ar));Co=zeros(size(ar));N=0;for a=arAA=a*tr;if typ==0,AA(AA<=0)=0;AA(AA>=255)=255;AA=round(AA);end%make it unsigned intif P,plot(AA,'y');title(num2str(a));pause(0.01);end%compute the functional on AA:fnlvalue=computeFunctional(fnlNo,typ,AA);N=N+1;fval(N)=fnlvalue(1);Co(N)=fval(N)/fval(1);endLambda=log(Co(2:N))./log(ar(2:N));suggestedlambda=mean(Lambda);disp(['suggested lambda = ' num2str(suggestedlambda) ... ' with std = ' num2str(std(Lambda))])disp([' Existing lambda '])if ~attHasLambda, disp(' Has no lambda'),else disp([' ' num2str(attLambda)])endif P,plot(tr,'*-'); end%check attKind:a=2;AA=interp1(tr,a*(0:n-1)+1,'v5cubic');%v5 means without extrapolationAA(isnan(AA))=0;if typ==0,AA(AA<=0)=0;AA(AA>=255)=255;AA=round(AA);end%make it unsigned intif P,plot(AA,'g');title(num2str(a));pause(0.01);endA=AA;ar=[1:n/2/7.14:n/2];fval=zeros(size(ar));Co=zeros(size(ar));N=0;for a=ar AA=interp1(A,(1:n)-a,'v5cubic');%v5 means without extrapolationAA(isnan(AA))=0;if typ==0,AA(AA<=0)=0;AA(AA>=255)=255;AA=round(AA);end%make it unsigned intif P,plot(AA,'g');title(num2str(a));pause(0.01);end%compute the functional on AA:fnlvalue=computeFunctional(fnlNo,typ,AA);N=N+1;fval(N)=fnlvalue(1);Co(N)=fval(N)-fval(1);endif P,plot(tr,'*-'); enddisp(['sensitive (2) with std = ' num2str(std(Co-ar))])disp(['invariant (1) with std = ' num2str(std(Co))])disp([' Existing attKind = ' num2str(attKind)])%check Back:% A - from the previous itemdisp(' ')AA=A(end:-1:1); nn=-round(n/3);nn2=- (nn+(n-1));%o,plot(nn:nn+n-1,A+10,'o');o,plot(nn2:nn2+n-1,AA+10,'o');fnlvalue=computeFunctional(fnlNo,typ,A,nn);fnlvalueBack=computeFunctional(fnlNo,typ,AA,nn2);disp('Check attBack:')Disp(fnlvalue(1),fnlvalue(2),fnlvalueBack(1))disp(' - the last two should be equal')disp([' Existing attBack = ' num2str(attBack)])%check Sack:% A,AA - from the previous itemdisp(' ')fnlvalueSack=computeFunctional(fnlNo,typ,-AA,nn2);disp('Check attSack:')if typ == 0, disp('typ=0, no check for "unsigned char"')elseDisp(fnlvalue(1),fnlvalue(3),fnlvalueSack(1))disp(' - the last two should be equal')disp([' Existing attSack = ' num2str(attSack)])end%check Blank:disp(' ')fnlvalue=computeFunctional(fnlNo,typ,tr*0);disp('Check attBlank:')disp([num2str(fnlvalue(1)) ' - this should be equal to'])disp([' Existing attBlank = ' num2str(attBlank)])%check IsIntegerdisp(' ')disp(' check IsInteger: tr is rounded:')fnlvalue=computeFunctional(fnlNo,typ,round(tr));isint=all( abs(fnlvalue-round(fnlvalue))<0.05);disp(['It seems that attIsInteger should be ' num2str(isint)])disp([' Existing attIsInteger = ' num2str(attIsInteger)])disp(' '); disp('Finished check of ')Disp(fnlNo,typ)disp('attName= '), disp(attName)if attKind == 1%Quality attKappa: contraction at the center ar=[1:3/10:3];fval=zeros(size(ar));Co=zeros(size(ar));N=0;for a=arAA=interp1(tr,a*((0:n-1)-(n-1)/2)+(n+1)/2,'v5cubic');%v5 means without extrapolationAA(isnan(AA))=0;if typ==0,AA(AA<=0)=0;AA(AA>=255)=255;AA=round(AA);end%make it unsigned int%compute the functional on AA:fnlvalue=computeFunctional(fnlNo,typ,AA);N=N+1;fval(N)=fnlvalue(1);endif attHasLambda, kappa=attKappa;else kappa=suggestedkappa;endCo=fval./ar.^kappa;figure,hold on,plot(1,0,'*');plot(ar,Co,'o-r');title('Quality of kappa; if inv.then constant')grid on,xlabel('contraction coefficient')endif attKind == 2%Quality attKappa: contraction at the origin (using fact lbeg=0)ar=[1:3/10:3];fval=zeros(size(ar));Co=zeros(size(ar));N=0;for a=arAA=interp1(tr,a*(0:n-1)+1,'v5cubic');%v5 means without extrapolationAA(isnan(AA))=0;if typ==0,AA(AA<=0)=0;AA(AA>=255)=255;AA=round(AA);end%make it unsigned int%compute the functional on AA:fnlvalue=computeFunctional(fnlNo,typ,AA);N=N+1;fval(N)=fnlvalue(1);endif attHasLambda, kappa=attKappa;else kappa=suggestedkappa;endCo=fval./ar.^kappa;figure,hold on,plot(1,0,'*');plot(ar,Co,'o-r');title('Quality of kappa; if sens.then constant')grid on,xlabel('contraction coefficient')end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function fnlvalue=computeFunctional(fnlNo,typ,AA,Lbeg)%really,it exists in a separate file%compute the functional on AA:fid = fopen('testTr.txt','w');%testTr.txt and testTrOut.txt are temporary filesfprintf(fid,'%d ',AA); % no need of '%d'; if they are only integer, then matlab does integerfclose(fid);fN=fnlNo;L=length(AA);callfunctionalcpp=['!functional ' num2str(fN) ' ' num2str(typ) ' ' num2str(L)];if nargin == 4, callfunctionalcpp=[callfunctionalcpp ' ' num2str(Lbeg)];endeval(callfunctionalcpp)fnlvalue=textread('testTrOut.txt');% three values: non back sackfunction [attName,attIsfnl,attKind,attGray,attBack,attSack,... attHasKappa,attKappa,attHasLambda,attLambda,attBlank,attIsInteger]... =arrtibutesFunctional(fnlNo)fN=fnlNo;eval(['!functional ' num2str(fN)])%makes attributes ready in the text files:attName=textread('testTrName.txt','%s','whitespace','');atts=textread('testTrAtts.txt');k=0;k=k+1;attIsfnl=atts(k);k=k+1;attKind=atts(k);k=k+1;attGray=atts(k);k=k+1;attBack=atts(k);k=k+1;attSack=atts(k);k=k+1;attHasKappa=atts(k);k=k+1;attKappa=atts(k);k=k+1;attHasLambda=atts(k);k=k+1;attLambda=atts(k);k=k+1;attBlank=atts(k);k=k+1;attIsInteger=atts(k);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -