角点测评可重复率函数原始

角点测评可重复率函数原始

function Result=Comparison_cpda()
     close all
     clc
     tic
     Result=[];
     parfor cnt =1:23
            cnt
            ldpath = sprintf('%d.bmp',cnt);
            f=imread(ldpath);
            [original_corners]=cpda(f);         %获得原始图片的角点
            Ar_a=[];
            Le_a=[];
            %% 旋转%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 旋转
            %rotation transformation,from -90°to 90°,at 10° apart,excluding 0°
            toppoints=[];
            Le_a_for_rotation=[];                                 %存放图片旋转后的定位误差
            Ar_a_for_rotation=[];                                 %存放图片旋转后的平均重复率
            for angle=0
                fr=imrotate(f,angle);
                %
                [change_corners,toppoints]=rotation_coordinate_change(original_corners,f,fr,angle);
                [Ar,Le]=calculate(fr,change_corners,toppoints);
                
                Ar_a_for_rotation=[Ar_a_for_rotation;Ar];
                Le_a_for_rotation=[Le_a_for_rotation;Le];
            end
            for angle=-90:10:-10
                fr=imrotate(f,angle);
                [change_corners,toppoints]=rotation_coordinate_change(original_corners,f,fr,angle);
                [Ar,Le]=calculate(fr,change_corners,toppoints);
                
                Ar_a_for_rotation=[Ar_a_for_rotation;Ar];
                Le_a_for_rotation=[Le_a_for_rotation;Le];
            end
            for angle=10:10:90
                fr=imrotate(f,angle);
                [change_corners,toppoints]=rotation_coordinate_change(original_corners,f,fr,angle);
                [Ar,Le]=calculate(fr,change_corners,toppoints);
                
                Ar_a_for_rotation=[Ar_a_for_rotation;Ar];
                Le_a_for_rotation=[Le_a_for_rotation;Le];

            end  
            Ar_a_for_rotation=mean(Ar_a_for_rotation);             %平均
            Le_a_for_rotation=mean(Le_a_for_rotation);

              Ar_a=[Ar_a;Ar_a_for_rotation];
              Le_a=[Le_a;Le_a_for_rotation];

            %% 旋转End%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 旋转End
            
            %% 比例%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 比例
              %%uniform and nonuniform scaling transformation,x from 0.5 to 2.0,at 0.1 apart,y from 0.5 
            toppoints=[];
            Ar_a_for_scale=[];
            Le_a_for_scale=[];
            for scale_x=0.5:0.1:2.0
                for scale_y=0.5:0.1:2.0
                    if scale_x~=1 ||scale_y~=1 
                        T = maketform('affine',[scale_x 0 0; 0 scale_y 0; 0 0 1]);
                        fu= imtransform(f,T);
                        a_first_change=nonuniform_coordinate_change(original_corners,scale_x,scale_y);
                        [Ar,Le]=calculate(fu,a_first_change,toppoints);
                        Le_a_for_scale=[Le_a_for_scale;Le];
                        Ar_a_for_scale=[Ar_a_for_scale;Ar];
                    end
                end
            end
            Ar_a_for_scale=mean(Ar_a_for_scale);
            Le_a_for_scale=mean(Le_a_for_scale);

              Ar_a=[Ar_a;Ar_a_for_scale];
              Le_a=[Le_a;Le_a_for_scale];
            %% 比例End%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 比例变换End
            
            %% 仿射%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 仿射
            %combined(affine) transformation,angles range form -30°to -30°;x,y from 
            %0.8 to 1.2,at 0.1 apart,y from 0.8 to 1.2,at 0.1 apart,excluding x=y
            toppoints=[]; 
            Le_a_for_affine=[];
            Ar_a_for_affine=[];
            angle_array=[-30 -20 -10 10 20 30 ];
            for i=1:6
                angle=angle_array(i);
                for scale_x=0.8:0.1:1.2
                    for scale_y=0.8:0.1:1.2
                        if abs(scale_x-scale_y)>1e-3
                            if scale_x~=1 ||scale_y~=1 
                                T = maketform('affine',[scale_x 0 0; 0 scale_y 0; 0 0 1]);
                                fr=imrotate(f,angle);
                                fu= imtransform(fr,T);
                                [a_first_change1,toppoints]=rotation_coordinate_change(original_corners,f,fr,angle);
                                a_first_change2=nonuniform_coordinate_change(a_first_change1,scale_x,scale_y);
                                if isempty(toppoints)==0
                                toppoints=floor(toppoints.*repmat([scale_y scale_x],[size(toppoints,1),1]));
                                end
                                [Ar,Le]=calculate(fu,a_first_change2,toppoints);
                                Le_a_for_affine=[Le_a_for_affine;Le];
                                Ar_a_for_affine=[Ar_a_for_affine;Ar];
                            end
                        end
                    end
                end
            end

            Ar_a_for_affine=mean(Ar_a_for_affine);
            Le_a_for_affine=mean(Le_a_for_affine);


              Ar_a=[Ar_a;Ar_a_for_affine];
              Le_a=[Le_a;Le_a_for_affine];

            %% 仿射End%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 仿射变换End
            
            %% Jpeg%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Jpeg
            toppoints=[];
            Le_a_for_compression=[];
            Ar_a_for_compression=[];
            for quality_factor=5:5:100
                imwrite(f,'a_compression.jpg','Quality',quality_factor,'mode','lossy');
                fc=imread('a_compression.jpg');
                [Ar,Le]=calculate(fc,original_corners,toppoints);
                Le_a_for_compression=[Le_a_for_compression;Le];
                Ar_a_for_compression=[Ar_a_for_compression;Ar];
            end

            Ar_a_for_compression=mean(Ar_a_for_compression);
            Le_a_for_compression=mean(Le_a_for_compression);


              Ar_a=[Ar_a;Ar_a_for_compression];
              Le_a=[Le_a;Le_a_for_compression];

            %% JpegEnd%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% JpegEnd
            
            %% 高斯噪声%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 高斯噪声
            toppoints=[];
            Le_a_for_noise=[];
            Ar_a_for_noise=[];
            for noise=0.005:0.005:0.05
                fi=imnoise(f,'Gaussian',noise);
                [Ar,Le]=calculate(fi,original_corners,toppoints);
                Le_a_for_noise=[Le_a_for_noise;Le];
                Ar_a_for_noise=[Ar_a_for_noise;Ar];
            end

            Ar_a_for_noise=mean(Ar_a_for_noise);
            Le_a_for_noise=mean(Le_a_for_noise);
            
            
            Ar_a=[Ar_a;Ar_a_for_noise];
            Le_a=[Le_a;Le_a_for_noise];
            
            Ar_a=Ar_a
            Le_a=Le_a
            
            Result=[Result;Ar_a];
            Result=[Result;Le_a];

            %% 高斯噪声End%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 高斯噪声End
    end % end cnt =1:12
    xlswrite('03Output/result_sum_cpda.xlsx',Result);
    toc
