MATLAB Matrix Indexing - Image Processing
Show older comments
Hello,
I am working on a MATLAB-based image recognition algorithm. However, I run into this problem. Would you mind helping me?
This is a simplified example that I created to illustrate the MATLAB problem that I am facing:
img = [0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0;0 0 0 0 1 1 0 0 0 0; 0 0 0 0 1 1 1 0 0 0;0 2 2 0 0 1 1 0 0 0; 0 2 2 2 2 0 0 0 0 0;0 0 2 2 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0 0 0;];
data = randi(255,10,10,50);
How would you get the location of the 1 in matrix img, label the location matrix as loc1, and then get all the values in the matrix data based on location matrix 1? (i.e. the end result should be a nxnx50 matrix)
My next question will be how do you run that procedure in parallel? (i.e. run for value 1 and 2 together)
Thank you so much!
Answers (1)
I'm not sure, but maybe you want this:
dataV = reshape(data, 100, []);
loc1 = (img == 1);
result = dataV(loc1, :);
In parallel? Hm, what does "1 and 2 together" mean? Maybe:
loc1 = (img == 1 || img == 2);
Or simply call the above in a loop? Perhaps a parfor loop?
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!