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

4 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2021-12-11
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.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2021-12-10
编辑:Image Analyst 2021-12-10
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 个评论
Image Analyst
Image Analyst 2021-12-11
编辑:Image Analyst 2021-12-11
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.
Deepika Sundresh
Deepika Sundresh 2021-12-11
I got this output:
>> which -all blockproc
/matlab/toolbox/images/images/blockproc.m
I haven't named my script blockproc.m :)

请先登录,再进行评论。

更多回答(2 个)

Matt J
Matt J 2021-12-10
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 个评论
Deepika Sundresh
Deepika Sundresh 2021-12-10
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?

请先登录,再进行评论。


Deepika Sundresh
Deepika Sundresh 2021-12-11
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!

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by