Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SpaceApp2018-Lunar Village People-Lava Tube identification App - Matlab script
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder App. The colorspace and
% minimum/maximum values for each channel of the colorspace were set in the
% App and result in a binary mask BW and a composite image maskedRGBImage,
% which shows the original RGB image values under the mask BW.
% Auto-generated by colorThresholder app on 22-Oct-2018
%------------------------------------------------------
% Convert RGB image to chosen color space
RGB = im2double(RGB);
cform = makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65'));
I = applycform(RGB,cform);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 47.714;
channel1Max = 94.428;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -35.728;
channel2Max = -29.696;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 13.372;
channel3Max = 39.345;
% Create mask based on chosen histogram thresholds
BW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
% Invert mask
BW = ~BW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Matlab script
%
% SpaceApp2018
%
% By: Hernan Giannetta (PhD)
%
% hgiannetta@frba.utn.edu.ar
%
% October 20-21, 2018
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Init
clc;
clear all;
close all;
% Local Variables
ImageIRName = 'qm-lroc-1540099077964.png';
ImageVISName = 'qm-lroc-1540099157713.png';
outputfolderName='output';
Image_Ori='Image_Ori';
Image_VIS_original_filename='Image_VIS_original';
Image_VIS_segmented_filename='Image_VIS_segmented';
%------------------
% Main
%------------------
%------------------
% VIS Image
%------------------
ImageVISarray = imread(ImageVISName);
% Image selection to analize
I=ImageVISarray;
figure, imshow(I), title('original image');
% %I = imread('coins.png');
% level = graythresh(I);
% BW = im2bw(I,level);
% figure,imshow(BW)
%binarize
I_Proc=segmentImage(I);
BW = im2bw(I_Proc);
figure; imshow(BW), title('segmentImage');
% erode
%SE1 = strel('arbitrary',eye(20))
SE1 = strel('disk',2);
BW1 = imerode(BW,SE1);
figure; imshow(BW1), title('erode');
% dilate image
SE2 = strel('disk',7);
BW2 = imdilate(BW1,SE2);
figure; imshow(BW2), title('imdilate');
%edge detection
[~, threshold] = edge(BW2, 'sobel');
fudgeFactor = .5;
BWs = edge(BW2,'sobel', threshold * fudgeFactor);
figure, imshow(BWs), title('binary gradient mask');
%dilate
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BWsdil = imdilate(BWs, [se90 se0]);
figure, imshow(BWsdil), title('dilated gradient mask');
%Fill Interior Gaps
BWdfill = imfill(BWsdil, 'holes');
figure, imshow(BWdfill);
title('binary image with filled holes');
%Remove Connected Objects on Border
BWnobord = imclearborder(BWdfill, 4);
figure, imshow(BWnobord), title('cleared border image');
%Smoothen the Object
seD = strel('diamond',1);
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
figure, imshow(BWfinal), title('segmented image');
% erode image
SE1 = strel('disk',2);
BWfinal2 = imerode(BWfinal,SE1);
figure; imshow(BWfinal2), title('erode');
% dilate image
SE2 = strel('disk',15);
BWfinal3 = imdilate(BWfinal2,SE2);
figure; imshow(BWfinal3), title('imdilate');
%Smoothen2 the Object
seD = strel('diamond',1);
BWfinal = imerode(BWfinal3,seD);
BWfinal = imerode(BWfinal,seD);
figure, imshow(BWfinal), title('segmented image');
BWoutline = bwperim(BWfinal);
Segout = I;
Segout(BWoutline) = 255;
figure, imshow(Segout), title('outlined original image');
%----------------------
% IR IMAGE
%----------------------
ImageIRarray = imread(ImageIRName);
% Image selection to analize
I_IR=ImageIRarray;
figure, imshow(I_IR), title('original image');
% create a mask
[BW,maskedRGBImage] = createMask(I_IR);
figure, imshow(BW), title('create a mask');
% erode image
SE1 = strel('disk',8);
BWfinal2 = imerode(BW,SE1);
figure; imshow(BWfinal2), title('erode');
% dilate image
SE2 = strel('disk',8);
BWfinal3 = imdilate(BWfinal2,SE2);
figure; imshow(BWfinal3), title('imdilate');
%Fill Interior Gaps
BWdfill = imfill(BWfinal3, 'holes');
figure, imshow(BWdfill);
title('binary image with filled holes');
%Smoothen2 the Object
seD = strel('diamond',1);
BWfinal = imerode(BWdfill,seD);
BWfinal = imerode(BWfinal,seD);
figure, imshow(BWfinal), title('segmented image');
% displaying the segmented object
BWoutline_IR = bwperim(BWfinal);
Segout_IR = I_IR;
Segout_IR(BWoutline_IR) = 255;
figure, imshow(Segout_IR), title('outlined original image');
%----------------------
% Arithmetic Image opperation VIS-IR
%----------------------
%Calculate the absolute difference of the two images.
%Fill Interior Gaps
BWdfill = imfill(BWoutline, 'holes');
figure, imshow(BWdfill);
title('binary image with filled holes');
BWdfill_IR = imfill(BWoutline_IR, 'holes');
figure, imshow(BWdfill);
title('binary image with filled holes');
% K = imabsdiff(BWdfill,BWdfill_IR);
% figure, imshow(K), title('Arithmetic Image opperation VIS-IR');
K = imadd(BWdfill,BWdfill_IR);
figure, imshow(K), title('Arithmetic Image opperation VIS-IR');
close all;
% displaying the segmented object
BWoutline_IR_Add = bwperim(K);
Segout_IR = I_IR;
Segout_IR(BWoutline_IR_Add) = 255;
figure, imshow(Segout_IR), title('Lava Tube Identified');
function [BW,maskedImage] = segmentImage(im)
%segmentImage segments image using auto-generated code from imageSegmenter App
% [BW,MASKEDIMAGE] = segmentImage(IM) segments image IM using auto-generated
% code from the imageSegmenter App. The final segmentation is returned in
% BW and a masked image is returned in MASKEDIMAGE.
% Auto-generated by imageSegmenter app on 22-Oct-2018
%----------------------------------------------------
% Convert to grayscale
im = rgb2gray(im);
% Initialize segmentation with threshold
mask = im>1; %(16)
% Evolve segmentation
BW = activecontour(im, mask, 2, 'Chan-Vese');
% Form masked image from input image and segmented image.
maskedImage = im;
maskedImage(~BW) = 0;
end
@LunarVillagePeople
Copy link
Author

LunarVillagePeople commented Oct 22, 2018

Hi all:

In the framework of the Evneto Space App 2018, our team (LunarVillagePeople-Buneos Aires -Argentina) made an application to identify lava tubes on the moon.

The shared code corresponds to the Matlab scripts used to identify the lava tubes on the moon, by processing the images of the moon in the visible and infrared spectrum.

The archives of the processed images will be found on the event page.

By.

Team LunarVillagePeople :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment