Dump Image Pixels into vector from coordinates

조회 수: 2 (최근 30일)
Philip
Philip 2012년 4월 16일
Is there a way to use 2 vectors of pixel coordinates to quickly dump the pixel values into a 3rd vector?
Currently, I am using the following for loop, but it is very slow:
for j=1:length(x)
pixel_vals(j) = img(x(j),y(j));
end
where, 'x' and 'y' are the x,y coordinates (1xN vector form).
Many thanks!

채택된 답변

Walter Roberson
Walter Roberson 2012년 4월 16일
pixel_vals = img(subs2ind(size(x), x, y));

추가 답변 (2개)

Image Analyst
Image Analyst 2012년 4월 16일
Philip, in response to the new question in your "Answer" (not your original question), the preferred way to do this is with ismember(). Your way would take forever for an image with lots of pixels that have labels. See this code snippet from my image segmentation tutorial, BlobsDemo: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
% Extract only those blobs that meet our criteria, and
% eliminate those blobs that don't meet our criteria.
% Note how we use ismember() to do this.
keeperBlobsImage = ismember(labeledImage, keeperIndexes);
% Re-label with only the keeper blobs kept.
labeledDimeImage = bwlabel(keeperBlobsImage, 8); % Label each blob so we can make measurements of it

Philip
Philip 2012년 4월 16일
On a similar note, is there any way that I can replace the following loop:
label_ids = [1 3 11 24 55];
map = zeros(height_img, width_img);
for i=1:length(label_ids)
map = BWlabel_img==label_ids(i) + map;
end
The label_ids correspond to line segments in a 'bwlabel' image that I want to keep. Essentially, I want to create a new image 'map' that contains only the line segments with these id numbers...
  댓글 수: 1
Image Analyst
Image Analyst 2012년 4월 16일
This should have been a new question, not an answer. Comments can't have formatted code, so see my answer below.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by