画出直线,找出直线经过的图像坐标

https://www.cnblogs.com/ImageVision/archive/2012/11/17/2775196.html

 

% 画出直线,找出直线经过的图像坐标
x1 = 10;
x2 = 50;
y1 = 15;
y2 = 40;
figure;
hold on
plot([x1 x2],[y1 y2])
hold off;
k = (y2 - y1) / (x2 - x1);
b = y1 - k *x1;
Num = 0;
for j = x1 : x2
            Hb = y1 - 1;                            %以A点为起点
            He = y1 + 1;
            H = 0;
            W = 0;
            Min = 1000;        
            for i = Hb : He
                Tmpb = i - k*j;
                delta = abs(b - Tmpb);
                if delta < Min
                    Min = delta;
                    H = i;
                    W = j;
                end
            end
            if H ~= 0 && W ~= 0
                Num = Num + 1;                  %直线上像素点个数
                Coor(Num,1) = H;                %直线上像素点位置坐标
                Coor(Num,2) = W;
                y1 = H;
            end                
end


hold on;
x = Coor(:,2);
y = Coor(:,1);
plot(x,y)
hold off;
set(gca,'xtick',0:1:100)
set(gca,'ytick',0:1:100)
grid on
 

 

 

posted on 2020-08-04 16:40  Maddock  阅读(501)  评论(0编辑  收藏  举报

导航