From: Luna Moon on 5 Jul 2010 12:20 Hi all, In recursive moving or rolling average, result_t = alpha * p_t + (1-alpha)* result_{t-1}, where p_t is the new arrival... How do we do this fast in Matlab, using the "filter" function format, i.e. it works on a whole array column-wise. Now, how to apply the same concept to rolling variance? I guess in order to do rolling variance, we need to first get the rolling average, and then the rolling mean square: rolling_mean_square_t = alpha*p_t^2 + (1- alpha)*rolling_mean_square_{t-1}, then using: rolling_variance_t = rolling_mean_square_t - rolling_mean_t ^2 ... Am I right? How to do this fast? Thanks
From: ImageAnalyst on 5 Jul 2010 12:33 Study my variance filter demo and write back if you have any questions after that: % Demo to take the local mean, variance, and standard deviation % of a gray scale image. % userImage, if passed in, is used as the image. % If userImage is not passed in, user is asked to use a demo image. % Code written by ImageAnalyst function local_variance(userImage) % Clean up. clc; % Clear the command window. close all; % Close all figures (except those of imtool.) workspace; % Make sure the workspace panel is showing. % Change the current folder to the folder of this m-file. % (The line of code below is from Brett Shoelson of The Mathworks.) % Don't use these lines if you're calling this from another m-file. if(~isdeployed) cd(fileparts(which(mfilename))); end % Initialize. fontSize = 20; if nargin == 0 % No image passed in on the command line. % Read in one of the standard MATLAB demo images % as our original gray scale image and display it. promptMessage = sprintf('Which image do you want to use.\nThe coins or the cameraman?'); button = questdlg(promptMessage, 'Select Image', 'Coins', 'Cameraman', 'Coins'); if strcmp(button, 'Coins') grayImage = double(imread('coins.png')); % Cast to double. else grayImage = double(imread('cameraman.tif')); % Cast to double. end else % Use the image array passed in on the command line. grayImage = double(userImage); % Cast to double. end % Start timing. startTime = tic; subplot(2, 2, 1); imshow(grayImage, []); title('Original Image', 'FontSize', fontSize); set(gcf, 'Position', get(0,'Screensize')); % Maximize figure. % Blur the image with a 5 by 5 averaging (box filter) window. blurredImage = conv2(grayImage, ones(5,5)/25); subplot(2, 2, 2); imshow(blurredImage, []); title('Blurred Image', 'FontSize', fontSize); % Perform a variance filter. % Output image is the variance of the input image in a 3 by 3 sliding window. VarianceFilterFunction = @(x) var(x(:)); varianceImage = nlfilter(grayImage, [3 3], VarianceFilterFunction); % An alternate way of doing the variance filter is on the next line: % varianceImage = reshape(std(im2col(originalImage,[3 3],'sliding')), size(originalImage)-2); subplot(2, 2, 3); imshow(varianceImage, []) title('Variance Image', 'FontSize', fontSize); % Compute the square root of the variance image to get the standard deviation. standardDeviationImage = sqrt(varianceImage); subplot(2, 2, 4); imshow(standardDeviationImage, []) title('Standard Deviation Image', 'FontSize', fontSize); elapsedTime = toc(startTime); message = sprintf('Done!\n\nElapsed time = %.2f seconds.', elapsedTime); msgbox(message); return; % End of local_variance() function.
From: Luna Moon on 5 Jul 2010 12:37 On Jul 5, 12:33 pm, ImageAnalyst <imageanal...(a)mailinator.com> wrote: > Study my variance filter demo and write back if you have any questions > after that: > > % Demo to take the local mean, variance, and standard deviation > % of a gray scale image. > % userImage, if passed in, is used as the image. > % If userImage is not passed in, user is asked to use a demo image. > % Code written by ImageAnalyst > function local_variance(userImage) > % Clean up. > clc; % Clear the command window. > close all; % Close all figures (except those of imtool.) > workspace; % Make sure the workspace panel is showing. > > % Change the current folder to the folder of this m-file. > % (The line of code below is from Brett Shoelson of The Mathworks.) > % Don't use these lines if you're calling this from another m-file. > if(~isdeployed) > cd(fileparts(which(mfilename))); > end > > % Initialize. > fontSize = 20; > if nargin == 0 > % No image passed in on the command line. > % Read in one of the standard MATLAB demo images > % as our original gray scale image and display it. > promptMessage = sprintf('Which image do you want to use.\nThe > coins or the cameraman?'); > button = questdlg(promptMessage, 'Select Image', 'Coins', > 'Cameraman', 'Coins'); > if strcmp(button, 'Coins') > grayImage = double(imread('coins.png')); % Cast to double. > else > grayImage = double(imread('cameraman.tif')); % Cast to double. > end > else > % Use the image array passed in on the command line. > grayImage = double(userImage); % Cast to double. > end > > % Start timing. > startTime = tic; > > subplot(2, 2, 1); > imshow(grayImage, []); > title('Original Image', 'FontSize', fontSize); > set(gcf, 'Position', get(0,'Screensize')); % Maximize figure. > > % Blur the image with a 5 by 5 averaging (box filter) window. > blurredImage = conv2(grayImage, ones(5,5)/25); > subplot(2, 2, 2); > imshow(blurredImage, []); > title('Blurred Image', 'FontSize', fontSize); > > % Perform a variance filter. > % Output image is the variance of the input image in a 3 by 3 sliding > window. > VarianceFilterFunction = @(x) var(x(:)); > varianceImage = nlfilter(grayImage, [3 3], VarianceFilterFunction); > % An alternate way of doing the variance filter is on the next line: > % varianceImage = reshape(std(im2col(originalImage,[3 3],'sliding')), > size(originalImage)-2); > subplot(2, 2, 3); > imshow(varianceImage, []) > title('Variance Image', 'FontSize', fontSize); > > % Compute the square root of the variance image to get the standard > deviation. > standardDeviationImage = sqrt(varianceImage); > subplot(2, 2, 4); > imshow(standardDeviationImage, []) > title('Standard Deviation Image', 'FontSize', fontSize); > elapsedTime = toc(startTime); > > message = sprintf('Done!\n\nElapsed time = %.2f seconds.', > elapsedTime); > msgbox(message); > return; % End of local_variance() function. Great code! However I don't have "imshow' or "nlfilter", i.e. I don't have image processing toolbox... and lets not get into that procurement etc. I am thinking of maybe there is a generic way in Matlab of doing "recursive" computation, be it recursive mean, or recursive variance/std, or other moments, etc. Any thoughts? Thanks a lot!
From: Steve Amphlett on 5 Jul 2010 12:48 Luna Moon <lunamoonmoon(a)gmail.com> wrote in message <6fc04321-2ece-4f41-ad2d-4a0a2b436bae(a)x27g2000yqb.googlegroups.com>... > > Great code! > > However I don't have "imshow' or "nlfilter", i.e. I don't have image > processing toolbox... and lets not get into that procurement etc. > > I am thinking of maybe there is a generic way in Matlab of doing > "recursive" computation, > > be it recursive mean, or recursive variance/std, or other moments, > etc. > > Any thoughts? Thanks a lot! Yes. Why do you use the word "recursive"? Your inputs do not depend on your outputs.
From: ImageAnalyst on 5 Jul 2010 12:49 Well just use blockproc and the var() functions - that's one way.
|
Next
|
Last
Pages: 1 2 Prev: How do you do a moving median fast? Next: How to align dates in Matlab fast? |