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

조회 수: 3 (최근 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.
Deepika Sundresh
Deepika Sundresh 2021년 12월 11일
I just saw one entry like the one you have written. There wasn't a dummy file.

댓글을 달려면 로그인하십시오.

채택된 답변

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