Error while finding mean and variance using blockproc of an image

1 view (last 30 days)
I have a blockproc function as follows:
function [mean_ch,variance] = block_process(subchannel)
fun1 = @(block_struct) mean(subchannel(:));
mean_ch = blockproc(subchannel, [8 8], fun1);
fun2 = @(block_struct) var(subchannel(:));
variance = blockproc(subchannel, [8 8], fun2);
end
When I run this for, say the red channel of my image(in tiff format), I get a matrix of all same values. When I try to do a scatterplot by reshaping these values I get a single value. I believe the function is only running over a single block hence the single value, but I am supposed to use blockproc as a sliding window operator to calculate the sample mean and variance of each 8x8 block. How can achieve the right results?

Accepted Answer

Walter Roberson
Walter Roberson on 12 Dec 2021
function [mean_ch,variance] = block_process(subchannel)
COL = @(M) M(:);
fun1 = @(block_struct) mean(COL(block_struct.data(:,:,subchannel)));
mean_ch = blockproc(subchannel, [8 8], fun1);
fun2 = @(block_struct) var(COL(block_struct.data(:,:,subchannel)));
variance = blockproc(subchannel, [8 8], fun2);
end
This assumes that you want to process each independent 8 x 8 block, rather than wanting do sliding window.
If you do want to do sliding window, and you want it to proceed one pixel at a time, then there is a bit of a problem because of the way that blockproc handles sliding windows: when you want to slide by an odd amount (such as 1 pixel at a time) then it can only handle blocks that are odd-sized (such as 7 x 7, but not 8 x 8)
Because of this, if you do want to shift one pixel at a time and you want 8 x 8 blocks, it is usually easier to do some conv2() operations, or nlfilter()
  7 Comments
Walter Roberson
Walter Roberson on 13 Dec 2021
Question: for your purposes, is it acceptable that your output is smaller than your input? Because there are ways to code it so that it operates on whatever is inside the sliding window -- though definitions of what is inside the window are certainly easier when the window is an odd size instead of an even size.
Deepika Sundresh
Deepika Sundresh on 13 Dec 2021
The output would be smaller than the input right? That's what I understood :)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 13 Dec 2021
To get the mean in a sliding window that slides over one pixel at a time:
windowWidth = 3; % Whatever...
kernel = ones(windowWidth) / windowWidth^2;
blurredImage = imfilter(grayImage, kernel);
To get the standard deviation, use stdfilt():
windowWidth = 3; % Whatever...
kernel = ones(windowWidth);
sdImage = stdfilt(grayImage, kernel);
% Square to get variance
varImage = double(sdImage) .^ 2;

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!