Array Indexing with vector

14 ビュー (過去 30 日間)
Kurt Lanes
Kurt Lanes 2019 年 10 月 29 日
I have a simple problem with vectorizing some code. I have two arrays ConvolvedImage is a RGB array with 5000x5000x3 pixels and FPA_Image is a is an array that is about 518x533x3 array. I am projecting the image in ConvolvedImage onto the FPA_Image image array but typically a 100 pixels will project into each FPA_Image pixel. In that case I sum the pixel values and divide by the number summed. For clarity I will include a little more code that just the line that fails to do what I imagine it should. The basic flow is build index which has the linear index into ConvolvedImage and indexes every pixel. FPAindex is a vector that has the linear pixel index that tell me where each ConvolvedImage maps to in the FPA_Image. IT is built from two vectros FPA_iX and FPA_iY. I do each color plane separately because I could not figure out how to handle all three in one statement. The basic problem is easiest to see on the last line of code that counts how many ConvolvedImage pixels fall into each FPA_Image pixel. The failure is simple even, though 100 of the values in FPAindex have the same value the value that goes into FPA_Image_Count is only the number 1 or zero. So only one of the elements is getting counted. If I unwrap the vectorization by indexing FPAindex(i) where i ranges over the lenght of FPAindex (2,500,000) it works as expected. The FPA_Image statement work the same. Only one of the pixels in ConvolvedImage gets assigned to the result. I can imagine why this is. Is there a way to vectroize this and have it work?
[ix , iy]= ndgrid([1:ImageROWS],[1:ImageCOLS]); % grid of indexes for Scene
ix = reshape(ix,[ImageROWS*ImageCOLS,1]); % Make row vector of x(row) indexes
iy = reshape(iy,[ImageROWS*ImageCOLS,1]); % Make row vector if y(column) indexes
index = sub2ind([ImageROWS,ImageCOLS,1],ix,iy); % Make a single row vector for (x,y)
FPAindex = sub2ind(size(FPA_Image),FPA_iX,FPA_iY)'; % Make a single row vector for (x,y)
FPA_Image(FPAindex) = ... % Sum scene pixel into FPA pixel
FPA_Image(FPAindex) + ConvolvedImage(index); % Columns are X increasing to right
FPA_Image(FPAindex+ROWS*COLUMNS) = ... % Sum scene pixel into FPA pixel
FPA_Image(FPAindex+ROWS*COLUMNS)+ConvolvedImage(index+ImageROWS*ImageCOLS);% Columns are X increasing to right
FPA_Image(FPAindex+2*ROWS*COLUMNS) = ... % Sum scene pixel into FPA pixel
FPA_Image(FPAindex+2*ROWS*COLUMNS)+ConvolvedImage(index+2*ImageROWS*ImageCOLS);% Columns are X increasing to right
FPA_Image_Count(FPAindex) = ...
FPA_Image_Count(FPAindex) + 1; % Count Scene pixels in this FPA Pixel
Any help appreciated.
Thanks
Kurt

回答 (0 件)

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by