Indexing 3D arrays using vectors of indexes
2 ビュー (過去 30 日間)
古いコメントを表示
I want to get RGB values of an image (utf-8 160x120x3 matrix). I know which pixels I want, I nx1 vectors, xi and yi, which are indexes for each of these pixels of interest. I would like the output in nx3 format. I've had this problem in the past where I'd like to get data from a 3D matrix using 2D indecies. This works in 2D where you can grab several rows at once using the colon (xi,:), but not in 3D. I would like to extract the "hyper-row" so to speak.
What is the fastest way to do this? I have a working solution using impixel that is way too slow for this application. I could create a 3D logical matrix, but I am only sampling a few pixels relative to the total number, and sparse matrices don't work for this applicaiton. I usually tend to vectorize rather than use for loops, but it doesn't seem like there is a great alternative.
lab_image = randi(255,160,120,3); % sample image
xi = [1 2 3];
yi = [1 2 3];
impixel(lab_image,xi,yi) % correct answer, but too slow.
lab_image(xi, yi, :) % wrong (but it works on 2D matrices?)
test2d = randi(255,10,10);
test2d(xi,:) % see how it grabs the rows associated with xi?
0 件のコメント
採用された回答
Rik
2022 年 10 月 15 日
The problem with indexing in Matlab is that it will grab every pair of indices, which is not always what you want.
The easiest solution is to convert your indices to linear indices with sub2ind. I would suggest doing each color channel separately and concatenating the result with cat(3,R,G,B).
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!