end

%本函数为图片旋转后,GT角点应该也响应旋转
%In:
%corners:Ground Truth为原来角点
%image_or为原始图片
%image_ch为旋转后的图片
%angele为图片旋转后的角度

%Out:
%corners:
function [corners,toppoints]=rotation_coordinate_change(corners,image_or,image_ch,angle)
    if isempty(corners)==0
        theta=-angle*pi/180;
        [cyo,cxo]=size(image_or);
        [cy_ro,cx_ro]=size(image_ch);
        tp1=[1 1];
        tp2=[1 cyo];
        tp3=[cxo 1];
        tp4=[cxo cyo];
        xo=corners(:,2);
        yo=corners(:,1);
        corners=[tp1;tp2;tp3;tp4;xo yo];
        corners_change_axis=corners.*repmat([1 -1],[size(corners,1),1])+repmat([-cxo/2 cyo/2],[size(corners,1),1]);
        corners_rotate=corners_change_axis*[cos(theta) -sin(theta);sin(theta) cos(theta)];
        corners_restore=corners_rotate.*repmat([1 -1],[size(corners_rotate,1),1])+repmat([cx_ro/2 cy_ro/2],[size(corners_rotate,1),1]);
        corners_restore=floor(corners_restore);
        corners=[corners_restore(:,2) corners_restore(:,1)];
        toppoints=corners(1:4,:);
        corners=corners(5:size(corners,1),:);
    else
        corners=[];
        toppoints=[];
    end
end

function [Ar,Le]=calculate(f,change_corners,toppoints)
    [detect_corners]=cpda(f);
    detect_corners2=detect_corners;
    size_change=size(change_corners,1);
    size_detect=size(detect_corners,1);
    Nc=size_change;
    Nd=size(detect_corners,1);
    Nr=0;
    Ar=0;
    Le=0;
    % mark=[];
    % miss=[];
    % false_corners=detect_corners;
    %% match first level corners
    if isempty(detect_corners)==0

        for i=1:size_change
            compare_first_corner=detect_corners-ones(size_detect,1)*change_corners(i,:);
            compare_first_corner=compare_first_corner.^2;
            compare_first_corner=compare_first_corner(:,1)+compare_first_corner(:,2);
            mark=find(compare_first_corner<=18);
            if size(mark,1)==0
    %             Nm=Nm+1;
    %             miss=[miss;change_corners(i,:)];
            else
                detect_corners(find(compare_first_corner==min(compare_first_corner)),:)=0;
                Nr =Nr+1;
                Le=Le+min(compare_first_corner);
            end
        end


    %%
        if isempty(toppoints)==0
            for i=1:4
            compare_first_corner=detect_corners2-ones(size_detect,1)*toppoints(i,:);
            compare_first_corner=compare_first_corner.^2;
            compare_first_corner=compare_first_corner(:,1)+compare_first_corner(:,2);
            mark=find(compare_first_corner<=18);
            if size(mark,1)~=0
                Nd=Nd-size(mark,1);
            end
            end
        end    
    %%
        Le=sqrt(Le/Nd);  
        Ar=Nr/2*(1/Nc+1/Nd);
    else
        Le=0;
        Ar=0;
    end
    
