Image Processing/Matrix Math Question
4 views (last 30 days)
Show older comments
Hello,
I have an m-by-n matrix of numerical values which correspond to weighting factors (essentially intensities) applied to each point (x,y) in an x-y coordinate system. What I would like to do is apply a function f(x,y) to each point (x,y) and then multiply the function by the weighting factor. This would hopefully result in another m-by-n matrix. In addition, I would like to sum up the values of the resulting matrix.
I would appreciate any help on this problem.
Thank you much,
John
0 Comments
Accepted Answer
Image Analyst
on 18 May 2013
That is exactly what the nlfilter() functions does. Here's your demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
A = imread('cameraman.tif');
[rows columns] = size(A);
A = im2double(A);
fun = @(x) median(x(:));
B = nlfilter(A,[5 5],fun);
subplot(2,2,1);
imshow(A)
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Display image B
subplot(2,2,2);
imshow(B)
title('Function applied to image', 'FontSize', fontSize);
% Get array of weights the same size as the image.
weights = peaks(256);
weights = weights - min(weights(:)); % Make all positive.
subplot(2,2,3);
imshow(weights, [])
title('Our weights', 'FontSize', fontSize);
% Multiply weights by image.
outputImage = weights .* B;
% Display image outputImage
subplot(2,2,4);
imshow(outputImage, [])
title('Now with weights applied', 'FontSize', fontSize);
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!