How can I compute the mean and variance of a channel using blockproc?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Deepika Sundresh
il 10 Dic 2021
Risposto: Deepika Sundresh
il 11 Dic 2021
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 Commenti
Walter Roberson
il 11 Dic 2021
which -all blockproc
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.
Risposta accettata
Image Analyst
il 10 Dic 2021
Modificato: Image Analyst
il 10 Dic 2021
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 Commenti
Image Analyst
il 11 Dic 2021
Modificato: Image Analyst
il 11 Dic 2021
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.
Più risposte (2)
Matt J
il 10 Dic 2021
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;
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!