📄 derterd.m
字号:
RGB = imread('tape.png'); %读取图片,数据存在RGB变量中;
I = rgb2gray(RGB); %将RGB转换为灰度图像数据;
threshold = graythresh(I); %获取灰度图像转换时的阈值;
BW = im2bw(I,threshold); %将图像转换位2进制数据
dim = size(BW);
col = round(dim(2)/2)-90;
row = min(find(BW(:,col)));
connectivity = 8;
num_points = 180;
contour = bwtraceboundary(BW, [row, col], 'N', connectivity, num_points);
imshow(RGB);
hold on;
plot(contour(:,2),contour(:,1),'g','LineWidth',2);
x = contour(:,2);
y = contour(:,1);
abc=[x y ones(length(x),1)]\[-(x.^2+y.^2)];
a = abc(1); b = abc(2); c = abc(3);
xc = -a/2;
yc = -b/2;
radius = sqrt((xc^2+yc^2)-c)
plot(xc,yc,'yx','LineWidth',2);
theta = 0:0.01:2*pi;
Xfit = radius*cos(theta) + xc;
Yfit = radius*sin(theta) + yc;
plot(Xfit, Yfit);
message = sprintf('通过计算得到的半径是 %2.3f pixels', radius);
text(15,15,message,'Color','y','FontWeight','bold');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -