300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 迭代阈值图像分割matlab Matlab 图像分割 (阈值处理)

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

时间:2021-07-24 17:11:43

相关推荐

迭代阈值图像分割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, :));

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。