This example shows how to perform image processing operations on a GPU. The example uses filtering to highlight the watery areas in a large aerial photograph.
Read an image into the workspace.
imCPU = imread('concordaerial.png');
Move the image to the GPU by creating a
imGPU = gpuArray(imCPU);
As a preprocessing step, change the RGB image to a grayscale image. Because
you are passing it a
performs the conversion operation on a GPU. If you pass a
gpuArray as an argument, then a function that has been
GPU-enabled performs the operation on the GPU.
imGPUgray = rgb2gray(imGPU);
View the image in the Image Viewer and inspect the pixel
values to find the value of watery areas. Note that you must bring the image
data back onto the CPU, using the
gather function, to use the
Image Viewer. As you move the mouse over the image, you can view the value of
the pixel under the cursor at the bottom of the Image Viewer. In the image,
areas of water have pixel values less than 70.
Filter the image on the GPU to get a new image that contains only the pixels with values of 70 or less and view it.
imWaterGPU = imGPUgray<70; figure;imshow(imWaterGPU);
Using morphological operators that are supported on the GPU, clean up the mask image by removing points that do not represent water.
imWaterMask = imopen(imWaterGPU,strel('disk',4)); imWaterMask = bwmorph(imWaterMask,'erode',3);
Blur the mask image, using
blurH = fspecial('gaussian',20,5); imWaterMask = imfilter(single(imWaterMask)*10, blurH);
Boost the blue channel to identify the watery areas.
blueChannel = imGPU(:,:,3); blueChannel = imlincomb(1, blueChannel,6, uint8(imWaterMask)); imGPU(:,:,3) = blueChannel;
View the result. The
can work with images on the GPU.
After filtering the image on the GPU, move the data back to the CPU using the
gather function, and write the modified image to a
outCPU = gather(imGPU); imwrite(outCPU,'concordwater.png');