哼º 2022-06-28 22:58 采纳率: 78.6%
浏览 32
已结题

matlab透视校正后,如何提取到需要的点坐标

我在使用matlab对灰度图进行透视校正后,如何提取到校正后参照物四个点的坐标呢

图像左下角有个现实中10*10cm的标尺,拍照后需要进行校正和尺度变换
在网上找到了别人写的透视校正函数,但是在使用时没有看懂

函数如下:

function perspective_mat = getPerspective(moving_points,fixed_points)
% GETPERSPECTIVE 根据点获取透视变换矩阵
% 输入:
%     moving_points:n*2点集坐标(x,y)
%     fixed_points:n*2点集坐标(x,y),点顺序要对应moving_points
% 输出:
%     perspective_mat:3*3透视变换矩阵
% 
%  perspective_mat矩阵形式为[a11,a12,a13; a21,a22,a23; a31,a32,1];
%   满足fixed_points = perspective_mat*moving_points
% author: cuixing158@foxmail.com
%

assert(size(moving_points,1) == size(fixed_points,1)&& ...
size(moving_points,1)>=4);

nums = size(moving_points,1);
coefficient_mat = zeros(2*nums,8);
b = zeros(2*nums,1);
for i = 1:nums
    currentPoint = moving_points(i,:);
    currentFixedPoint = fixed_points(i,:);
    coefficient_mat(2*i-1,:) = [currentPoint(1),currentPoint(2),1,...
        0,0,0,...
        -currentPoint(1)*currentFixedPoint(1),-currentFixedPoint(1)*currentPoint(2)];
    b(2*i-1) = currentFixedPoint(1);
    
    coefficient_mat(2*i,:) = [0,0,0,...
        currentPoint(1),currentPoint(2),1,...
        -currentPoint(1)*currentFixedPoint(2),-currentPoint(2)*currentFixedPoint(2)];
    b(2*i) = currentFixedPoint(2);
end
perspective_mat = coefficient_mat\b; % 大于4个点时候为最小二乘法计算
perspective_mat = reshape([perspective_mat;1],3,3)';

在使用这个函数之前,通过彩色图像的颜色分割和角点检测找到了原图左下角标尺的位置Q(2*4矩阵)分别是四个点,但是透视之后还需要这四个点进行尺度变换,由于已经变成了灰度图,没办法通过颜色分割,想请教下如何从上面的函数中找到之前的标尺角点并且单独拿出来使用呢

问题补充:
函数应用的完整代码如下:

%img = rgb2gray(I);
img=I;
width = size(img,2);
height = size(img,1);
figure;imshow(img)
hold on
% moving_points = [0,0;
%                 100,50;
%                 0,50;
%                 50,100];
plot(Q(:,1),Q(:,2),'ro');title('角点定位');
fixed_points = [0,0;
    100,0;
    0,200;
    100,200];
 
%% method 2, get perspective matrix
perspective_mat = getPerspective(Q,fixed_points);  %Q是透视变换时参考的左下角正方形标尺的四个角
A = perspective_mat;
X = [1;width;1;width]; % 原图片的四个角点x坐标
Y = [1;1;height;height]; % 原图片的四个角点y坐标
 
moving_points_mat = [X(:)';Y(:)';ones(1,size(X(:),1))];
dst_points = A*moving_points_mat;
for i = 1:size(dst_points,2)
    dst_points(1:2,i) = dst_points(1:2,i)/dst_points(3,i);
end
figure;
plot(dst_points(1,:),dst_points(2,:),'bo');title('原图像仿射变换后的坐标范围')
grid on;
 
%% 仿射变换后图像逐像素进行插值赋值
min_x = min(dst_points(1,:));
max_x = max(dst_points(1,:));
min_y = min(dst_points(2,:));
max_y = max(dst_points(2,:));
W = round(max_x - min_x);
H = round(max_y -min_y);
wrapImg = 255*ones(H,W);
tic;
for i = 1:H
    for j = 1:W
        x = min_x+j; % 使得x,y的范围在原坐标范围内
        y = min_y+i;
        moving_point = A\[x;y;1];
        temp_point = [moving_point(1);moving_point(2)]./moving_point(3);
        if temp_point(1)>=1&&temp_point(1)<width&& ...
                temp_point(2)>=1&&temp_point(2)<height
            wrapImg(i,j) = img(round(temp_point(2)),round(temp_point(1)));
        end
    end
end
t_manual = toc;
figure;
imshow(uint8(wrapImg));title(['图像仿射变换后(手写函数),耗时(s):',num2str(t_manual)])
  • 写回答

2条回答 默认 最新

  • 哼º 2022-08-04 12:57
    关注

    解决了,matlab有自带的几何校正函数

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月12日
  • 已采纳回答 8月4日
  • 修改了问题 6月29日
  • 创建了问题 6月28日

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!