What is a vectorized way to calculate variance of an image patch

2 ビュー (過去 30 日間)
Victor Yi
Victor Yi 2015 年 5 月 30 日
コメント済み: GAURA YADAV 2020 年 6 月 19 日
I have a 560*560*3 image file, I want to divide the image into many small 8*8 patches, then calculate variance of each patch. What is a vectorized way to calculate variance of each image patch ?
  2 件のコメント
Walter Roberson
Walter Roberson 2015 年 5 月 30 日
How do you want to calculate variance of color? If you have an 8 x 8 x 3 subsection, S, representing color, do you want var(S(:)), which treats all the pixel components equally? Or do you want variance of the grayscale equivalent, and thus variance of brightness? Or do you want to convert to HSL and do variance of Hue? Or.... ?
Victor Yi
Victor Yi 2015 年 5 月 31 日
I want to calculate the variance of each R, G, and B of the 8*8*3 image patch, 3 variance values for one patch.

サインインしてコメントする。

採用された回答

Roger Stafford
Roger Stafford 2015 年 5 月 31 日
編集済み: Roger Stafford 2015 年 5 月 31 日
You do need to answer Walter's question. Here are two possibilities:
1) From your I = 560 x 560 x 3 array, each 8 x 8 x 3 block will produce a 1 x 1 x 3 array of variances giving a 70 x 70 x 3 result. That is, each of the three colors has its separate page of 70 x 70 variances in a 3D result.
p = 8; q = 8; % Each block is p x q
m = 70; n = 70; c = 3; % There are m x n x c blocks
r = 0:(p*q*m*n*c-1);
s = 1+r+p*(m-1)*floor(r/p)-p*(m*q-1)*floor(r/(p*q))...
+p*m*(q-1)*floor(r/(p*m*q));%The required permutation
V = reshape(var(reshape(I(s),p*q,[])),m,n,c); % Variances
2) Your I array is 560 x 560 of grey-level values. Each 8 x 8 block will produce a single variance giving a 2D 70 x 70 result.
p = 8; q = 8; % Each block is p x q
m = 70; n = 70; % There are m x n blocks
r = 0:(p*q*m*n-1);
s = 1+r+p*(m-1)*floor(r/p)-p*(m*q-1)*floor(r/(p*q))...
+p*m*(q-1)*floor(r/(p*m*q));%The required permutation
V = reshape(var(reshape(I(s),p*q,[])),m,n); % Variances
  2 件のコメント
Victor Yi
Victor Yi 2015 年 5 月 31 日
Thank you ! the first sample code solved the problem, but I still didn't figure out the meaning of the sample code. can you explain what is the meaning of the variable "s" and how did you use it to calculate variance.
Walter Roberson
Walter Roberson 2015 年 5 月 31 日
var() of a lot of values that are not consecutive can be handled by moving the values so that they are consecutive and then taking the variance. Roger builds "s" as a list of the indices of elements that need to go together. It is a clever approach but not readable at all in the form written.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2015 年 5 月 31 日
You can use blockproc() (in the Image Processing Toolbox) to calculate the variance of each small tile of an image in a line or two of code since the function is meant for this purpose. Run my attached demos for an example.
  1 件のコメント
Victor Yi
Victor Yi 2015 年 5 月 31 日
Thank you the new approach ! I didn't know there is a function for this purpose.

サインインしてコメントする。


Matt J
Matt J 2015 年 5 月 31 日
Another approach to consider is stdfilt(), which will compute standard deviation in a sliding window fashion rather than a tiled fashion. This is more computation than you need. However, if stdfilt is sufficiently code-optimized, it might be worth it.
  3 件のコメント
Image Analyst
Image Analyst 2015 年 6 月 1 日
And, if you want to do your own custom function with whatever crazy thing you can imagine, then you can use nlfilter(). It's like conv2(), imfilter(), stdfilt(), entropyfilt(), etc. except that you get to define your own custom code to apply at each filter window location.
GAURA YADAV
GAURA YADAV 2020 年 6 月 19 日
Thanks.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by