对多边形边缘检测并拟合线段长度和角度(matlab实现)
作者:互联网
这是瑶瑶写的
clear all;close all;clc;
%边缘检测
I = imread('5.jpg');
figure;
subplot(1,2,1);
imshow(I);
I1=im2bw(I,0.72);%二值化
subplot(1,2,2);
imshow(edge(I1, 'sobel'));
I2=edge(I1, 'sobel');
%边缘检测
f1=imread('5.jpg'); %读入要提取的图片
figure;subplot(1,3,1);imshow(f1);
f2=im2bw(f1,0.72);%二值化
f3=im2uint8(f2);
subplot(1,3,2);imshow(f3);
BW=edge (f3,'canny',[0,0.7],0.9);%提取边缘
subplot(1,3,3);imshow(BW);
%角点检测
%MatLab角点检测程序harris。
ori_im2=rgb2gray(imread('5.jpg'));
%ori_im2=imresize(ori_im2',0.50,'bicubic'); %加上这句图就变成竖着的了
fx = [5 0 -5;8 0 -8;5 0 -5]; % % la gaucienne,ver axe x
Ix = filter2(fx,ori_im2); % la convolution vers axe x
fy = [5 8 5;0 0 0;-5 -8 -5]; % la gaucienne,ver axe y
Iy = filter2(fy,ori_im2); % la convolution vers axe y
Ix2 = Ix.^2;
Iy2 = Iy.^2;
Ixy = Ix.*Iy;
clear Ix;
clear Iy;
h= fspecial('gaussian',[5 5],0.7); % générer une fonction gaussienne,sigma=0.7
Ix2 = filter2(h,Ix2);
Iy2 = filter2(h,Iy2);
Ixy = filter2(h,Ixy);
height = size(ori_im2,1);
width = size(ori_im2,2);
result = zeros(height,width); % enregistrer la position du coin
R = zeros(height,width);
K=0.04;
Rmax = 0; % chercher la valeur maximale de R
for i = 1:height
for j = 1:width
M = [Ix2(i,j) Ixy(i,j);Ixy(i,j) Iy2(i,j)];
R(i,j) = det(M)-K*(trace(M))^2; % % calcule R
if R(i,j) > Rmax
Rmax = R(i,j);
end;
end;
end;
cnt = 0;
for i = 2:height-1
for j = 2:width-1
% réduire des valuers minimales ,la taille de fenetre 3*3
if R(i,j) > 0.01*Rmax && R(i,j) > R(i-1,j-1) && R(i,j) > R(i-1,j) && R(i,j) > R(i-1,j+1) && R(i,j) > R(i,j-1) && R(i,j) > R(i,j+1) && R(i,j) > R(i+1,j-1) && R(i,j) > R(i+1,j) && R(i,j) > R(i+1,j+1)
result(i,j) = 1;
cnt = cnt+1;
end;
end;
end;
[posr2, posc2] = find(result == 1);
cnt % compter des coins
figure
imshow(ori_im2);
hold on;
plot(posc2(1),posr2(1),'r*');
plot(posc2(2),posr2(2),'g*');
plot(posc2(3),posr2(3),'b*');
plot(posc2(4),posr2(4),'y*');
%set(gcf,'color','black');
disp('[posr2, posc2]');disp([posr2, posc2]);
dis1 = sqrt((posc2(1)-posc2(2))^2 + (posr2(1)-posr2(2))^2);%红点和绿点距离
dis2 = sqrt((posc2(2)-posc2(4))^2 + (posr2(2)-posr2(4))^2);%黄点和绿点距离
dis3 = sqrt((posc2(3)-posc2(4))^2 + (posr2(3)-posr2(4))^2);%黄点和蓝点距离
dis4 = sqrt((posc2(1)-posc2(3))^2 + (posr2(1)-posr2(3))^2);%红点和蓝点距离
dis = [dis1,dis2,dis3,dis4];
%dot([x1-x2,y1-y2],[x3-x2,y3-y2])为计算内积,norm([x1-x2,y1-y2])为计算向量长度,acosd为计算以度为单位的夹角信息
theta1 = acosd(dot([posc2(1)-posc2(2),posr2(1)-posr2(2)],[posc2(4)-posc2(2),posr2(4)-posr2(2)])/(norm([posc2(1)-posc2(2),posr2(1)-posr2(2)])*norm([posc2(4)-posc2(2),posr2(4)-posr2(2)])));%绿点处
theta2 = acosd(dot([posc2(3)-posc2(4),posr2(3)-posr2(4)],[posc2(2)-posc2(4),posr2(2)-posr2(4)])/(norm([posc2(3)-posc2(4),posr2(3)-posr2(4)])*norm([posc2(2)-posc2(4),posr2(2)-posr2(4)])));%黄点处
theta3 = acosd(dot([posc2(1)-posc2(3),posr2(1)-posr2(3)],[posc2(4)-posc2(3),posr2(4)-posr2(3)])/(norm([posc2(1)-posc2(3),posr2(1)-posr2(3)])*norm([posc2(4)-posc2(3),posr2(4)-posr2(3)])));%蓝点处
theta4 = acosd(dot([posc2(2)-posc2(1),posr2(2)-posr2(1)],[posc2(3)-posc2(1),posr2(3)-posr2(1)])/(norm([posc2(2)-posc2(1),posr2(2)-posr2(1)])*norm([posc2(3)-posc2(1),posr2(3)-posr2(1)])));%红点处
theta = [theta1,theta2,theta3,theta4];
disp('dis=');disp(dis);
disp('theta=');disp(theta);
引用:
MatLab角点检测(harris经典程序)_浴血重生-学习空间-CSDN博客_harris角点检测matlab
标签:多边形,ori,posc2,im2,matlab,&&,拟合,norm,posr2 来源: https://blog.csdn.net/weixin_48262500/article/details/122201341