Using color channel to find blue sky in a image.

6 views (last 30 days)
I want to design a angirithm to determine if an image has blue sky, gray sky, or no sky. I want to use color channel and assumes sky is in the top of the image.
For example, This image has blue sky and the algorithm should return "blue sky".
This image has gray sky and the algorithm should return "gray sky".
This image has no sky and the algorithm should return "none".
Thanks a lot!

Accepted Answer

Image Analyst
Image Analyst on 26 Mar 2017
Edited: Image Analyst on 26 Mar 2017
This is very easy. Any of my 3 color segmentation demos in my File Exchange should work. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862&sort=downloads_desc
You can also use the Color Thresholder on the Apps tab of the Tool ribbon.
You might also want to do a texture filter like stdfilt() to try to find relatively smooth areas of the image that are near the top of the image. Sky should be both fairly smooth in texture and fairly uniform in color, in most cases. Are you going to have to detect sky with isolated puffy white clouds in it? That could complicate things.
  2 Comments
David Levi
David Levi on 14 Feb 2022
According to your answer, how can I detect detect sky with isolated puffy white clouds in the image?
(without using Color Thresholder)
Image Analyst
Image Analyst on 14 Feb 2022
Edited: Image Analyst on 19 Feb 2022
If you're going to have an incredibly wide variety of sky appearances you might have to use a deep learning model like SegNet.
Try this:
% Program to take a folder of RGB images, and labeled images and train a SegNet Deep Learning Model.
tic;
% Define the size of your RGB and labeled (gray scale) images.
imageSize = [128, 128, 3];
%============================================================================================================================================
% Specify where the RGB training images live. Images are 128x128x3 (unless you use something different above).
imageDir = fullfile(pwd, 'RGB Resized to 128');
if ~isfolder(imageDir)
errorMessage = sprintf('Error: RGB folder does not exist:\n%s', imageDir);
uiwait(errordlg(errorMessage));
return;
end
% Specify where the labeled ("ground truth") images live. Images are 128x128 (unless you use something different above).
labelDir = fullfile(pwd, 'Labeled Images)';
if ~isfolder(labelDir)
errorMessage = sprintf('Error: label folder does not exist:\n%s', labelDir);
uiwait(errordlg(errorMessage));
return;
end
%============================================================================================================================================
% Setup options:
% Create Checkpoint Folder so each iteration can be saved. Used to restart in case it crashes.
checkpointPath = fullfile(pwd, 'Checkpoint');
if ~isfolder(checkpointPath)
mkdir(checkpointPath);
end
% Assign label values
classNames = ["background","Foreground"];
labelIDs = [2, 1];
% Create Segnet Deep Learning network consisting of 2 classes and 6 layers.
numClasses = 2;
numLayers = 6;
lgraph = segnetLayers(imageSize, numClasses, numLayers);
% Create Image Datastore containing the resized RGB images
imds = imageDatastore(imageDir);
%imdsnew = transform(imds,@fcn,'IncludeInfo',IncludeInfo)
% Create Label Data Store
pxds = pixelLabelDatastore(labelDir,classNames,labelIDs);
% Create Augmenter which applies random reflection/translation/scale
augmenter = imageDataAugmenter('RandXReflection',true,...
'RandXTranslation',[-10 10],'RandYTranslation',[-10 10],'RandXScale',[0.8 1.2]);
% create the datastore for images,labels and augmenter together
pximds = pixelLabelImageDatastore(imds,pxds,'DataAugmentation',augmenter);
%============================================================================================================================================
% Train network - 50 epochs and 10 Augmentations per mask
options = trainingOptions('sgdm','InitialLearnRate',1e-3, ...
'MaxEpochs',1000,'VerboseFrequency',1,'MiniBatchSize',4,'Shuffle','every-epoch','Plots','training-progress','CheckpointPath',checkpointPath );
fprintf('Starting Deep Learning Training.\n');
netMasker128 = trainNetwork(pximds,lgraph,options);
elapsedSeconds = toc;
fprintf('Done with Deep Learning Training at %s, after %.1f minutes.\n', datestr(now), elapsedSeconds/60);
%============================================================================================================================================
% Save the segnet model to a mat file.
fullFileName = sprintf('SegNetMasker.mat');
save(fullFileName, 'netMasker128');

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!