end

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%change the standard corners' coordinate when the images transformed with
%%uniform or nonuniform scaling
function [corners]=nonuniform_coordinate_change(corners,scale_x,scale_y)
    if isempty(corners)==0
        corners=floor(corners.*repmat([scale_y scale_x],[size(corners,1),1]));
    else
        corners=[];
    end
end

角点测评基于。。重复率函数

function Result=Comparisonb_superpiont()
 close all
 clc
 tic

Result=[];

Precision_a=[];
Recall_a=[];
ACU_a=[];
F_a=[];
PicCount=0;

addpath('superpoint_original');
addpath('gttxt');
addpath('02Images');

for cnt =1:21
    
    
    PicCount=PicCount+1;
    ResultCol=[];
%     gt
     txtfilename=strcat('gttxt/',num2str(cnt),'.txt')
    a_first=getCoord(txtfilename);
%    a_first(:,[1, 2] ) =  a_first(:,[2, 1] );
    ldpath = sprintf('%d.png',cnt);  %这里不需要检测坐标,但是还是需要读取原图
    f=imread(ldpath);
   
%     原始角点
    or_filename=strcat(num2str(cnt),'_r_0.txt');
     or_corner=getCoord(or_filename)
     or_corner(:,[1, 2] ) =  or_corner(:,[2, 1] );
%      用gt对原始角点过滤
     filerorigin=filter_origion(or_corner,a_first);
     
      
cnt





%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Rotation
%rotation transformation,from -90°to 90°,at 10° apart,excluding 0°
 Ar_a=[];
 Le_a=[];
%% 旋转%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 旋转
%rotation transformation,from -90°to 90°,at 10° apart,excluding 0°
toppoints=[];
Le_a_for_rotation=[];                                 %存放图片旋转后的定位误差
Ar_a_for_rotation=[];                                 %存放图片旋转后的平均重复率
for angle=0
    fr=imrotate(f,angle);
    filename=strcat(num2str(cnt),'_r_',num2str(angle),'.txt')
    [filerorigin_change,toppoints]=rotation_coordinate_change(filerorigin,f,fr,angle);
     [Ar,Le]=calculate(filename,filerorigin_change,toppoints);
     
    Ar_a_for_rotation=[Ar_a_for_rotation;Ar];
    Le_a_for_rotation=[Le_a_for_rotation;Le];
    
    

end
for angle=-90:10:-10
    fr=imrotate(f,angle);
    filename=strcat(num2str(cnt),'_r_',num2str(angle),'.txt');
    [filerorigin_change,toppoints]=rotation_coordinate_change(filerorigin,f,fr,angle);
    [Ar,Le]=calculate(filename,filerorigin_change,toppoints);
    
    Ar_a_for_rotation=[Ar_a_for_rotation;Ar];
    Le_a_for_rotation=[Le_a_for_rotation;Le];

end
for angle=10:10:90
    fr=imrotate(f,angle);
    filename=strcat(num2str(cnt),'_r_',num2str(angle),'.txt');
    [filerorigin_change,toppoints]=rotation_coordinate_change(filerorigin,f,fr,angle);
    [Ar,Le]=calculate(filename,filerorigin_change,toppoints);
    
    Ar_a_for_rotation=[Ar_a_for_rotation;Ar];
    Le_a_for_rotation=[Le_a_for_rotation;Le];

end
            Ar_a_for_rotation=mean(Ar_a_for_rotation);             %平均
            Le_a_for_rotation=mean(Le_a_for_rotation);

              Ar_a=[Ar_a;Ar_a_for_rotation];
              Le_a=[Le_a;Le_a_for_rotation];



%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% scaling
%%uniform and nonuniform scaling transformation,x from 0.5 to 2.0,at 0.1 apart,y from 0.5 
%%to 2.0,at 0.1 apart,excluding x=y
toppoints=[];
Ar_a_for_scale=[];
Le_a_for_scale=[];
for scale_x=0.5:0.1:2.0
    for scale_y=0.5:0.1:2.0
            if scale_x~=1 ||scale_y~=1 
                filename=strcat(num2str(cnt),'_s_',num2str(scale_x),'_',num2str(scale_y),'.txt');
                
                T = maketform('affine',[scale_x 0 0; 0 scale_y 0; 0 0 1]);
                %fu= imtransform(f,T);
                filerorigin_change=nonuniform_coordinate_change(filerorigin,scale_x,scale_y);
                [Ar,Le]=calculate(filename,filerorigin_change,toppoints);

                Le_a_for_scale=[Le_a_for_scale;Le];
                Ar_a_for_scale=[Ar_a_for_scale;Ar];
            end
    end
end

            Ar_a_for_scale=mean(Ar_a_for_scale);
            Le_a_for_scale=mean(Le_a_for_scale);

              Ar_a=[Ar_a;Ar_a_for_scale];
              Le_a=[Le_a;Le_a_for_scale];

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    Affine
%combined(affine) transformation,angles range form -30°to -30°;x,y from 
%0.8 to 1.2,at 0.1 apart,y from 0.8 to 1.2,at 0.1 apart,excluding x=y
toppoints=[]; 
Le_a_for_affine=[];
Ar_a_for_affine=[];


