Error while finding mean and variance using blockproc of an image
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Deepika Sundresh
le 12 Déc 2021
Réponse apportée : Image Analyst
le 13 Déc 2021
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?
0 commentaires
Réponse acceptée
Walter Roberson
le 12 Déc 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 commentaires
Walter Roberson
le 13 Déc 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.
Plus de réponses (1)
Image Analyst
le 13 Déc 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;
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!