问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
clear ;
clc
%读取图像
% I=imread('huan.jpg');
[filename,pathname]=uigetfile({'.jpg';'.png';'.bmp';'.*'});
if isequal(filename,0)||isequal(pathname,0)
errordlg('没有选中文件','错误');
else
file=strcat(pathname,filename);
end
im=imread(file);
I1=im;
%最大类间方差法二值化
level=graythresh(I1);
I2=double(imbinarize(I1,level));
figure(1),
subplot(131),imshow(im,[]);
title('原图像');
subplot(132),imshow(I1,[]);
title('灰度图像');
subplot(133),imshow(I2,[]);
title('二值图像');
%边缘检测
level1=graythresh(double(I2));
a1=double(edge(I2,'canny',level));
figure(2),
imshow(a1,[]);
title('canny算子边缘检测');
%形态学滤波
se=strel('disk',3);
%腐蚀
s=imdilate(a1,se); %腐蚀的作用是消除物体边界点,使目标缩小,可以消除小于结构元素的噪声点
%膨胀
% w=double(w);
t=double(imerode(s,se));
figure(3),
imshow(t,[]);
[r1,c1]=find(t==1);
% %裁剪
% p =imcrop(t,[1,1,abs(211),abs(388)]);
% [r1,c1]=find(p==1);
% num1=length(r1);
%
% w=imcrop(t,[240,24,abs(218),abs(388)]);
% [r2,c2]=find(w==1);
% num2=length(r2);
%
% z=imcrop(t,[450,17,abs(173),abs(388)]);
% [r3,c3]=find(z==1);
% num3=length(r3);
% %最小二乘法
[R1,A1,B1]=circ(r1,c1);
% [R2,A2,B2]=circ(r2,c2);
% [R3,A3,B3]=circ(r3,c3);
theta=0:pi/100:2pi;
x1=A1+R1cos(theta);
y1=B1+R1*sin(theta);
% x2=A2+R2cos(theta);
% y2=B2+R2sin(theta);
%
% x3=A3+R3cos(theta);
% y3=B3+R3sin(theta);
figure(4);
subplot(121),imshow(t,[]);
subplot(122),imshow(t,[]);
hold on;
plot(y1,x1,'r','linewidth',1.5);
axis equal
hold off;
运行结果及报错内容
错误使用 edge
第 1 个输入, I, 应为 二维。
出错 edge>parse_inputs (第 482 行)
validateattributes(I,{'numeric','logical'},{'real','nonsparse','2d'},mfilename,'I',1);
出错 edge (第 213 行)
[a,method,thresh,sigma,thinning,H,kx,ky] = parse_inputs(args{:});
出错 test (第 31 行)
a1=double(edge(I2,'canny',level1));