Help Vectorizing For-Loop with Complex Statements
Afficher commentaires plus anciens
% size of image
imageSize = 564;
% generate the color range used in the colormap
colormap_used_in_image = jet(256^3); % for perfect results use 256^3, 256^2 is good estimator
colormap_used_in_image = colormap_used_in_image * 255; % convert to actual RGB values
% example image where some of the data is in the jet colormap, some isn't
data = randi(255,564,564,3);
% convert data to doubles for math
data = double(data);
% empty array to be filled with our jet mapped data
jetmappedData = nan(imageSize,imageSize);
% this loop calculates which value from the colormap best fits the
% image
wb = waitbar(0,append('Colormapping Values for Image # ',string(1)));
for r = 1:imageSize % for each row in the image
for c = 1:imageSize % for each column in the image
diff1 = abs(data(r,c,1) - colormap_used_in_image(:,1)); % difference in R value
diff2 = abs(data(r,c,2) - colormap_used_in_image(:,2)); % difference in G value
diff3 = abs(data(r,c,3) - colormap_used_in_image(:,3)); % difference in B value
diffTotal = diff1 + diff2 + diff3; % difference in all the values
[val,loc] = min(diffTotal);
if val <= 3 % if the value isn't found in jet
jetmappedData(r,c) = loc;
else
jetmappedData(r,c) = NaN;
end
end
waitbar(r/imageSize,wb,append('Colormapping Values for Image # ',string(1)));
end
I have a lossless image (564x564) generated from MATLAB with a jet-colorbar from a range of [0,1]. The original data is lost and I'm trying to recover it from the image. Here I am converting RGB values to their original datapoints. Over sixteen trillion operations need to be performed per photo... far too many. How can I vectorize this for loop to increase efficiency? (I also have a 3070ti GPU if it would help to utilize).
Thanks!
2 commentaires
Matt J
le 24 Avr 2022
Your code cannot be run. Input data has not been attached.
Parker McLeod
le 24 Avr 2022
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!