find pixels value in a volume using indexing/pixel position

1 view (last 30 days)
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

Answers (1)

Image Analyst
Image Analyst on 8 Jul 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 Comments
Hannah
Hannah on 9 Jul 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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!