Sampling pixel intensities according to distance matrix...

2 ビュー (過去 30 日間)
Chaz Pelas
Chaz Pelas 2021 年 10 月 12 日
コメント済み: Image Analyst 2021 年 10 月 18 日
I have Mat(X) that consists of distances; lets say
Mat(X) = [2 1 2; 1 0 1; 2 1 2]
And an image, let's call it Mat(Y), containing an intensity value in every i, j element; for example
Mat(Y) = [9 5 6; 7 1 3; 2 8 4]
I would like a vector describing the average pixel intensity at the distances described by Mat(X) such that
y(0) = 1
y(1) = (5+3+7+8)/4
y(2) = (9+6+4+2)/4
I am not a very saavy coder as I imagine that this should not be very difficult to do, yet I am struggling to make it happen; ANY POINTERS ARE GREATLY APPRECIATED ! ! !

採用された回答

Voss
Voss 2021 年 10 月 12 日
Let X be your matrix of distances and Y be your matrix of intensities. Then the following code makes use of logical indexing to calculate the average value of Y at each unique value of X:
uX = unique(X(:)); % vector of unique distances
n_uX = numel(uX); % number of unique distances
uY = zeros(1,n_uX); % initialize average intensity vector
for i = 1:n_uX % for each unique distance
uY(i) = mean(Y(X == uX(i))); % average intensity is the mean of the intensities where distance == that unique distance
end
  2 件のコメント
DGM
DGM 2021 年 10 月 12 日
Might also want to round X so that the equality test works reliably. If other binning methods are used, it might be good to test for equality with tolerance.
Chaz Pelas
Chaz Pelas 2021 年 10 月 15 日
Benjamin thank you, I was able to get much closer to what I was looking for with this!

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2021 年 10 月 12 日
Use splitapply() which was meant for this kind of thing:
MatX = [2 1 2; 1 0 1; 2 1 2]
% And an image, let's call it Mat(Y), containing an intensity value in every i, j element; for example
MatY = [9 5 6; 7 1 3; 2 8 4]
theMeans = splitapply(@mean, MatY(:), MatX(:)+1)
theMeans =
1
5.75
5.25
  4 件のコメント
Chaz Pelas
Chaz Pelas 2021 年 10 月 15 日
I apologize for the erroneous example, I am near completely unaware of the limitations and uses of this platform. I greatly appreciate the hastey reply and the few suggestions, the stats toolbox had some neat things in it and the other functions had some interesting uses too.
Image Analyst
Image Analyst 2021 年 10 月 18 日
So did my code work for you like it did for me? Are we done here? If not, attach your nonworking code and nonworking data.

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


DGM
DGM 2021 年 10 月 12 日
編集済み: DGM 2021 年 10 月 12 日
Disregarding splitapply() for a moment, the issue of working with non-integers can be avoided by using the histogram tools to bin the distance array as desired.
X = [2 1 2; 1 0 1; 2 1 2]/100;
Y = [9 5 6; 7 1 3; 2 8 4];
nbins = 3; % you probably want more than 3
[~,~,idx] = histcounts(X,nbins);
binmeans = zeros(nbins,1);
for b = 1:nbins
binmeans(b) = mean(Y(idx == b));
end
binmeans
binmeans = 3×1
1.0000 5.7500 5.2500
If you want to use splitapply instead of the loop, you can do that too:
X = [2 1 2; 1 0 1; 2 1 2]/100;
Y = [9 5 6; 7 1 3; 2 8 4];
nbins = 3; % you probably want more than 3
[~,~,idx] = histcounts(X,nbins);
binmeans2 = splitapply(@mean,Y(:),idx(:))
binmeans2 = 3×1
1.0000 5.7500 5.2500
I'm sure findgroups would work too.
X = [2 1 2; 1 0 1; 2 1 2]/100;
Y = [9 5 6; 7 1 3; 2 8 4];
nbins = 3; % you probably want more than 3
idx = findgroups(X(:));
binmeans2 = splitapply(@mean,Y(:),idx(:))
binmeans2 = 3×1
1.0000 5.7500 5.2500
Findgroups may be simpler to use than assuming that groups are uniformly distributed (as with histogram tools). Depends on what you want, I guess.

カテゴリ

Help Center および File ExchangeRead, Write, and Modify Image についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by