matlab实现透视变换倾斜校正
说明:输⼊:四个点的坐标,输出:想要的矩形的⼤⼩。也就是对任意四边形都可以经过透视变换进⾏校正成你想要的矩形。(因为我处理的是视频,所以⾥⾯有循环进⾏校正的代码)
这是循环的处理视频的每⼀帧,对每⼀帧都进⾏相同的坐标校正,再保存。
下⾯是我处理之后的效果:
这是带有四个点的图⽚:code⾥⾯输⼊的就是这四个点的坐标(这⾥的坐标是指像素哦)
下图是经过透视变换坐标校正之后的图⽚
vedio_read.m⽂件
% 读取视频并输出图⽚的每⼀帧,进⾏坐标校正之后,再保存到⽂件夹⾥。
% fileName = 'step.avi';
fileName = 'suppleness.avi';
obj = VideoReader(fileName);
numFrames = obj.NumberOfFrames;% 帧的总数
for k = 1 :numFrames % 读取数据
frame = read(obj,k);
image_pro = Test_perspectivetransformation(frame);
imwrite(image_pro,strcat('image_supple\',num2str(k),'.jpg'),'jpg');% 保存帧
end
% image_pro = Test_perspectivetransformation('image_supple\1,jpg');
% imshow(image_pro);
%
m_PerspectiveTransformationTest.m⽂件
function Imgback = m_PerspectiveTransformationTest(imgIn,pointLT,pointRT,pointLB,pointRB,outHeitht,outWidth) [imgInHeight,imgInWidth,imgInDimension] = size(imgIn);
%为了中⼼投影变换,需要将4个点转化为三个向量,具体看参考⽂献
vector10 = pointLB - pointLT;
vector01 = pointRT - pointLT;
紫蔷薇vector11 = pointRB - pointLT;
%把vector11表⽰成vector10和vector01的线性组合,以使三个向量共⾯
A = [vector10' , vector01'];
B = vector11' ;
S = A\B;
a0 = S(1);
a1 = S(2);
%输出矩形
怎么写自我介绍Imgback = uint8(zeros(outHeitht,outWidth,imgInDimension));
%利⽤循环操作来对每个像素点赋值
for heightLoop = 1:outHeitht
for widthLoop = 1:outWidth
%以下算法为参考⽂献中的公式表⽰
x0 = heightLoop/outHeitht;
x1 = widthLoop/outWidth;
FenMu = a0+a1-1+(1-a1)*x0+(1-a0)*x1; %分母
y0 = a0*x0/FenMu;
y1 = a1*x1/FenMu;
%根据得到的参数找到对应的源图像中的坐标位置,并赋值
coordInOri = y0*vector10 + y1*vector01 + pointLT;
heightC = round(coordInOri(1));
widthC = round(coordInOri(2));
if (heightC > imgInHeight || heightC <= 0 || widthC >imgInWidth || widthC <=0 )
disp(['m_PerspectiveTransformation超出范围' num2str(heightC) num2str(widthC)]);
pau();
return;
end
for dimentionLoop = 1:imgInDimension
%使⽤最近邻域插值,使⽤⾼级插值⽅法效果会更好
Imgback(heightLoop,widthLoop,dimentionLoop) = imgIn(heightC,widthC,dimentionLoop);
end
end
end
% figure; imshow(Imgback); title('投影变换的结果');
function Imgback = m_PerspectiveTransformation(imgIn,pointLT,pointRT,pointLB,pointRB,outHeitht,outWidth)
[imgInHeight,imgInWidth,imgInDimension] = size(imgIn);
%为了中⼼投影变换,需要将4个点转化为三个向量,具体看参考⽂献
相对的反义词vector10 = pointLB - pointLT;
vector01 = pointRT - pointLT;
vector11 = pointRB - pointLT;
%把vector11表⽰成vector10和vector01的线性组合,以使三个向量共⾯
A = [vector10' , vector01'];
B = vector11' ;
S = A\B;
a0 = S(1);
a1 = S(2);
m_PerspectiveTransformation.m⽂件
%输出矩形
Imgback = uint8(zeros(outHeitht,outWidth,imgInDimension));
%利⽤循环操作来对每个像素点赋值
for heightLoop = 1:outHeitht
for widthLoop = 1:outWidth
%以下算法为参考⽂献中的公式表⽰
x0 = heightLoop/outHeitht;
x1 = widthLoop/outWidth;
石家庄环城水系FenMu = a0+a1-1+(1-a1)*x0+(1-a0)*x1; %分母
y0 = a0*x0/FenMu;
y1 = a1*x1/FenMu;
%根据得到的参数找到对应的源图像中的坐标位置,并赋值
炸鱿鱼须
coordInOri = y0*vector10 + y1*vector01 + pointLT;
heightC = round(coordInOri(1));
世界无烟日widthC = round(coordInOri(2));
if (heightC > imgInHeight || heightC <= 0 || widthC >imgInWidth || widthC <=0 )
转基因鱼disp(['m_PerspectiveTransformation超出范围' num2str(heightC) num2str(widthC)]);
pau();
return;
end
for dimentionLoop = 1:imgInDimension
%使⽤最近邻域插值,使⽤⾼级插值⽅法效果会更好
Imgback(heightLoop,widthLoop,dimentionLoop) = imgIn(heightC,widthC,dimentionLoop);
end
end
end
% figure; imshow(Imgback); title('投影变换的结果');
巴音布鲁克大草原这会输出校正过的视频的每⼀帧,下⾯附图⽚帧合并为视频的代码,其实是可以放在⼀起的,我懒得弄。⾃⼰合成⼀个整体就好了。
% 图⽚帧进⾏合成视频
myObj = VideoWriter('supple.avi');%⽬标视频路径及名称
open(myObj);
for i=1:351%逐帧加⼊视频中
fname=strcat('image_supple\',num2str(i),'.jpg');
frame = imread(fname);
writeVideo(myObj,frame);
end
clo(myObj);%关闭视频对象