Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
improve speed: caculate stastistical variance for every pixel in an image

Subject: improve speed: caculate stastistical variance for every pixel in an image

From: Chang Qin

Date: 30 Nov, 2010 17:46:20

Message: 1 of 5

Hi,
I want to caculate stastistical variance for every pixel in an image by using a window 71*71 centered window. The image size is 1440*960. My code is as follows:

Ivar=zeros(size(Im)); %Im is the oringal gray image
wsize=71;
for i=((wsize-1)/2+1):(size(Im,1)-(wsize-1)/2)
    for j=((wsize-1)/2+1):(size(Im,2)-(wsize-1)/2)
        window=Im((i-(wsize-1)/2):(i+(wsize-1)/2),(j-(wsize-1)/2):(j+(wsize-1)/2));
        window=window(:);
        window=single(window);
        Ivar(i,j)=var(window);
    end
end

This code works but it takes a long time to finish caculating.
Is there any inbuilt function that enables me to do this?

I think the problem is because of the two loop but I do not know how to avoid this.
There is a function "medfilt2" in matlab and it also uses a window and caculate for each pixel. However, "medfilt2" works far more faster than my code.

Can anybody give me some clues for this?
Thank you very much!!!

Subject: improve speed: caculate stastistical variance for every pixel in an image

From: Sean de

Date: 30 Nov, 2010 18:10:39

Message: 2 of 5

"Chang Qin" <cqin111@gmail.com> wrote in message <id3d9c$j2q$1@fred.mathworks.com>...
> Hi,
> I want to caculate stastistical variance for every pixel in an image by using a window 71*71 centered window. The image size is 1440*960. My code is as follows:
>
> Ivar=zeros(size(Im)); %Im is the oringal gray image
> wsize=71;
> for i=((wsize-1)/2+1):(size(Im,1)-(wsize-1)/2)
> for j=((wsize-1)/2+1):(size(Im,2)-(wsize-1)/2)
> window=Im((i-(wsize-1)/2):(i+(wsize-1)/2),(j-(wsize-1)/2):(j+(wsize-1)/2));
> window=window(:);
> window=single(window);
> Ivar(i,j)=var(window);
> end
> end
>
> This code works but it takes a long time to finish caculating.
> Is there any inbuilt function that enables me to do this?
>
> I think the problem is because of the two loop but I do not know how to avoid this.
> There is a function "medfilt2" in matlab and it also uses a window and caculate for each pixel. However, "medfilt2" works far more faster than my code.
>
> Can anybody give me some clues for this?
> Thank you very much!!!

Since you have medfilt2 we know you have the Image Processing Toolbox.

Take a look at:
>>doc stdfilt
Then by knowing the relationship between standard deviation and variance the second step is trivial.

Subject: improve speed: caculate stastistical variance for every pixel in an image

From: Chang Qin

Date: 30 Nov, 2010 18:30:20

Message: 3 of 5

Thank you so much, Sean de! That's really what I want.
Is there a similar function if I want to caculate the maximum for every pixel in the image by using a window 71*71 centered window?

Subject: improve speed: caculate stastistical variance for every pixel in an image

From: Chang Qin

Date: 30 Nov, 2010 18:36:05

Message: 4 of 5

"Chang Qin" <cqin111@gmail.com> wrote in message <id3frs$9ja$1@fred.mathworks.com>...
> Thank you so much, Sean de! That's really what I want.
> Is there a similar function if I want to caculate the maximum for every pixel in the image by using a window 71*71 centered window?

answer by myself:
J = rangefilt(I)
!!!!
Thank you~~~~

Subject: improve speed: caculate stastistical variance for every pixel in an image

From: Image Analyst

Date: 30 Nov, 2010 20:54:06

Message: 5 of 5

"Chang Qin" <cqin111@gmail.com> wrote in message
> answer by myself:
> J = rangefilt(I)
> !!!!
> Thank you~~~~
-----------------------------------------
Sorry, but that's NOT the answer. To find the max value in a sliding window, use imdilate(), not rangefilt().

By the way, here's my demo on taking Standard Deviation, Vairance, Sobel, and convolution filters:
(You may need to join any lines that the newsreader splits into two).

% 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, 3, 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, 3, 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, 3, 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, 3, 4);
imshow(standardDeviationImage, [])
title('Standard Deviation Image', 'FontSize', fontSize);

% Compute the standard deviation filter with MATLAB's built-in stdfilt() filter.
standardDeviationImage2 = stdfilt(grayImage);
subplot(2, 3, 5);
imshow(standardDeviationImage2, [])
title('Built-in stdfilt() filter', 'FontSize', fontSize);

% Perform Sobel filter
% h = fspecial('sobel') returns a 3-by-3 filter h (shown below) that emphasizes horizontal edges
% using the smoothing effect by approximating a vertical gradient.
% If you need to emphasize vertical edges, transpose the filter h'.
% [ 1 2 1
% 0 0 0
% -1 -2 -1 ]
verticalSobelKernel = fspecial('sobel');
sobelImage = imfilter(grayImage, verticalSobelKernel);
subplot(2, 3, 6);
imshow(sobelImage, [])
title('Sobel edge filter', 'FontSize', fontSize);

elapsedTime = toc(startTime);
message = sprintf('Done!\n\nElapsed time = %.2f seconds.', elapsedTime);
msgbox(message);
return; % End of local_variance() function.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us