angle_array=[-30 -20 -10 10 20 30 ];
for i=1:6
    angle=angle_array(i);
    for scale_x=0.8:0.1:1.2
        for scale_y=0.8:0.1:1.2
            if abs(scale_x-scale_y)>1e-3
                if scale_x~=1 ||scale_y~=1 
                    filename=strcat(num2str(cnt),'_a_',num2str(angle),'_',num2str(scale_x),'_',num2str(scale_y),'.txt');
                    
                    T = maketform('affine',[scale_x 0 0; 0 scale_y 0; 0 0 1]);
                    fr=imrotate(f,angle);
                    fu= imtransform(fr,T);
                    [filerorigin_change1,toppoints]=rotation_coordinate_change(filerorigin,f,fr,angle);
                    filerorigin_change2=nonuniform_coordinate_change(filerorigin_change1,scale_x,scale_y);
                    if isempty(toppoints)==0
                        toppoints=floor(toppoints.*repmat([scale_y scale_x],[size(toppoints,1),1]));
                    end
                   [Ar,Le]=calculate(filename,filerorigin_change2,toppoints);
                  Le_a_for_affine=[Le_a_for_affine;Le];
                   Ar_a_for_affine=[Ar_a_for_affine;Ar];
                    
                end
            end
        end
    end
end

            Ar_a_for_affine=mean(Ar_a_for_affine);
            Le_a_for_affine=mean(Le_a_for_affine);


              Ar_a=[Ar_a;Ar_a_for_affine];
              Le_a=[Le_a;Le_a_for_affine];

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Jpeg
%Jpeg lossy compression ,quality factors range from 5 to 100,at 5 apart
toppoints=[];
Le_a_for_compression=[];
Ar_a_for_compression=[];

for quality_factor=5:5:100
%     quality_factor
%     thresh = graythresh(f);%自动确定阈值
%     fuu=im2bw(f,thresh);
    
    filename=strcat(num2str(cnt),'_j_',num2str(quality_factor),'.txt');
    
%     imwrite(fuu,'a_compression.jpg','Quality',quality_factor,'mode','lossy');
%     fc=imread('a_compression.jpg');
    [Ar,Le]=calculate(filename,filerorigin,toppoints);
    
   Le_a_for_compression=[Le_a_for_compression;Le];
   Ar_a_for_compression=[Ar_a_for_compression;Ar];
end

           Ar_a_for_compression=mean(Ar_a_for_compression);
            Le_a_for_compression=mean(Le_a_for_compression);


              Ar_a=[Ar_a;Ar_a_for_compression];
              Le_a=[Le_a;Le_a_for_compression];

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   Gaussian
%gaussian white noise, range from0.005 to 0.05,at 0.005 apart
toppoints=[];
Le_a_for_noise=[];
 Ar_a_for_noise=[];
for noise=0.005:0.005:0.05
    filename=strcat(num2str(cnt),'_n_',num2str(noise),'.txt')

%     f=f.*255;
%     fi=imnoise(f,'Gaussian',noise);
    [Ar,Le]=calculate(filename,filerorigin,toppoints);
     Le_a_for_noise=[Le_a_for_noise;Le];
     Ar_a_for_noise=[Ar_a_for_noise;Ar];
    
end

Ar_a_for_noise=mean(Ar_a_for_noise);
            Le_a_for_noise=mean(Le_a_for_noise);
            Ar_a=[Ar_a;Ar_a_for_noise];
            Le_a=[Le_a;Le_a_for_noise];
            
            Ar_a=Ar_a
            Le_a=Le_a
            
            Result=[Result;Ar_a];
            Result=[Result;Le_a];





Result=[Result;ResultCol];

end

pathname=strcat('result/','repeate_superpoint_');

% mkdir(pathname);
filename=strcat(datestr(now,'ddmmmyyyyHHMMSS'),'.xlsx');
outputname=strcat(pathname,filename);  
xlswrite(outputname,Result);

function [coord]=getCoord(filename)
    
    fid = fopen(filename,'r');
    formatSpec = '%f\t%f';
    sizeA = [2 Inf];
    A = fscanf(fid,formatSpec,sizeA);
%      A([1, 2],: ) =  A([2, 1],: );
%    A= fopen(fid,formatSpec,sizeA);
    fclose(fid);
    a_first=A';   %需要转置
    coord=a_first;



function [corners,toppoints]=rotation_coordinate_change(corners,image_or,image_ch,angle)
if size(image_or,3)==3
    % Transform RGB image to a Gray one.
    image_or=rgb2gray(image_or);
end

if size(image_ch,3)==3
    % Transform RGB image to a Gray one.
    image_ch=rgb2gray(image_ch);
