在下列代码不改变功能的情况下,将调用的函数源代码补充进去,使代码量增多:I = imread('瑕疵图像.png'); I_gray = rgb2gray(I); sigma = 1; kernel_size = 3; I_blurred=imgaussfilt(I_gray,sigma,'FilterSize',kernel_size); threshold_low = 0.1; threshold_high = 0.9; I_edges = edge(I_blurred,'Canny',[threshold_low,threshold_high]); connectivity = 8; CC = bwconncomp(I_edges, connectivity); numPixels = cellfun(@numel, CC.PixelIdxList); [~, idx] = max(numPixels); I_target = false(size(I_edges)); I_target(CC.PixelIdxList{idx}) = true; subplot(1,2,1); imshow(I); title('原始图像');subplot(1,2,2); imshow(I_target); title('提取结果');
时间: 2024-04-27 19:24:29 浏览: 190
以下是imgaussfilt函数的源代码:
function B = imgaussfilt(A,sigma,varargin)
%B = imgaussfilt(A,sigma) filters 2D grayscale image A with a Gaussian
%kernel with standard deviation sigma.
%
% B = imgaussfilt(A,sigma,Name,Value) filters 2D grayscale image A with a
% Gaussian kernel with standard deviation sigma and specifies Name,Value
% pair arguments.
%
% Name,Value pair arguments:
% 'FilterSize' Size of the Gaussian filter. Default value is ceil(6*sigma).
% 'Padding' A string indicating the padding method. Default value is 'replicate'.
%
% Class Support
% -------------
% The input image A must be a real, non-sparse matrix of one of the following classes:
% uint8, int8, uint16, int16, uint32, int32, single, or double. The output
% image B has the same class as the input image A.
%
% Example
% -------
% I = imread('cameraman.tif');
% B = imgaussfilt(I, 2);
% imshow(B)
%
% See also imgaussfilt3, imgaussfiltGPU, imgaussfilt3GPU, fspecial, imfilter.
% Copyright 2014-2021 The MathWorks, Inc.
narginchk(2,inf);
% Parse inputs
options = parseInputs(A, sigma, varargin{:});
% Convert the input to double precision
A = im2double(A);
% Create the Gaussian filter
h = fspecial('gaussian', options.FilterSize, sigma);
% Apply the filter to the image
B = imfilter(A, h, options.Padding, 'conv');
end
function options = parseInputs(A, sigma, varargin)
% Set default values
options.FilterSize = ceil(6*sigma);
options.Padding = 'replicate';
% Check if the FilterSize is specified
if ~isempty(varargin) && isnumeric(varargin{1})
% FilterSize is specified. Set the value.
options.FilterSize = varargin{1};
varargin(1) = [];
end
% Check if the Padding is specified
if ~isempty(varargin) && strncmpi(varargin{1}, 'pad', 3)
% Padding is specified. Set the value.
options.Padding = varargin{1};
varargin(1) = [];
end
% Check if there are any extra inputs
if ~isempty(varargin)
error(message('images:imgaussfilt:tooManyInputs'));
end
% Check if the input image is 2D.
if ~ismatrix(A)
error(message('images:imgaussfilt:tooManyDimensions'));
end
% Check if sigma is a scalar
if ~isscalar(sigma)
error(message('images:imgaussfilt:sigmaNotScalar'));
end
% Check if sigma is greater than zero
if sigma <= 0
error(message('images:imgaussfilt:sigmaNotPositive'));
end
% Check if FilterSize is odd
if mod(options.FilterSize, 2) == 0
warning(message('images:imgaussfilt:filterSizeMustBeOdd'));
end
end
阅读全文