find pixels value in a volume using indexing/pixel position

5 vues (au cours des 30 derniers jours)
Hannah
Hannah le 8 Juil 2014
Commenté : Image Analyst le 9 Juil 2014
hello
I have a 500X3 matrix named PixelPos where it represents pixel position in a volume x, where the first column PixelPos(:,1) has the row numbers, PixelPos(:,2) is the column number and PixelPos(:,3) is the slice number. i want to get the value for these pixels, i used a for loop to find the value for each pixel in x but it is very time consuming, is there a function in matlab that could be helpful ?
thanks Hannah

Réponses (1)

Image Analyst
Image Analyst le 8 Juil 2014
Looping 500 times should not be time consuming. Just make sure you access the right most values (the Z values) less frequently so you don't have a lot of memory thrashing.
clc;
PixelPos = randi(9, [50,3]); % Sample data
% Get sort order based on column 3.
[~, sortOrder] = sort(PixelPos(:,3));
sortedPositions = PixelPos(sortOrder, :);
for k = 1 : length(sortedPositions)
row = sortedPositions(k, 1);
col = sortedPositions(k, 2);
slice = sortedPositions(k, 3);
value = array3d(row, columns, slice);
fprintf('For row=%d, column=%2, slice=%d, the value = %d',...
row, col, slice, value);
end
  2 commentaires
Hannah
Hannah le 9 Juil 2014
Thank you. am working with medical data and anticipating that for the data i will receive the matrix size could increase to a large number ~ 10^7. so i was wondering if there is another way other than using a loop. i found sub2ind function in matlab to work well.
rows = PixelPos(:,1);
colm = PixelPos(:,2);
slice = PixelPos(:,3);
linearInd = sub2ind(size(x), rows, colm,slice);
value = x(linearInd)
Hannah x
Image Analyst
Image Analyst le 9 Juil 2014
Try it and see.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by