end
if isempty(corners)==0
    theta=-angle*pi/180;
    [cyo,cxo]=size(image_or);
    [cy_ro,cx_ro]=size(image_ch);
    tp1=[1 1];
    tp2=[1 cyo];
    tp3=[cxo 1];
    tp4=[cxo cyo];
    xo=corners(:,2);
    yo=corners(:,1);
    corners=[tp1;tp2;tp3;tp4;xo yo];
    corners_change_axis=corners.*repmat([1 -1],[size(corners,1),1])+repmat([-cxo/2 cyo/2],[size(corners,1),1]);
    corners_rotate=corners_change_axis*[cos(theta) -sin(theta);sin(theta) cos(theta)];
    corners_restore=corners_rotate.*repmat([1 -1],[size(corners_rotate,1),1])+repmat([cx_ro/2 cy_ro/2],[size(corners_rotate,1),1]);
    corners_restore=floor(corners_restore);
    corners=[corners_restore(:,2) corners_restore(:,1)];
    toppoints=corners(1:4,:);
    corners=corners(5:size(corners,1),:);
else
    corners=[];
    toppoints=[];
end


%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%change the standard corners' coordinate when the images transformed with
%%uniform or nonuniform scaling
function [corners]=nonuniform_coordinate_change(corners,scale_x,scale_y)
if isempty(corners)==0
    corners=floor(corners.*repmat([scale_y scale_x],[size(corners,1),1]));
else
    corners=[];
end


%%%%%%%%%%%%%%%%%检测到的角点和gt比较%%%%%%%%%%%%%%%%%%
function [filtercorners]=filter_origion(corners,cor_first)
  original_cor=corners; 
  size_first=size(cor_first,1);
  size_corners=size(corners,1);
%   filter_origion_corners=[];
  No=size_first;
  Nt=size(corners,1);
  Nr=0;
  Nf=0;
  Nm=0;
  mark=[];
  miss=[];
  filtercorners=[];
  false_corners=corners;
 if isempty(corners)==0
    for i=1:size_first
        compare_first_corner=corners-ones(size_corners,1)*cor_first(i,:);
        compare_first_corner=compare_first_corner.^2;
        compare_first_corner=compare_first_corner(:,1)+compare_first_corner(:,2);
        mark=find(compare_first_corner<=9);
        if size(mark,1)==0
            Nm=Nm+1;
            miss=[miss;cor_first(i,:)];
        else
            filtercorners=[filtercorners;cor_first(i,:)];
            corners(find(compare_first_corner==min(compare_first_corner)),:)=0
            false_corners(find(compare_first_corner==min(compare_first_corner)),:)=0
            Nr =Nr+1;
        end
    end
    miss=miss;
    false_corners=false_corners;
    
%     filtercorners1=original_cor-false_corners
%     indexzero= all(filtercorners1~=0,2)
%      indexnozero=find(indexzero)
%      filtercorners=[filtercorners;filtercorners1(indexnozero,:)]
    Nf=Nt-Nr;  
 else
    Nm=size_first;
    Nr=0;
    Nf=0;
 end
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%filename:变换图片检测的坐标
%cor_first:原图检测坐标进行相应变换
%toppoints:旋转后出现的四个角坐标
function [Ar,Le]=calculate(filename,change_corners,toppoints)
[detect_corners]=getCoord(filename);%%%%%%%this can be replaced by other corner detectors
if size(detect_corners,1)>0
detect_corners(:,[1, 2] ) =  detect_corners(:,[2, 1] );
% imgfilename=filename;
% imgfilename(size(imgfilename,2)-3:size(imgfilename,2))='.jpg';
% figure;imshow(imgfilename);
% hold on;
% plot(corners(:,2),corners(:,1),'r*');
% [corners]=FilterCorner(corners,toppoints);
% figure;imshow(imgfilename); hold on; plot(corners(:,2),corners(:,1),'b*');

detect_corners2=detect_corners;
    size_change=size(change_corners,1);
    size_detect=size(detect_corners,1);
    Nc=size_change;
    Nd=size(detect_corners,1);
    Nr=0;
    Ar=0;
    Le=0;
    % mark=[];
    % miss=[];
    % false_corners=detect_corners;
    %% match first level corners
    if isempty(detect_corners)==0

        for i=1:size_change
            compare_first_corner=detect_corners-ones(size_detect,1)*change_corners(i,:);
            compare_first_corner=compare_first_corner.^2;
            compare_first_corner=compare_first_corner(:,1)+compare_first_corner(:,2);
            mark=find(compare_first_corner<=18);
            
            if size(mark,1)==0
    %             Nm=Nm+1;
    %             miss=[miss;change_corners(i,:)];
%                      Nr=0;
%                      Le=0;
            else
                detect_corners(find(compare_first_corner==min(compare_first_corner)),:)=0;
                Nr =Nr+1;
                Le=Le+min(compare_first_corner);
            end
        end


    %%
        if isempty(toppoints)==0
            for i=1:4
            compare_first_corner=detect_corners2-ones(size_detect,1)*toppoints(i,:);
            compare_first_corner=compare_first_corner.^2;
            compare_first_corner=compare_first_corner(:,1)+compare_first_corner(:,2);
            mark=find(compare_first_corner<=18);
            if size(mark,1)~=0
                Nd=Nd-size(mark,1);
            end
            end
        end    
    %%
