How can I compute the mean and variance of a channel using blockproc?

I amy trying to compute the block-based mean and variance of each channel of an image using blockproc through block of size 8x8. After splitting the image into its channels (in the RGGB format), I used blockproc as for the red channel (I_r)
fun1 = @(block_struct) mean(block_struct.data);
mean_ch = blockproc(I_r, [8 8], fun1);
fun2 = @(block_struct) var(block_struct.data);
variance = blockproc(I_r, [8 8], fun2);
scatter(mean_ch, variance); %scatter plot of mean and variance
I get the error saying that blockproc has too many input arguments. Considering I took the syntax from the documentation, I am unable to figure what the problem is. Why does it say "too many input arguments"? Is there a different way to find mean and variance with blockproc? Any help is appreciated.

2 Comments

which -all blockproc
/MATLAB/toolbox/images/images/blockproc.m
You should see one output that looks similar to the above, but in the directory that you installed MATLAB in to.
I suspect you will see two files instead, and that the first one is a third-party file that is interfering with your use of the Mathworks code.
I just saw one entry like the one you have written. There wasn't a dummy file.

Sign in to comment.

 Accepted Answer

You need to use (:) to turn the block data into a column vector, otherwise you get multiple values instead of one.
%===============================================================================================================================
% Define the function that we will apply to each block.
% First in this demo we will take the standard deviation gray value in the block
% and create an equal size block where all pixels have the standard deviation value.
% Image will be the same size since we are using ones() and so for each block
% there will be a block of 8 by 8 output pixels.
stdFilterFunction = @(theBlockStructure) std(theBlockStructure.data(:)) * ones(size(theBlockStructure.data), class(theBlockStructure.data));
% Note: it's the ones(size(theBlockStructure.data), class(theBlockStructure.data)) that
% makes the output be the same size as the input rather than 1/8th the size of the input. It basically replicates the standard deviation pixel into an 8x8 square.
%===============================================================================================================================
% Standard Deviation of 8 pixel by 8 pixel block
% Block process the image to replace every pixel in the
% 8 pixel by 8 pixel block by a single pixel whose value is the standard deviation of the pixels in the block.
blockSize = [8, 8];
% Quirk: must cast grayImage to single or double for it to work with std().
% blockyImage8888 = blockproc(grayImage, blockSize, stdFilterFunction); % Doesn't work.
blockyImage8888 = blockproc(double(grayImage), blockSize, stdFilterFunction); % Works.
[rows, columns] = size(blockyImage8888);
See my attached demos for a full demo.
Or you could try mean2() instead of mean(), and var2() instead of var().

3 Comments

Thank you. I tried mean and mean2 both, and I got the same error. I then ran your demo code for mean, and I got this error:
Error using blockproc
Too many input arguments.
Error in blockproc_mean (line 54)
blockyImage8888 = blockproc(grayImage, blockSize, meanFilterFunction); % Works
Now I am unable to understand why this is happening. I tried with a different image as well.
What does this say
>> which -all blockproc
You didn't, by chance, name your script "blockproc.m" did you? Never name your scripts or functions after built-in functions.
I got this output:
>> which -all blockproc
/matlab/toolbox/images/images/blockproc.m
I haven't named my script blockproc.m :)

Sign in to comment.

More Answers (2)

It will be more efficient if you download sepblockfun() instead,
A=double(rgbImage);
Means=sepblockfun(A,[8,8,1],'mean');
Vars=sepblockfun(A.^2,[8,8,1],'mean') - Means.^2;

1 Comment

The requirement for me is to use blockproc only, and that is why i am facing issues. Could you tell me how I can do it blockproc itself?

Sign in to comment.

I was able to execute the code properly, it was that an older function was saved as blockproc when I downloaded the assignment, and it was interfering with my code. I deleted it and the blockproc works fine. Thank you all for the help!

Products

Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!