open source pkg v1

This commit is contained in:
Vijay Yadev
2020-08-04 19:12:31 -04:00
parent bef213dba9
commit c389fc2c47
3708 changed files with 1624220 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
function [ coeffs ] = CalculateCoefficients( alphas, betas, triangulation, destination )
%CALCULATECOEFFICIENTS Summary of this function goes here
% Detailed explanation goes here
xs = destination(:,1);
ys = destination(:,2);
triangulation = triangulation + 1;
numTri = size(triangulation, 1);
coeffs = zeros(numTri, 3);
for l=1:numTri
i = triangulation(l,1);
j = triangulation(l,2);
k = triangulation(l,3);
c1 = xs(i);
c2 = xs(j) - c1;
c3 = xs(k) - c1;
c4 = ys(i);
c5 = ys(j) - c4;
c6 = ys(k) - c4;
coeffs(l,1) = c1 + c2*alphas(l,1) + c3*betas(l,1);
coeffs(l,2) = c2*alphas(l,2) + c3*betas(l,2);
coeffs(l,3) = c2*alphas(l,3) + c3*betas(l,3);
coeffs(l,4) = c4 + c5*alphas(l,1) + c6*betas(l,1);
coeffs(l,5) = c5*alphas(l,2) + c6*betas(l,2);
coeffs(l,6) = c5*alphas(l,3) + c6*betas(l,3);
end
end

View File

@@ -0,0 +1,15 @@
function [warpedImage] = Crop(inputImage, destinationPoints, triangulation, triX, mask, alphas, betas, nPix, minX, minY)
coeffs = CalculateCoefficients(alphas, betas, triangulation, destinationPoints);
[ mapX, mapY ] = WarpRegion( minX, minY, mask, triX, coeffs );
if(size(inputImage,3) == 1)
warpedImage = Remap(inputImage, mapX, mapY);
else
warpedImage = [];
for i=1:size(inputImage,3)
warpedImage = cat(3, warpedImage, Remap(inputImage(:,:,i), mapX, mapY));
end
end
end

View File

@@ -0,0 +1,113 @@
function [ alphas, betas, triX, mask, xmin, ymin, npix ] = InitialisePieceWiseAffine( triangulation, sourcePoints )
%INITIALISEPIECEWICEAFFINE Summary of this function goes here
% Detailed explanation goes here
triangulation = triangulation + 1;
numPoints = size(sourcePoints, 1);
numTris = size(triangulation, 1);
alphas = zeros(size(triangulation, 1), 3);
betas = zeros(size(triangulation, 1), 3);
xs = sourcePoints(:,1);
ys = sourcePoints(:,2);
for i = 1:numTris
j = triangulation(i, 1);
k = triangulation(i, 2);
l = triangulation(i, 3);
c1 = ys(l) - ys(j);
c2 = xs(l) - xs(j);
c4 = ys(k) - ys(j);
c3 = xs(k) - xs(j);
c5 = c3*c1 - c2*c4;
alphas(i, 1) = (ys(j) * c2 - xs(j) * c1) / c5;
alphas(i, 2) = c1/c5;
alphas(i, 3) = -c2/c5;
betas(i, 1) = (xs(j) * c4 - ys(j) * c3)/c5;
betas(i, 2) = -c4/c5;
betas(i, 3) = c3/c5;
end
xmin = min(xs);
ymin = min(ys);
xmax = max(xs);
ymax = max(ys);
w = int32(xmax - xmin + 1);
h = int32(ymax - ymin + 1);
mask = zeros(h, w);
triX = zeros(h, w);
shape = [xs, ys];
for i = 1:h
for j = 1:w
currTri = findTriangle(double([double(j)-1+xmin, double(i)-1+ymin])', triangulation, shape);
if(currTri ~= -1)
triX(i, j) = currTri - 1;
mask(i, j) = 1;
else
triX(i, j) = -1;
end
end
end
npix = sum(sum(mask));
end
function [tri] = findTriangle(point, tris, controlPoints)
numTris = size(tris, 1);
tri = -1;
for i=1:numTris
if(PointInTriangle(point, controlPoints(tris(i,1),:)', controlPoints(tris(i,2),:)', controlPoints(tris(i,3),:)'))
tri = i;
break;
end
end
end
function inTriangle = PointInTriangle(point, v1, v2, v3)
inTriangle = SameSide(point, v1,v2,v3) && SameSide(point, v2,v1,v3) && SameSide(point, v3,v1,v2);
end
function sameSide = SameSide(toTest,v1,v2,v3)
x0 = toTest(1);
y0 = toTest(2);
x1 = v1(1);
x2 = v2(1);
x3 = v3(1);
y1 = v1(2);
y2 = v2(2);
y3 = v3(2);
x = (x3-x2)*(y0-y2) - (x0-x2)*(y3-y2);
y = (x3-x2)*(y1-y2) - (x1-x2)*(y3-y2);
if(x*y >= 0)
sameSide = 1;
else
sameSide = 0;
end
% cross1 = cross( v3 - v2, toTest - v2);
% cross2 = cross( v3 - v2, v1 - v2);
%
% sameSide = (cross1 * cross2') >= 0;
end

View File

@@ -0,0 +1,17 @@
function [ outputTexture ] = Remap( inputTexture, mapX, mapY )
%REMAP Summary of this function goes here
% Detailed explanation goes here
outputTexture = zeros(size(mapX));
[X,Y] = meshgrid(0:size(inputTexture,2)-1,0:size(inputTexture,1)-1);
inds = find(mapX ~= -1);
xSources = mapX(inds);
ySources = mapY(inds);
Z = interp2(X, Y, double(inputTexture), xSources, ySources);
outputTexture(inds) = Z;
end

View File

@@ -0,0 +1,49 @@
function [ mapX, mapY ] = WarpRegion( xmin, ymin, mask, triX, coeffs )
%WARPREGION Summary of this function goes here
% Detailed explanation goes here
%%
[h, w] = size(mask);
mapX = zeros(size(mask));
mapY = zeros(size(mask));
ys = [1:h]' * ones(1, w) + ymin - 1;
xs = ([1:w]' * ones(1, h))' + xmin - 1;
for t=0:size(coeffs,1)-1
trimap = triX == t;
a = coeffs(t+1,:);
xo = a(1) + a(2) * xs + a(3) * ys;
mapX(trimap) = xo(trimap);
yo = a(4) + a(5) * xs + a(6) * ys;
mapY(trimap) = yo(trimap);
end
mapX(~mask) = -1;
mapY(~mask) = -1;
%%
% [h, w] = size(mask);
% mapX_2 = zeros(size(mask));
% mapY_2 = zeros(size(mask));
%
% ys = [1:h]' * ones(1, w) + ymin - 1;
% xs = ([1:w]' * ones(1, h))' + xmin - 1;
%
% ys = ys(:);
% xs = xs(:);
%
% xos = coeffs(1,:) + bsxfun(@times, coeffs(2,:), xs) + bsxfun(@times, coeffs(3,:), ys);
% yos = coeffs(4,:) + bsxfun(@times, coeffs(5,:), xs) + bsxfun(@times, coeffs(6,:), ys);
%
% maps = repmat(trimap(:),1, size(coeffs,1));
% maps = repmat
end