%     Le=sqrt(Le/Nd);   
%     Ar=Nr/2*(1/Nc+1/Nd);
      
        if Nd~=0
        Le=sqrt(Le/Nd);  
        else
            Le=0;
        end
        
        if Nc==0
            Ar=Nr/2*(1/Nd);
         
         elseif Nd==0
             Ar=Nr/2*(1/Nc); 
        else
            Ar=Nr/2*(1/Nc+1/Nd);
        end
    else
        Le=0;
        Ar=0;
    end
    else
     Le=0;
        Ar=0;
end






function [corner]=FilterCorner(oldcorner,toppoints)
%corners中有部分坐标是由于旋转后边缘检测的,需要删除
new_c=[];
if isempty(toppoints)==0
    dist=15;
    for i=1:size(oldcorner,1)
        P=oldcorner(i,:)';
        %line1
        Q1=toppoints(1,:)';
        Q2=toppoints(2,:)';
        d1=abs(det([Q2-Q1,P-Q1]))/norm(Q2-Q1);
        
        %line2
        Q1=toppoints(1,:)';
        Q2=toppoints(2,:)';
        d2=abs(det([Q2-Q1,P-Q1]))/norm(Q2-Q1);
        
        %line3
        Q1=toppoints(1,:)';
        Q2=toppoints(2,:)';
        d3=abs(det([Q2-Q1,P-Q1]))/norm(Q2-Q1);
        
        %line4
        Q1=toppoints(1,:)';
        Q2=toppoints(2,:)';
        d4=abs(det([Q2-Q1,P-Q1]))/norm(Q2-Q1);
        
        
        
        if(d1>dist&&d2>dist&&d3>dist&&d4>dist)
            new_c=[new_c;oldcorner(i,:)];
            continue;
        end
    end
    corner=new_c;
else
    corner=oldcorner;
    
    
    
end

基于。。的可重复率不是读取文件的

function Result=Comparison_weae()
     close all
     clc
     tic
     Result=[];
     for cnt =1:21
            cnt
            ldpath = sprintf('%d.png',cnt);
            f=imread(ldpath);
            f=rgb2gray(f);
            s = ['load(''' int2str(cnt) '.mat'')'];
            a=eval(s);
            a_first=a.first;
            [original_corners1]=weae_g(f);  %获得原始图片的角点
            original_corners= filter_origion(original_corners1,a_first);
            
