project a homography transferred image to another image

6 views (last 30 days)
So I have been working on this for a while, I need to detect an ARtag in a video, read the tag, calculate the homography matrix, apply the Homography matrix to an undistorted image (which I was able to do all this), now I need to project the transformed image on the original image to replace the ARTag, that's what I need help with. will attach my code and the pictures
if true
% code
clc
clear all
clear all figures
L = imread('Lena.png');
s0= vid('Tag0.mp4');
s1= vid('Tag1.mp4');
s2= vid('Tag2.mp4');
T0 = (s0(446).cdata);
T1 = (s1(325).cdata);
T2 = (s2(495).cdata);
[cor0, BW0] = detect(T0);
[cor1, BW1] = detect(T1);
[cor2, BW2] = detect(T2);
CRP0 = im(BW0);
CRP1 = im(BW1);
CRP2 = im(BW2);
A0 = SS(CRP0);
A1 = SS(CRP1);
A2 = SS(CRP2);
Ref = imread('ref_marker.png');
BW_Ref = im2bw(Ref,0.8);
NUM0 = CC(A0)
NUM1 = CC(A1)
NUM2 = CC(A2)
pin = [512,1;512,512;1,512;1,1]
H0=homography_solve(pin',cor0');
H1=homography_solve(pin',cor1');
H2=homography_solve(pin',cor2');
corner0=CM(BW0,cor0);
corner1=CM(BW1,cor1);
corner2=CM(BW2,cor2);
TL0=TLena(H0,L);
TL1=TLena(H1,L);
TL2=TLena(H2,L);
function [cor,BW] = detect(image)
BW = im2bw(image,0.9);%processed image
ss = regionprops(~BW,'Extrema');
corn= zeros(4,2);
for n=1:2
for m=1:2
for l=1:8
corn(l,m) = ss(n).Extrema(l,m);%contains extrema points of multiple objects
end
end
end
cor=[0 0; 0 0; 0 0; 0 0];
cor(1,:)=floor((corn(1,:)+corn(2,:))/2);
cor(2,:)=floor((corn(3,:)+corn(4,:))/2);
cor(3,:)=floor((corn(5,:)+corn(6,:))/2);
cor(4,:)=floor((corn(7,:)+corn(8,:))/2);
end
function s = vid(video)
vidObj = VideoReader(video);
vidHeight = vidObj.Height;
vidWidth = vidObj.Width;
s = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'));
k = 1;
while hasFrame(vidObj)
s(k).cdata = readFrame(vidObj);
k = k+1;
end
end
function [CROP] = im(image)
% n= [-20 -20 40 40];
BB = regionprops(~image,'BoundingBox');
CROP = imcrop (image, [BB(2).BoundingBox]);
end
function [ARRAY] = SS(image)
[R,C] = size(image);
L=floor(R/8);
W=floor(C/8);
for m = 1:8
for n = 1:8
ARRAY(m,n) = image(round(L/2+(m-1)*L),round(W/2+(n-1)*W));
end
end
end
function [NUM] = CC(array)
D = array;
if D(4,5)==1&&D(5,4)==1
NUM =0;
else
if D(4,5)==0
NUM = 2;
else
NUM =1;
end
end
end
function [crn_mrk] = CM (image,pos)
dbl=im2double(image);
crn_mrk = insertMarker(dbl,pos,'plus','color','red','size',10);
figure, imshow(crn_mrk)
end
function [transformed_Lena] = TLena(H,Image)
tform = projective2d(H');
transformed_Lena = imwarp(Image,tform);
figure, imshow(transformed_Lena)
end
end
so to summarize, I need to project transformedLena on bwframe to replace the artag

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!