From f618466c25d43f3bae9e40920273bf77de1e1149 Mon Sep 17 00:00:00 2001 From: leochanj105 Date: Mon, 19 Oct 2020 23:09:30 -0400 Subject: initial sd-vbs initial sd-vbs add sd-vbs sd-vbs --- SD-VBS/benchmarks/stitch/src/matlab/calculateH.m | 24 +++++++++ SD-VBS/benchmarks/stitch/src/matlab/dist2.m | 31 ++++++++++++ .../benchmarks/stitch/src/matlab/extractFeatures.m | 56 +++++++++++++++++++++ SD-VBS/benchmarks/stitch/src/matlab/getANMS.m | 25 +++++++++ .../stitch/src/matlab/getDirParamLocal.m | 4 ++ SD-VBS/benchmarks/stitch/src/matlab/harris.m | 37 ++++++++++++++ SD-VBS/benchmarks/stitch/src/matlab/main.m | 47 +++++++++++++++++ .../benchmarks/stitch/src/matlab/matchFeatures.m | 13 +++++ .../stitch/src/matlab/pics/matched_base_cur.jpg | Bin 0 -> 22688 bytes .../stitch/src/matlab/pics/ransaced_feat.jpg | Bin 0 -> 19198 bytes SD-VBS/benchmarks/stitch/src/matlab/printImage.m | 2 + SD-VBS/benchmarks/stitch/src/matlab/ransac.m | 19 +++++++ SD-VBS/benchmarks/stitch/src/matlab/readData.m | 9 ++++ .../stitch/src/matlab/script_run_profile.m | 38 ++++++++++++++ .../stitch/src/matlab/showInterestPoints.m | 7 +++ SD-VBS/benchmarks/stitch/src/matlab/transformH.m | 13 +++++ 16 files changed, 325 insertions(+) create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/calculateH.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/dist2.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/extractFeatures.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/getANMS.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/getDirParamLocal.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/harris.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/main.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/matchFeatures.m create mode 100644 SD-VBS/benchmarks/stitch/src/matlab/pics/matched_base_cur.jpg create mode 100644 SD-VBS/benchmarks/stitch/src/matlab/pics/ransaced_feat.jpg create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/printImage.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/ransac.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/readData.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/script_run_profile.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/showInterestPoints.m create mode 100755 SD-VBS/benchmarks/stitch/src/matlab/transformH.m (limited to 'SD-VBS/benchmarks/stitch/src/matlab') diff --git a/SD-VBS/benchmarks/stitch/src/matlab/calculateH.m b/SD-VBS/benchmarks/stitch/src/matlab/calculateH.m new file mode 100755 index 0000000..caefd74 --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/calculateH.m @@ -0,0 +1,24 @@ +function retH=calculateH(pnt1, pnt2) + +[n temp]=size(pnt1); + +X=zeros(0,8); +b=zeros(0,1); + +for i=1:n + X=[X; pnt1(i,1) pnt1(i,2) 1 0 0 0 -pnt2(i,1)*pnt1(i,1) -pnt2(i,1)*pnt1(i,2);0, 0, 0, pnt1(i,1), pnt1(i,2), 1, -pnt2(i,2)*pnt1(i,1), -pnt2(i,2)*pnt1(i,2)]; + b=[b; pnt2(i,1); pnt2(i,2)]; +end + +if n==4 + %if abs(det(X)~=0 + H=X^(-1)*b; + %else + % H=[0 0 0; 0 0 0; 0 0 1]; + %end +else + Xt = X'; + H=(Xt*X)^(-1)*Xt*b; +end + +retH=[H(1) H(2) H(3);H(4) H(5) H(6);H(7) H(8) 1]; diff --git a/SD-VBS/benchmarks/stitch/src/matlab/dist2.m b/SD-VBS/benchmarks/stitch/src/matlab/dist2.m new file mode 100755 index 0000000..89116ac --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/dist2.m @@ -0,0 +1,31 @@ +function n2 = dist2(x, c) +%DIST2 Calculates squared distance between two sets of points. +% +% Description +% D = DIST2(X, C) takes two matrices of vectors and calculates the +% squared Euclidean distance between them. Both matrices must be of +% the same column dimension. If X has M rows and N columns, and C has +% L rows and N columns, then the result has M rows and L columns. The +% I, Jth entry is the squared distance from the Ith row of X to the +% Jth row of C. +% +% See also +% GMMACTIV, KMEANS, RBFFWD +% + +% Copyright (c) Christopher M Bishop, Ian T Nabney (1996, 1997) + +[ndata, dimx] = size(x); +[ncentres, dimc] = size(c); +if dimx ~= dimc + error('Data dimension does not match dimension of centres') +end + +ct = c'; +opt = (x.^2)'; +op2t = (x.^2)'; +tempT = (ones(ncentres, 1) * sum(opt, 1))'; +n2 = tempT + ones(ndata, 1) * sum(op2t,1) - 2.*(x*ct); + + + diff --git a/SD-VBS/benchmarks/stitch/src/matlab/extractFeatures.m b/SD-VBS/benchmarks/stitch/src/matlab/extractFeatures.m new file mode 100755 index 0000000..38d2f7c --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/extractFeatures.m @@ -0,0 +1,56 @@ +function vecF=extractFeatures(I, x, y, dataDir) + +Igray = I; +w = (1/16)*[1 4 6 4 1]; +[n temp]=size(x); + +%SUB sample rate s=5 +wt = w'; +Iconv=conv2(w, wt, Igray, 'same'); +Iconv=conv2(w, wt, Iconv, 'same'); +Isub=Iconv(1:5:end,1:5:end); +[nr nc]=size(Isub); + +Xsub=min(floor(x/5)+1, nc-4); +Ysub=min(floor(y/5)+1, nr-4); + +if(length(Xsub) < 6 || length(Ysub) < 10) + vecF = [Xsub Ysub]; + return; +end + +newSize = 4; +[rows, cols] = size(I); +if(rows > 32 & cols >32) + newSize = 64; +end + +for i=1:n + + minValy = 3; + minValx = 3; + if( Ysub(i) < 4) + Ysub(i) = 4; + end + if( Xsub(i) < 4) + Xsub(i) = 4; + end + + if( Ysub(i) > size(Isub,1)-4) + Ysub(i) = size(Isub,1)-4; + end + if( Xsub(i) > size(Isub,2)-4) + Xsub(i) = size(Isub,2)-4; + end + + vecF(i,:)=reshape(Isub(Ysub(i)-minValy:Ysub(i)+4,Xsub(i)-minValx:Xsub(i)+4),1,newSize); + + %normalization + vecF(i,:)=vecF(i,:)-mean(vecF(i,:)); + vecF(i,:)=vecF(i,:)/std(vecF(i,:)); + %imagesc(reshape(vecF(i,:),8,8)); colormap gray + %drawnow + %pause +end + + diff --git a/SD-VBS/benchmarks/stitch/src/matlab/getANMS.m b/SD-VBS/benchmarks/stitch/src/matlab/getANMS.m new file mode 100755 index 0000000..caece31 --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/getANMS.m @@ -0,0 +1,25 @@ +function [interestPnts]=getANMS(x, y, v, r, dataDir) +%interestPnts=[x y v] +MAX_LIMIT=100000000; +C_ROBUST=0.9; +r_sq=r^2; +points=[x y v]; +[n temp]=size(v); +[srtdV srtdVIdx]=sort(v,'descend'); +srtdPnts=points(srtdVIdx,:); + +interestPnts=zeros(0,3); + +suppressR=ones(n,1)*MAX_LIMIT; +supId=find(suppressR>r_sq); + +iter = 0; +while length(supId)>0 + interestPnts=[interestPnts; srtdPnts(supId(1),:)]; + srtdPnts=srtdPnts(supId(2:end),:); + suppressR=suppressR(supId(2:end),:); + + suppressR=min(suppressR,(C_ROBUST*interestPnts(end,3)>srtdPnts(:,3)).*((srtdPnts(:,1)-interestPnts(end,1)).^2 + (srtdPnts(:,2)-interestPnts(end,2)).^2)+(C_ROBUST*interestPnts(end,3)<=srtdPnts(:,3))*MAX_LIMIT); + supId=find(suppressR>r_sq); + iter = iter + 1; +end diff --git a/SD-VBS/benchmarks/stitch/src/matlab/getDirParamLocal.m b/SD-VBS/benchmarks/stitch/src/matlab/getDirParamLocal.m new file mode 100755 index 0000000..7b36886 --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/getDirParamLocal.m @@ -0,0 +1,4 @@ +function DIR_PARAM=getDirParam +DIR_PARAM.main_dir=pwd; +DIR_PARAM.data_dir=fullfile(DIR_PARAM.main_dir,'data'); +DIR_PARAM.report_dir=fullfile(DIR_PARAM.main_dir,'report'); diff --git a/SD-VBS/benchmarks/stitch/src/matlab/harris.m b/SD-VBS/benchmarks/stitch/src/matlab/harris.m new file mode 100755 index 0000000..423330e --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/harris.m @@ -0,0 +1,37 @@ + +% Sample code for detecting Harris corners, following +% Brown et al, CVPR 2005 +% by Alyosha Efros, so probably buggy... + +function [x,y,v] = harris(im, dataDir); + +g1 = [1,4,6,4,1; 4,16,24,16,4;6,24,36,24,6;4,16,24,16,4;1,4,6,4,1]/256; +g2 = [1,2,1;2,4,2;1,2,1]/16; + +img1 = conv2(im,g1,'same'); % blur image with sigma_d + +Ix = conv2(img1,[-0.5 0 0.5],'same'); % take x derivative +Iy = conv2(img1,[-0.5;0;0.5],'same'); % take y derivative + +% Compute elements of the Harris matrix H +%%% we can use blur instead of the summing window +Ix2 = conv2(Ix.*Ix,g2,'same'); +Iy2 = conv2(Iy.*Iy,g2,'same'); +IxIy = conv2(Ix.*Iy,g2,'same'); + +R = (Ix2.*Iy2 - IxIy.*IxIy) ... % det(H) + ./ (Ix2 + Iy2 + eps); % trace(H) + epsilon + +% don't want corners close to image border +[rows, cols] = size(im); + +% non-maxima supression within 3x3 windows +nonmax = inline('max(x)'); +Rmax = colfilt(R,[3 3],'sliding',nonmax); % find neighbrhood max +Rnm = R.*(R == Rmax); % supress non-max + +% extract all interest points +[y,x,v] = find(Rnm); + + + diff --git a/SD-VBS/benchmarks/stitch/src/matlab/main.m b/SD-VBS/benchmarks/stitch/src/matlab/main.m new file mode 100755 index 0000000..9be8504 --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/main.m @@ -0,0 +1,47 @@ +Icur_list=readData('capitol'); +Icur=Icur_list{1}; +%Icur=Icur(1:150,:,:); +[x y v]=harris(Icur); +interestPntsCur=getANMS(x, y, v, 24); +Fcur=extractFeatures(Icur, interestPntsCur(:,1), interestPntsCur(:,2)); + +for id=2:length(Icur_list) + Iprev=Icur; + Fprev=Fcur; + interestPntsPrev=interestPntsCur; + Icur=Icur_list{id}; + %Icur=Icur(1:150,:,:); + [x y v]=harris(Icur); + %showInterestPoints(I, x, y) + interestPntsCur=getANMS(x, y, v, 16); + %showInterestPoints(I, interestPnts(:,1), interestPnts(:,2)) + Fcur=extractFeatures(Icur, interestPntsCur(:,1), interestPntsCur(:,2)); + matchedPntsIdx=matchFeatures(Fprev, Fcur); + matchedPntsPrev=interestPntsPrev(matchedPntsIdx(:,1),:); + matchedPntsCur=interestPntsCur(matchedPntsIdx(:,2),:); + + %subplot(1,2,1);showInterestPoints(Iprev, matchedPntsPrev(:,1), matchedPntsPrev(:,2));axis image + %subplot(1,2,2);showInterestPoints(Icur, matchedPntsCur(:,1), matchedPntsCur(:,2));axis image + %drawnow + %pause; + %printImage(['featureMatching' int2str(id-1) '_' int2str(id)]); + [retH retPntsIdx]=ransac(matchedPntsPrev, matchedPntsCur, 10000, 100); + subplot(2,2,1);showInterestPoints(Iprev, matchedPntsPrev(retPntsIdx,1), matchedPntsPrev(retPntsIdx,2));axis image + subplot(2,2,2);showInterestPoints(Icur, matchedPntsCur(retPntsIdx,1), matchedPntsCur(retPntsIdx,2));axis image + T=maketform('projective', retH'); + Tfn_Mat=T; + [nr nc nd]=size(Iprev); + Itrans=imtransform(Iprev,T,'XData', [1 nc], 'YData', [1 nr],'FillValues',[0;0;0]); + subplot(2,2,3);imshow(Itrans) + subplot(2,2,4);imagesc(rgb2gray(abs(Icur-Itrans))) + drawnow + pause; + printImage(['ransac' int2str(id-1) '_' int2str(id)]); +end + + %[Itrans2 XData YData]=imtransform(Iprev,T,'FillValues',[0;0;0]); + %for the fast iteration + %[Itrans2 XData2 YData2]=imtransform(Iprev,T,'XData', [-2*nc 2*nc], 'YData', [-2*nr 2*nr],'FillValues',[0;0;0]); + + %h=imshow(Itrans); + diff --git a/SD-VBS/benchmarks/stitch/src/matlab/matchFeatures.m b/SD-VBS/benchmarks/stitch/src/matlab/matchFeatures.m new file mode 100755 index 0000000..7af77e0 --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/matchFeatures.m @@ -0,0 +1,13 @@ +function retMatch=matchFeatures(vecF1, vecF2) +[n1 temp]=size(vecF1); +[n2 temp]=size(vecF2); + +retMatch=zeros(0,2); + +for i=1:n1 + [val id]=sort(dist2(vecF1(i,:),vecF2)); + if val(2)~=0 & val(1)/val(2)<0.65 + retMatch=[retMatch; i id(1)]; + end +end + diff --git a/SD-VBS/benchmarks/stitch/src/matlab/pics/matched_base_cur.jpg b/SD-VBS/benchmarks/stitch/src/matlab/pics/matched_base_cur.jpg new file mode 100644 index 0000000..a4bd68b Binary files /dev/null and b/SD-VBS/benchmarks/stitch/src/matlab/pics/matched_base_cur.jpg differ diff --git a/SD-VBS/benchmarks/stitch/src/matlab/pics/ransaced_feat.jpg b/SD-VBS/benchmarks/stitch/src/matlab/pics/ransaced_feat.jpg new file mode 100644 index 0000000..821ca11 Binary files /dev/null and b/SD-VBS/benchmarks/stitch/src/matlab/pics/ransaced_feat.jpg differ diff --git a/SD-VBS/benchmarks/stitch/src/matlab/printImage.m b/SD-VBS/benchmarks/stitch/src/matlab/printImage.m new file mode 100755 index 0000000..82e74f0 --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/printImage.m @@ -0,0 +1,2 @@ +function printImage(fname, DIR_PARAM) +print('-djpeg', fullfile(DIR_PARAM, [fname '.jpg'])); diff --git a/SD-VBS/benchmarks/stitch/src/matlab/ransac.m b/SD-VBS/benchmarks/stitch/src/matlab/ransac.m new file mode 100755 index 0000000..4a3c364 --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/ransac.m @@ -0,0 +1,19 @@ +function [retH retPntsIdx]=ransac(pntsPrev, pntsCur, k, epsilon) +[n temp]=size(pntsPrev); +pntsPrev=[pntsPrev(:,1:2) ones(n,1)]; +pntsCur=[pntsCur(:,1:2) ones(n,1)]; + +inlierIdx=cell(k,1); +inlierNum=zeros(k,1); +for i=1:k + seed=randperm(n); + H=calculateH(pntsPrev(seed(1:4),:), pntsCur(seed(1:4),:)); + err=(pntsCur-transformH(H,pntsPrev)); + inlierIdx{i}=find(sum(err(:,1:2).^2,2) < epsilon); + inlierNum=length(inlierIdx{i}); +end + +[v maxIdx]=max(inlierNum); +retPntsIdx=inlierIdx{maxIdx}; +retH=calculateH(pntsPrev(retPntsIdx,:), pntsCur(retPntsIdx,:)); + diff --git a/SD-VBS/benchmarks/stitch/src/matlab/readData.m b/SD-VBS/benchmarks/stitch/src/matlab/readData.m new file mode 100755 index 0000000..19643bb --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/readData.m @@ -0,0 +1,9 @@ +function retImgs=readData(imgName, dataDir) +fn=fullfile(dataDir, imgName, '*.jpg'); +filelist=dir(fn); + +retImgs=cell(length(filelist),1); +for i=1:length(filelist) + retImgs{i}=imread(fullfile(dataDir, imgName, filelist(i).name)); +end + diff --git a/SD-VBS/benchmarks/stitch/src/matlab/script_run_profile.m b/SD-VBS/benchmarks/stitch/src/matlab/script_run_profile.m new file mode 100755 index 0000000..f2ca68b --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/script_run_profile.m @@ -0,0 +1,38 @@ +function script_run_profile(dataDir, resultDir, type, common, toolDir) + +path(path, common); +image_list = 1; + +elapsed = zeros(1,2); +for i=1:image_list + Icur = readImage([dataDir, '/', num2str(i) ,'.bmp']); + rows = size(Icur,1); + cols = size(Icur,2); + fprintf(1,'Input size\t\t- (%dx%d)\n', rows, cols); + + %% Self check params + tol = 1; + + %% Timing + start = photonStartTiming; + + [x y v]=harris(Icur,dataDir); + interestPntsCur=getANMS(x, y, v, 24, dataDir); + Fcur=extractFeatures(Icur, interestPntsCur(:,1), interestPntsCur(:,2), dataDir); + + %% Timing + stop = photonEndTiming; + + temp = photonReportTiming(start, stop); + elapsed(1) = elapsed(1) + temp(1); + elapsed(2) = elapsed(2) + temp(2); + + +end + +%% Self checking +fWriteMatrix(Fcur, dataDir); + +%% Timing +photonPrintTiming(elapsed); + diff --git a/SD-VBS/benchmarks/stitch/src/matlab/showInterestPoints.m b/SD-VBS/benchmarks/stitch/src/matlab/showInterestPoints.m new file mode 100755 index 0000000..487cae4 --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/showInterestPoints.m @@ -0,0 +1,7 @@ +function showInterestPoints(im, x, y) +% show 'em +imagesc(im); +colormap(gray); +hold on; +plot(x,y,'r.'); +hold off; \ No newline at end of file diff --git a/SD-VBS/benchmarks/stitch/src/matlab/transformH.m b/SD-VBS/benchmarks/stitch/src/matlab/transformH.m new file mode 100755 index 0000000..7a22eb8 --- /dev/null +++ b/SD-VBS/benchmarks/stitch/src/matlab/transformH.m @@ -0,0 +1,13 @@ +function retPnts=transformH(H, pnts) +MAX_LIMIT=10000000; +[n temp]=size(pnts); + +pntsT = pnts'; +retPnts=(H*pntsT)'; +for i=1:n + if retPnts(i,3)~=0 + retPnts(i,:)=retPnts(i,:)./retPnts(i,3); + else + retPnts(i,:)=[MAX_LIMIT MAX_LIMIT 1]; + end +end -- cgit v1.2.2