%             figure(1);
%             imshow(f);
%             hold on;
%             plot(a_first(:,2), a_first(:,1),'ro','MarkerSize',2,'MarkerFaceColor','r');
%             
%             
%              figure(2);
%              imshow(f);
%             hold on;
%             plot(original_corners1(:,2), original_corners1(:,1),'ro','MarkerSize',2,'MarkerFaceColor','r');
%             
%            original_corners1= filter_origion(original_corners,a_first)
%            figure(3);
%             imshow(f);
%             hold on;
%             plot(original_corners(:,2), original_corners(:,1),'ro','MarkerSize',2,'MarkerFaceColor','r');
            
            Ar_a=[];
            Le_a=[];
            %% 旋转%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 旋转
            %rotation transformation,from -90°to 90°,at 10° apart,excluding 0°
            toppoints=[];
            Le_a_for_rotation=[];                                 %存放图片旋转后的定位误差
            Ar_a_for_rotation=[];                                 %存放图片旋转后的平均重复率
            for angle=0
                fr=imrotate(f,angle);
                %
                [change_corners,toppoints]=rotation_coordinate_change(original_corners,f,fr,angle);
                [Ar,Le]=calculate(fr,change_corners,toppoints);
                
                Ar_a_for_rotation=[Ar_a_for_rotation;Ar];
                Le_a_for_rotation=[Le_a_for_rotation;Le];
            end
            for angle=-90:10:-10
                fr=imrotate(f,angle);
                [change_corners,toppoints]=rotation_coordinate_change(original_corners,f,fr,angle);
                [Ar,Le]=calculate(fr,change_corners,toppoints);
                
                Ar_a_for_rotation=[Ar_a_for_rotation;Ar];
                Le_a_for_rotation=[Le_a_for_rotation;Le];
            end
            for angle=10:10:90
                fr=imrotate(f,angle);
                [change_corners,toppoints]=rotation_coordinate_change(original_corners,f,fr,angle);
                [Ar,Le]=calculate(fr,change_corners,toppoints);
                
                Ar_a_for_rotation=[Ar_a_for_rotation;Ar];
                Le_a_for_rotation=[Le_a_for_rotation;Le];

            end  
            Ar_a_for_rotation=mean(Ar_a_for_rotation);             %平均
            Le_a_for_rotation=mean(Le_a_for_rotation);

              Ar_a=[Ar_a;Ar_a_for_rotation];
              Le_a=[Le_a;Le_a_for_rotation];

            %% 旋转End%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 旋转End
            
            %% 比例%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 比例
              %%uniform and nonuniform scaling transformation,x from 0.5 to 2.0,at 0.1 apart,y from 0.5 
            toppoints=[];
            Ar_a_for_scale=[];
            Le_a_for_scale=[];
            for scale_x=0.5:0.1:2.0
                for scale_y=0.5:0.1:2.0
                    if scale_x~=1 ||scale_y~=1 
                        T = maketform('affine',[scale_x 0 0; 0 scale_y 0; 0 0 1]);
                        fu= imtransform(f,T);
                        a_first_change=nonuniform_coordinate_change(original_corners,scale_x,scale_y);
                        [Ar,Le]=calculate(fu,a_first_change,toppoints);
                        Le_a_for_scale=[Le_a_for_scale;Le];
                        Ar_a_for_scale=[Ar_a_for_scale;Ar];
                    end
                end
            end
            Ar_a_for_scale=mean(Ar_a_for_scale);
            Le_a_for_scale=mean(Le_a_for_scale);

              Ar_a=[Ar_a;Ar_a_for_scale];
              Le_a=[Le_a;Le_a_for_scale];
            %% 比例End%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 比例变换End
            
            %% 仿射%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 仿射
            %combined(affine) transformation,angles range form -30°to -30°;x,y from 
            %0.8 to 1.2,at 0.1 apart,y from 0.8 to 1.2,at 0.1 apart,excluding x=y
            toppoints=[]; 
            Le_a_for_affine=[];
            Ar_a_for_affine=[];
            angle_array=[-30 -20 -10 10 20 30 ];
            for i=1:6
                angle=angle_array(i);
                for scale_x=0.8:0.1:1.2
                    for scale_y=0.8:0.1:1.2
                        if abs(scale_x-scale_y)>1e-3
                            if scale_x~=1 ||scale_y~=1 
                                T = maketform('affine',[scale_x 0 0; 0 scale_y 0; 0 0 1]);
                                fr=imrotate(f,angle);
                                fu= imtransform(fr,T);
                                [a_first_change1,toppoints]=rotation_coordinate_change(original_corners,f,fr,angle);
                                a_first_change2=nonuniform_coordinate_change(a_first_change1,scale_x,scale_y);
                                if isempty(toppoints)==0
                                toppoints=floor(toppoints.*repmat([scale_y scale_x],[size(toppoints,1),1]));
                                end
                                [Ar,Le]=calculate(fu,a_first_change2,toppoints);
                                Le_a_for_affine=[Le_a_for_affine;Le];r
                                Ar_a_for_affine=[Ar_a_for_affine;Ar];
                            end
                        end
                    end
                end
            end

            Ar_a_for_affine=mean(Ar_a_for_affine);
            Le_a_for_affine=mean(Le_a_for_affine);


              Ar_a=[Ar_a;Ar_a_for_affine];
              Le_a=[Le_a;Le_a_for_affine];

            %% 仿射End%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 仿射变换End
            
            %% Jpeg%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Jpeg
            toppoints=[];
            Le_a_for_compression=[];
            Ar_a_for_compression=[];
            for quality_factor=5:5:100
                imwrite(f,'a_compression.jpg','Quality',quality_factor,'mode','lossy');
                fc=imread('a_compression.jpg');
                [Ar,Le]=calculate(fc,original_corners,toppoints);
                Le_a_for_compression=[Le_a_for_compression;Le];
                Ar_a_for_compression=[Ar_a_for_compression;Ar];
            end

            Ar_a_for_compression=mean(Ar_a_for_compression);
            Le_a_for_compression=mean(Le_a_for_compression);


              Ar_a=[Ar_a;Ar_a_for_compression];
              Le_a=[Le_a;Le_a_for_compression];

            %% JpegEnd%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% JpegEnd
            
            %% 高斯噪声%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 高斯噪声
            toppoints=[];
            Le_a_for_noise=[];
            Ar_a_for_noise=[];
            for noise=0.005:0.005:0.05
                fi=imnoise(f,'Gaussian',noise);
                [Ar,Le]=calculate(fi,original_corners,toppoints);
                Le_a_for_noise=[Le_a_for_noise;Le];
                Ar_a_for_noise=[Ar_a_for_noise;Ar];
            end

            Ar_a_for_noise=mean(Ar_a_for_noise);
            Le_a_for_noise=mean(Le_a_for_noise);
            
            
            Ar_a=[Ar_a;Ar_a_for_noise];
            Le_a=[Le_a;Le_a_for_noise];
            
            Ar_a=Ar_a
            Le_a=Le_a
            
            Result=[Result;Ar_a];
            Result=[Result;Le_a];

            %% 高斯噪声End%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 高斯噪声End
    end % end cnt =1:12
    xlswrite('result/ccn_result_sum_weae-1-21.xlsx',Result);
    toc
end

%本函数为图片旋转后,GT角点应该也响应旋转
%In:
%corners:Ground Truth为原来角点
%image_or为原始图片
%image_ch为旋转后的图片
%angele为图片旋转后的角度

%Out:
%corners:





