% linear filter
I = imread('coins.png');
figure, imshow(I), title('Original image');
h = ones(5,5)/25; % create a normalized 5-by-5 pixel averaging filter
I2 = imfilter(I, h);
figure, imshow(I2), title('Filtered image');
% show the zero padding and border replication
I = imread('eight.tif');
figure, imshow(I), title('Original image');
h = ones(5,5)/25; % create a normalized 5-by-5 pixel averaging filter
I2 = imfilter(I, h);
figure, imshow(I2), title('Filtered image');
I3 = imfilter(I, h, 'replicate'); % also circular, symmetric
figure, imshow(I3), title('Filtered image with border replication');
% create filter
I = imread('moon.tif');
h = fspecial('unsharp');
I2 = imfilter(I, h, 'symmetric');
figure, imshow(I), title('Original image');
figure, imshow(I2), title('filtered image');
% filter color image
rgb = imread('peppers.png');
imshow(rgb);
h = ones(5,5)/25;
rgb2 = imfilter(rgb,h);
figure, imshow(rgb2);
% guided filter
A = imread('toysnoflash.png');
figure, imshow(A), title('Input image - camera flash off');
G = imread('toysflash.png');
figure, imshow(G), title('Guidance image - camera flash on');
nhoodSize = 3;
smoothValue = 0.001 * diff(getrangefromclass(G)).^2;
B = imguidedfilter(A, G, 'NeighborhoodSize', nhoodSize, 'DegreeOfSmoothing', smoothValue);
figure, imshow(B), title('Filtered Image');
% segment thermographic image after edge-preserving filtering
I = imread('hotcoffee.tif');
whos
range = [min(I(:)) max(I(:))];
figure, imshow(I, []), colormap(gca, hot), title('Original image')
smoothValue = 0.01 * diff(range).^2;
J = imguidedfilter(I, 'DegreeOfSmoothing', smoothValue);
figure, imshow(J, []), colormap(gca, hot), title('Guided filtered image')
thresh = multithresh(J, 2); % compute a 2-level threshold using otsu method
L = imquantize(J, thresh); % segment the image using two threshold
L = imfill(L); % FILL HOLES
figure, imshow(label2rgb(L)), title('Label matrix from 3-level otsu')
props = regionprops(L, I, {'Area', 'BoundingBox', 'MeanIntensity', 'Centroid'}); % get information about the regions
[~, idx] = max([props.Area]);
figure, imshow(I, []), colormap(gca, hot), title('Segmented regions with mean temperature')
for n = 1:numel(props)
if n ~= idx % if not the background
rectangle('Position', props(n).BoundingBox, 'EdgeColor', 'c') % draw bounding box around region
T = [num2str(props(n).MeanIntensity, 3) '\circ C'];
text(props(n).Centroid(1), props(n).Centroid(2), T, 'Color', 'c', 'FontSize', 12)
end
end
% reducing noise in image gradients
originalImage = imread('yellowlily.jpg');
originalImage = rgb2gray(originalImage);
% warning off images:initSize:adjustingMag
figure, imshow(originalImage);
noisyImage = imnoise(originalImage, 'gaussian');
figure, imshow(noisyImage);
sobelGradient = imgradient(noisyImage);
figure, imshow(sobelGradient, []), title('sobel gradient magnitude')
sigma = 2; % first smooth the image using gaussian filter
smoothImage = imgaussfilt(noisyImage, sigma);
smoothGradient = imgradient(smoothImage, 'CentralDifference');
figure, imshow(smoothGradient, []), title('Smoothed gradient magnitude')
评论0