使用移动平均的图像阈值处理

来源:互联网 发布:网店淘宝拍拍 编辑:程序博客网 时间:2024/06/09 23:21

使用移动平均的图像阈值处理


1、代码

clc;clear all;close all;f = imread('c.tif');figure;imshow(f);title('原图像');T = graythresh(f);g1 = im2bw(f, T); g2 = movingthresh(f, 20, 0.5);figure, imshow(g2);title('变换后');

函数movingthresh定义为:

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.f = tofloat(f);[M, N] = size(f);if (n < 1) || (rem(n, 1) ~= 0)error('n must be an integer >= 1.')endif K < 0 || K > 1error('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, :));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, :));

2、结果


0 1
原创粉丝点击