function [corners,toppoints]=rotation_coordinate_change(corners,image_or,image_ch,angle)
    if isempty(corners)==0
        theta=-angle*pi/180;
        [cyo,cxo]=size(image_or);
        [cy_ro,cx_ro]=size(image_ch);
        tp1=[1 1];
        tp2=[1 cyo];
        tp3=[cxo 1];
        tp4=[cxo cyo];
        xo=corners(:,2);
        yo=corners(:,1);
        corners=[tp1;tp2;tp3;tp4;xo yo];
        corners_change_axis=corners.*repmat([1 -1],[size(corners,1),1])+repmat([-cxo/2 cyo/2],[size(corners,1),1]);
        corners_rotate=corners_change_axis*[cos(theta) -sin(theta);sin(theta) cos(theta)];
        corners_restore=corners_rotate.*repmat([1 -1],[size(corners_rotate,1),1])+repmat([cx_ro/2 cy_ro/2],[size(corners_rotate,1),1]);
        corners_restore=floor(corners_restore);
        corners=[corners_restore(:,2) corners_restore(:,1)];
        toppoints=corners(1:4,:);
        corners=corners(5:size(corners,1),:);
    else
         corners=[];
        toppoints=[];
    end
end
%%%%%%%%%%%%%%%%%检测到的角点和gt比较%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%检测到的角点和gt比较%%%%%%%%%%%%%%%%%%
function [filtercorners]=filter_origion(corners,cor_first)
  original_cor=corners; 
  size_first=size(cor_first,1);
  size_corners=size(corners,1);
%   filter_origion_corners=[];
  No=size_first;
  Nt=size(corners,1);
  Nr=0;
  Nf=0;
  Nm=0;
  mark=[];
  miss=[];
  filtercorners=[];
  false_corners=corners;
 if isempty(corners)==0
    for i=1:size_first
        compare_first_corner=corners-ones(size_corners,1)*cor_first(i,:);
        compare_first_corner=compare_first_corner.^2;
        compare_first_corner=compare_first_corner(:,1)+compare_first_corner(:,2);
        mark=find(compare_first_corner<=9);
        if size(mark,1)==0
            Nm=Nm+1;
            miss=[miss;cor_first(i,:)];
        else
            filtercorners=[filtercorners;cor_first(i,:)];
            corners(find(compare_first_corner==min(compare_first_corner)),:)=0
            false_corners(find(compare_first_corner==min(compare_first_corner)),:)=0
            Nr =Nr+1;
        end
    end
    miss=miss;
    false_corners=false_corners;
    
%     filtercorners1=original_cor-false_corners
%     indexzero= all(filtercorners1~=0,2)
%      indexnozero=find(indexzero)
%      filtercorners=[filtercorners;filtercorners1(indexnozero,:)]
    Nf=Nt-Nr;  
 else
    Nm=size_first;
    Nr=0;
    Nf=0;
 end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Ar,Le]=calculate(f,change_corners,toppoints)
    [detect_corners]=weae_g(f);
    detect_corners2=detect_corners;
    size_change=size(change_corners,1);
    size_detect=size(detect_corners,1);
    Nc=size_change;
    Nd=size(detect_corners,1);
    Nr=0;
    Ar=0;
    Le=0;
    % mark=[];
    % miss=[];
    % false_corners=detect_corners;
    %% match first level corners
    if isempty(detect_corners)==0

        for i=1:size_change
            compare_first_corner=detect_corners-ones(size_detect,1)*change_corners(i,:);
            compare_first_corner=compare_first_corner.^2;
            compare_first_corner=compare_first_corner(:,1)+compare_first_corner(:,2);
            mark=find(compare_first_corner<=18);
            
            if size(mark,1)==0
    %             Nm=Nm+1;
    %             miss=[miss;change_corners(i,:)];
%                      Nr=0;
%                      Le=0;
            else
                detect_corners(find(compare_first_corner==min(compare_first_corner)),:)=0;
                Nr =Nr+1;
                Le=Le+min(compare_first_corner);
            end
        end


    %%
        if isempty(toppoints)==0
            for i=1:4
            compare_first_corner=detect_corners2-ones(size_detect,1)*toppoints(i,:);
            compare_first_corner=compare_first_corner.^2;
            compare_first_corner=compare_first_corner(:,1)+compare_first_corner(:,2);
            mark=find(compare_first_corner<=18);
            if size(mark,1)~=0
                Nd=Nd-size(mark,1);
            end
            end
        end    
    %%
%     Le=sqrt(Le/Nd);   
%     Ar=Nr/2*(1/Nc+1/Nd);
      
        if Nd~=0
        Le=sqrt(Le/Nd);  
        else
            Le=0;
        end
        
        if Nc==0
            Ar=Nr/2*(1/Nd);
         
         elseif Nd==0
             Ar=Nr/2*(1/Nc); 
        else
            Ar=Nr/2*(1/Nc+1/Nd);
        end
    else
        Le=0;
        Ar=0;
    end
    
end

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%change the standard corners' coordinate when the images transformed with
%%uniform or nonuniform scaling
function [corners]=nonuniform_coordinate_change(corners,scale_x,scale_y)
    if isempty(corners)==0
        corners=floor(corners.*repmat([scale_y scale_x],[size(corners,1),1]));
    else
        corners=[];
    end
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值