Skip to content

Instantly share code, notes, and snippets.

@marsfan
Last active August 31, 2020 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marsfan/5526d75efaa618961e2ad5ee7473904c to your computer and use it in GitHub Desktop.
Save marsfan/5526d75efaa618961e2ad5ee7473904c to your computer and use it in GitHub Desktop.
Demonstration of how processing SNAP images can be improved using GPU
% Clear any open stuff
clc
close all
%% Process the image on the CPU
% "clear all" will empty everything, including compiled scripts, MEX functions, and more.
% By using it, we are essentially creating a clean slate for each section
% call.
clear all
tic
% Path to band files
path = 'S2A_MSIL2A_20200812T170901_N0214_R112_T14SPB_20200812T213938.data';
% Load the RGB bands into MATLAB
red = multibandread(fullfile(path, 'B4.img'), [10980, 10980, 1], 'uint16=>single', 0, 'bsq', 'ieee-be');
green = multibandread(fullfile(path, 'B3.img'), [10980, 10980, 1], 'uint16=>single', 0, 'bsq', 'ieee-be');
blue = multibandread(fullfile(path, 'B2.img'), [10980, 10980, 1], 'uint16=>single', 0, 'bsq', 'ieee-be');
% Scale Bands to be between 0 and 1 for proper display in MATLAB
red = red./ ((2^16)-1);
green = green./ ((2^16)-1);
blue = blue./ ((2^16)-1);
% Auto-Adjust the contrast of the Bands
red = imadjust(red);
green = imadjust(green);
blue = imadjust(blue);
% Combine the bands into a RGB image
rgb = cat(3, red, green, blue);
fprintf("CPU Compute Time: %g\n", toc)
clear rgb red green blue
%% Process the images on the GPU
clear all
tic
% Path to band files
path = 'S2A_MSIL2A_20200812T170901_N0214_R112_T14SPB_20200812T213938.data';
red = multibandread(fullfile(path, 'B4.img'), [10980, 10980, 1], 'uint16=>single', 0, 'bsq', 'ieee-be');
green = multibandread(fullfile(path, 'B3.img'), [10980, 10980, 1], 'uint16=>single', 0, 'bsq', 'ieee-be');
blue = multibandread(fullfile(path, 'B2.img'), [10980, 10980, 1], 'uint16=>single', 0, 'bsq', 'ieee-be');
% Load the bands into the GPU
red = gpuArray(red);
green = gpuArray(green);
blue = gpuArray(blue);
% Scale Bands to be between 0 and 1 for proper display in MATLAB
red = red./ ((2^16)-1);
green = green./ ((2^16)-1);
blue = blue./ ((2^16)-1);
% Auto-Adjust the contrast of the Bands
red = imadjust(red);
green = imadjust(green);
blue = imadjust(blue);
% Combine bands into RGB image
rgb = cat(3, red, green, blue);
fprintf("GPU Compute Time: %g\n", toc)
clear rgb red green blue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment