www.gusucode.com > IPCV_Eval_Kit_R2019a_0ce6858工具箱源码程序matlab > IPCV_Eval_Kit_R2019a_0ce6858/code/demo_files/I4_02_1_featureMatching_BRISK.m

    %% 摿挜揰傪梡偄偨暔懱偺専弌
clc;clear;close all;imtool close all;

%% 専嶕偡傞暔懱偺夋憸偺撉崬傒\帵
Iref = imread('I4_02_1_p1a.jpg');
figure; imshow(Iref);

%% 旕専嶕夋憸偺撉崬傒\帵
I = imread('I4_02_1_p2a.jpg');
figure;imshow(I);

%% 偦傟偧傟偺夋憸偱丄摿挜揰専弌丒昞帵
Gref = rgb2gray(Iref);        % 僌儗乕僗働乕儖傊曄姺
G    = rgb2gray(I   );

PointsRef = detectBRISKFeatures(Gref);  % 摿挜揰偺専弌
PointsI   = detectBRISKFeatures(G);

subplot(1,2,1); imshow(Iref); hold on;
plot(PointsRef.selectStrongest(100));     % 忋埵100億僀儞僩傪僾儘僢僩
subplot(1,2,2); imshow(I   ); hold on;
plot(PointsI.selectStrongest(100)); hold off; shg;

%% 摿挜検(Feature, ValidPoints)拪弌丄昞帵
[FeaturesRef, vPointsRef] = extractFeatures(Gref, PointsRef, 'Method', 'BRISK');
[FeaturesI,   vPointsI  ] = extractFeatures(G,    PointsI,   'Method', 'BRISK');

subplot(1,2,1); imshow(Iref); hold on;
plot(vPointsRef.selectStrongest(100),'showOrientation',true);
subplot(1,2,2); imshow(I); hold on;
plot(vPointsI.selectStrongest(100),'showOrientation',true);hold off;shg;

%% 摿挜検(FeaturesRef,FeaturesI)偺儅僢僠儞僌丒寢壥昞帵   (堦晹 outlier 偑懚嵼)
indexPairs = matchFeatures(FeaturesRef, FeaturesI, 'MatchThreshold',30, 'MaxRatio',0.8);

matchedPointsRef = vPointsRef(indexPairs(:, 1));  % Iref忋偺埵抲庢弌
matchedPointsI   = vPointsI(  indexPairs(:, 2));  % I忋偺埵抲庢弌
figure;
showMatchedFeatures(Iref, I, matchedPointsRef, matchedPointsI, 'montage'); truesize;

%% 曄姺峴楍偺悇掕(RANSAC)偲丄岆懳墳揰偺彍嫀丄惓懳墳揰偺昞帵
[tform, inlierPointsRef, inlierPointsI] = ...
    estimateGeometricTransform(matchedPointsRef, matchedPointsI, 'projective', 'MaxDistance', 3);
figure; showMatchedFeatures(Iref, I, inlierPointsRef, inlierPointsI, 'montage');

%% 摼傜傟偨曄姺峴楍偺昞帵
tform.T

%% 摼傜傟偨峴楍偱椞堟傪曄姺偟丄専弌偟偨晹暘傪慄偱埻傓
PolygonRef = bbox2points([1 1 size(Iref, 2) size(Iref, 1)]);   %尦夋憸偺巐嬿
newPolygonRef = transformPointsForward(tform, PolygonRef);    %曄姺屻偺巐嬿

foundObj = insertShape(I, 'Polygon', reshape(newPolygonRef',[1 8]), 'Color','red', 'LineWidth',10);
figure; imshow(foundObj);

%% 廔椆






% 
% 
% 
% 
% %% 尒偮偐偭偨暔懱偺埵抲傪敀偔昞帵
% Orig = true(size(Iref(:,:,1)));
% foundObj = imwarp(Orig, tform, 'OutputView', imref2d(size(I)));
% figure; imshow(foundObj);
% 
% %% 尦夋憸偵廳偹偰昞帵
% figure;imshowpair(I, foundObj, 'blend');
% 
% %% 尒偮偐偭偨暔懱偺埵抲偺奺庬忣曬偺庢摼
% stats = regionprops(foundObj, 'Centroid')      % 椞堟偺廳怱
% 
% %% 掙曈偺妏搙 (億儕僑儞偺掙曈偺2揰偺嵗昗偐傜寁嶼)
% angleBottom = atan2d( -1 * (newPolygonRef(2,3) - newPolygonRef(2,4)), ...
%                             newPolygonRef(1,3) - newPolygonRef(1,4)  )   % 扨埵偼搙
% 
% %% 旕専嶕夋憸忋偺暔懱晹暘傪拪弌
% croppedI = I;
% croppedI(repmat(~foundObj, [1 1 3])) = 0;
% figure; imshow(croppedI);
%                 
% %% 専嶕暔懱偺夋憸傪婔壗妛揑曄姺
% convertedRef = imwarp(Iref, tform, 'OutputView', imref2d(size(I)));
% figure; imshow(convertedRef);
% 
% %% 桳岠側懳墳揰偺悢
% inlierPointsRef.Count
% 
% %% 尦偺夋憸忋偺揰傪曄姺偟丄巆偭偨儅僢僠儞僌僄儔乕傪寁嶼
% inlierPointsRef1 = transformPointsForward(tform, inlierPointsRef.Location);
% 
% dis = (inlierPointsI.Location - inlierPointsRef1) .^2
% dis1 = sum(sqrt(dis(:,1) + dis(:,2)))
% 
% 
% %% 
% 
% % Copyright 2015 The MathWorks, Inc.