求matlab代码 运用链码计算不规则图形的周长以及面积

求matlab代码 运用链码计算不规则图形的周长以及面积,第1张

function CIR = C_CIR(Img)

%% 函数功能:计算输入的灰度图像的(或二值图像)的周长

% 函数输入:二值图像

% 函数输入:周长

%% 测试链码求周长CIR

I = im2bw(Img)

[m, n] = size(I)

figure(1)imshow(I)title('原图')

R = 1 % 二值图像的背景像素

I0 = zeros(m+2, n+2) + R% 原图像+边框,避免边界查找出错

I0(2:(m+1), 2:(n+1)) = I

x0 = 0

y0 = 0

fsp = 0% 是否找到起始点

%%%%%%%%%%%%%%%%%%%% 寻找目标边界的一个边界点 %%%%%%%%%%%%%%%%

for i = 1:m

for j = 1:n

if (I0(i,j) ~= R)

x0 = i% 记录起始点的纵坐标

y0 = j% 记录起始点的横坐标

fsp = 1

break

end

end

if fsp == 1

break

end

end

row = x0

col = y0

%%%%%%%%%%%%%%%%%% 按某一方向查找边界并且记录 %%%%%%%%%%%%%%%%%%%%%%%%

A = []% 存储边界点坐标的矩阵

B = zeros(m,n)% 边界跟踪的图像矩阵

ended = 0

direction = 4 % 从起始点的左边开始查找

while (ended == 0)

found_next = 0

b = [row-1, col-1]% 消除边框,与原图保持一致

A = [Ab]

B(row-1, col-1) = 255

while (found_next == 0)

switch mod(direction,8)

case 0

if (I0(row, col + 1)~= R)

row = row% 记录当前坐标

col = col + 1

direction = 5 % 下一点从方向5开始查找

found_next = 1% 找到边界点,准备跳出循环

end

case 1

if (I0(row - 1, col + 1) ~= R)

row = row - 1

col = col + 1

direction = 6

found_next = 1

end

case 2

if (I0(row - 1, col) ~= R)

row = row - 1

col = col

direction = 7

found_next = 1

end

case 3

if (I0(row - 1, col - 1) ~= R)

row = row - 1

col = col - 1

direction = 0

found_next = 1

end

case 4

if (I0(row, col - 1) ~= R)

row = row

col = col - 1

direction = 1

found_next = 1

end

case 5

if (I0(row + 1, col - 1) ~= R)

row = row + 1

col = col - 1

direction = 2

found_next = 1

end

case 6

if (I0(row + 1, col) ~= R)

row = row + 1

col = col

direction = 3

found_next = 1

end

case 7

if (I0(row + 1, col + 1) ~= R)

row = row + 1

col = col + 1

direction = 4

found_next = 1

end

end

if (found_next == 0)

direction = direction + 1

end

end

if (and((x0 == row), (y0 == col)))

ended = 1

end

end

A1 = [A[x0-1,y0-1]]

figure(2)imshow(B)% 输出跟踪结果

hold on

%%%%%%%%%%%%%%%%%%%%%%%%%%% 求8方向链码 %%%%%%%%%%%%%%%%%%%%%%

C = []

for i = 1:length(A1) - 1

if (A1(i+1, 1) - A1(i, 1) == 0) && ( A1(i+1, 2) - A1(i, 2) == 1)

code = 0

C = [C, code]

end

if (A1(i+1, 1) - A1(i, 1) == -1) &&( A1(i+1, 2) - A1(i, 2) == 1)

code = 1

C = [C, code]

end

if (A1(i+1, 1) - A1(i, 1) == -1) && (A1(i+1, 2) - A1(i,2) == 0)

code = 2

C = [C, code]

end

if (A1(i+1, 1) - A1(i, 1) == -1) &&(A1(i+1, 2) - A1(i, 2) == -1)

code = 3

C = [C, code]

end

if (A1(i+1, 1) - A1(i,1) == 0) && (A1(i+1, 2) - A1(i,2) == -1)

code = 4

C = [C, code]

end

if (A1(i+1, 1) - A1(i, 1) == 1) &&(A1(i+1, 2) - A1(i, 2) == -1)

code = 5

C = [C, code]

end

if (A1(i+1, 1) - A1(i, 1) == 1) && (A1(i+1, 2) - A1(i, 2) == 0)

code = 6

C = [C, code]

end

if (A1(i+1, 1) - A1(i, 1) == 1) &&(A1(i+1, 2) - A1(i, 2) == 1)

code = 7

C = [C, code]

end

end

%%%%%%%%%%%%%%%%%%%%%% 计算周长CIR %%%%%%%%%%%%%%%%%%%%%%%%%%%

sum1 = 0

sum2 = 0

for i = 1:length(C)

if mod(C(1,i), 2) == 0

sum1 = sum1 + 1

end

if mod(C(1,i), 2) == 1

sum2 = sum2 + 1

end

end

CIR = sum1 + sum2 * sqrt(2)% 计算边界周长

end

%%bound2im为自定义函数,需放到MATLAB当前路径中一起运行

I = imread('peppers.png')

red = 2*I(:,:,1)-I(:,:,2)-I(:,:,3)%超红色模型

t = graythresh(red)%OUTS自适应阈值

red_bw = im2bw(red,t)%二值化

%%%腐蚀

se = strel('disk',10)

red_imerode = imerode(red_bw,se)

%%%膨胀

se = strel('disk',10)

red_dilate = imdilate(red_bw,se)

%%%第一种提取方法,不需要那个附加的自定义函数%%%

g = bwperim(red_dilate,4)

%%%%%第二种提取方法%%%%

%b = bwboundaries(red_dilate,'noholes',‘conn’,4)%轮廓提取

%%%找出最长边界

%d = cellfun('length',b)

%[maxd,k] = max(d)

%b = b{k}

%%%

%[m,n] = size(I)%获取原图像大小,用于确定边界图像大小

g = bound2im(b,m,n)%产生边界图像


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12142307.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存