迭代阈值图像分割matlab,Matlab 图像分割 (阈值处理)

使用移动平均的可变阈值处理,对很多对比度差的图像处理效果可以很好,但相应的程序代码的编写却很羞涩,难懂。

function g = movingthresh(f, n, K)

%MOVINGTHRESH Image segmentation using a moving average threshold.

% G = MOVINGTHRESH(F, n, K) segments image F by thresholding its

% intensities based on the moving average of the intensities along

% individual rows of the image. The average at pixel k is formed

% by averaging the intensities of that pixel and its n − 1

% preceding neighbors. To reduce shading bias, the scanning is

% done in a zig-zag manner, treating the pixels as if they were a

% 1-D, continuous stream. If the value of the image at a point

% exceeds K percent of the value of the running average at that

% point, a 1 is output in that location in G. Otherwise a 0 is

% output. At the end of the procedure, G is thus the thresholded

% (segmented) image. K must be a scalar in the range [0, 1].

% Preliminaries.

% || means or operator;rem(n,1) means the residual of n/1

f = tofloat(f);

[M, N] = size(f);

if (n < 1) || (rem(n, 1) ~= 0)

error('n must be an integer >= 1.')

end

if K < 0 || K > 1

error('K must be a fraction in the range [0, 1].')

end

% Flip every other row of f to produce the equivalent of a zig-zag scanning pattern. Convert image to a vector.

f(2:2:end, :) = fliplr(f(2:2:end, :)); %fliplr realize the overturn of the vector

f = f'; % Still a matrix.

f = f(:)'; % Convert to row vector for use in function filter.

% Compute the moving average.

maf = ones(1, n)/n; % The 1-D moving average filter.

ma = filter(maf, 1, f); % Computation of moving average.

% Perform thresholding.

g = (f > K * ma);

% Go back to image format (indexed subscripts).

g = reshape(g, N, M)';

% Flip alternate(交替的) rows back.

g(2:2:end, :) = fliplr(g(2:2:end, :));

如果有人看懂,麻烦讲解一下,有利于大家讨论学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值