我必须通过特征检测将像素从一个图像转换为另一个图像。我已经计算了投影变换矩阵。一个图像是基本图像,另一个是线性翻译图像。
现在我必须定义一个更大的网格,并将基础图像中的像素分配给它。例如,如果基本图像在(1,1)
处为20,则在较大的网格上,我将在(1,1)
处具有20。并将零分配给网格的所有未填充值。然后我必须将线性转换的图像映射到基本图像上,并根据“delaunay三角剖分”编写我自己的算法,以在图像之间进行插值。
我的问题是,当我将翻译后的图像映射到基本图像时,我会使用概念
(w,z)=inv(T).*(x,y)
A=inv(T).*B
其中(w,z)
是基本图片的坐标,(x,y)
是翻译图片的坐标,A
是包含坐标(w z 1)
和B
的矩阵,包含坐标(x y 1)
的矩阵。
如果我使用以下代码,我会得到新坐标,但如何将这些内容与图像联系起来?我的第二张图像的像素是否也转换为第一张图像?如果没有,我该怎么做?
close all; clc; clear all;
image1_gray=imread('C:\Users\Javeria Farooq\Desktop\project images\a.pgm');
figure; imshow(image1_gray); axis on; grid on;
title('Base image');
impixelinfo
hold on
image2_gray =imread('C:\Users\Javeria Farooq\Desktop\project images\j.pgm');
figure(2); imshow(image2_gray); axis on; grid on;
title('Unregistered image1');
impixelinfo
% Detect and extract features from both images
points_image1= detectSURFFeatures(image1_gray, 'NumScaleLevels', 100, 'NumOctaves', 5, 'MetricThreshold', 500 );
points_image2 = detectSURFFeatures(image2_gray, 'NumScaleLevels', 100, 'NumOctaves', 12, 'MetricThreshold', 500 );
[features_image1, validPoints_image1] = extractFeatures(image1_gray, points_image1);
[features_image2, validPoints_image2] = extractFeatures(image2_gray, points_image2);
% Match feature vectors
indexPairs = matchFeatures(features_image1, features_image2, 'Prenormalized', true) ;
% Get matching points
matched_pts1 = validPoints_image1(indexPairs(:, 1));
matched_pts2 = validPoints_image2(indexPairs(:, 2));
figure; showMatchedFeatures(image1_gray,image2_gray,matched_pts1,matched_pts2,'montage');
legend('matched points 1','matched points 2');
figure(5); showMatchedFeatures(image1_gray,image3_gray,matched_pts4,matched_pts3,'montage');
legend('matched points 1','matched points 3');
% Compute the transformation matrix using RANSAC
[tform, inlierFramePoints, inlierPanoPoints, status] = estimateGeometricTransform(matched_pts1, matched_pts2, 'projective')
figure(6); showMatchedFeatures(image1_gray,image2_gray,inlierPanoPoints,inlierFramePoints,'montage');
[m n] = size(image1_gray);
image1_gray = double(image1_gray);
[x1g,x2g]=meshgrid(m,n) % A MESH GRID OF 2X2
k=imread('C:\Users\Javeria Farooq\Desktop\project images\a.pgm');
ind = sub2ind( size(k),x1g,x2g);
%[tform1, inlierFramepPoints, inlierPanopPoints, status] = estimateGeometricTransform(matched_pts4, matched_pts3, 'projective')
%figure(7); showMatchedFeatures(image1_gray,image3_gray,inlierPanopPoints,inlierFramepPoints,'montage');
%invtform=invert(tform)
%x=invtform
%[xq,yq]=meshgrid(1:0.5:200.5,1:0.5:200.5);
r=[];
A=[];
k=1;
%i didnot know how to refer to variable tform so i wrote the transformation
%matrix from variable structure tform
T=[0.99814272,-0.0024304502,-1.2932052e-05;2.8876773e-05,0.99930143,1.6285858e-06;0.029063907,67.809265,1]
%lets take i=1:400 so my r=2 and resulting grid is 400x400
for i=1:200
for j=1:200
A=[A; i j 1];
z=A*T;
r=[r;z(k,1)/z(k,3),z(k,2)/z(k,3)];
k=k+1;
end
end
%i have transformed the coordinates but how to assign values??
%r(i,j)=c(i,j)
d1=[];
d2=[];
for l=1:40000
d1=[d1;A(l,1)];
d2=[d2;r(l,1)];
X=[d1 d2];
X=X(:);
end
c1=[];
c2=[];
for l=1:40000
c1=[c1;A(l,2)];
c2=[c2;r(l,2)];
Y=[c1 c2];
Y=Y(:);
end
%this delaunay triangulation is of vertices as far as i understand it
%doesnot have any pixel value of any image
DT=delaunayTriangulation(X,Y);
triplot(DT,X,Y);
答案 0 :(得分:1)
我通过以下两个步骤解决了这个问题:
使用transformPointsForward命令转换image的坐标,使用estimateGeometrcTransform返回的tform对象
在Matlab中使用scatteredInterpolant类并使用命令scatInterpolant 将变换后的坐标分配给它们各自的像素值。
F = scatterInterpolant(P,z)
这里P =包含所有变换坐标的nx2矩阵
z=nx1 matrix containing pixel values of image that is transformed,it is obtained by converting image to column vector using image=image(:)
最后,所有变换的坐标都与基本图像上的像素值一起出现,并且可以进行插值。
答案 1 :(得分:0)
你在这里做了太多的工作,我认为你根本不需要Delaunay三角测量。使用图像处理工具箱中的imwarp
功能转换图像。它采用原始图像和tform
返回的estimateGeometricTransform
对象。