Filling gaps in between lines to make it a continuous line

8 views (last 30 days)
Can anyone suggest a method ... How to fill the gaps in between the small edge detected lines in the image to make it a continuous line. Here is my image link

Accepted Answer

Image Analyst
Image Analyst on 18 Oct 2011
As you requested, here's my demo for how to use createMask:
% Demo to write an ellipse and a line into the overlay of an image,
% and then to burn those overlays into the image.
%----- Initializing steps -----
% Clean up
clc;
clear all;
close all;
workspace; % Display the workspace panel.
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Display images to prepare for the demo.
monochromeImage = imread('pout.tif');
subplot(2, 4, 1);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 2);
imshow(monochromeImage);
title('Original Image with ellipse in overlay');
subplot(2, 4, 5);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 6);
imshow(monochromeImage);
title('Original Image with line in overlay');
set(gcf, 'Position', get(0, 'ScreenSize')); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
%----- Burn ellipse into image -----
% Create elliptical mask, h, as an ROI object over the second image.
subplot(2, 4, 2);
hEllipse = imellipse(gca,[10 10 50 150]); % Second argument defines ellipse shape and position.
% Create a binary image ("mask") from the ROI object.
binaryImage = hEllipse.createMask();
% Display the ellipse mask.
subplot(2, 4, 3);
imshow(binaryImage);
title('Binary mask of the ellipse');
% Let's try to add some text. (Doesn't work)
% hText = text(50, 100, 'Line of Text');
% textMask = hText.createMask();
% binaryImage = binaryImage & textMask;
% imshow(binaryImage);
% Burn ellipse into image by setting it to 255 wherever the mask is true.
monochromeImage(binaryImage) = 255;
% Display the image with the "burned in" ellipse.
subplot(2, 4, 4);
imshow(monochromeImage);
title('New image with ellipse burned into image');
%----- Burn line into image -----
burnedImage = imread('pout.tif');
% Create line mask, h, as an ROI object over the second image in the bottom row.
subplot(2, 4, 6);
hLine = imline(gca,[10 100],[10 100]); % Second argument defines line endpoints.
% Create a binary image ("mask") from the ROI object.
binaryImage2 = hLine.createMask();
% Display the line mask.
subplot(2, 4, 7);
imshow(binaryImage2);
title('Binary mask of the line');
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage(binaryImage2) = 255;
% Display the image with the "burned in" line.
subplot(2, 4, 8);
imshow(burnedImage);
title('New image with line burned into image');
  5 Comments
Stelios Fanourakis
Stelios Fanourakis on 10 Aug 2019
Image Analyst, I need to do exactly the same thing as your code above does but in an automatic way to trace specific pixel arrays and outline them, creating a convex hull. You can check my latest question from my account for more details.
Duaa Abu Sadaa
Duaa Abu Sadaa on 23 Jun 2022
Edited: Duaa Abu Sadaa on 23 Jun 2022
Hello, I am trying to fill the gaps in a Landsat 7 image on matlab, but I could not write an appropriate code to do so, can you please help in case you have idea about it.
Thanks

Sign in to comment.

More Answers (3)

Image Analyst
Image Analyst on 15 Oct 2011
Skeletonize the white blobs. Then find the endpoints with bwmorph(,'endpoints'). Connect each endpoint to the closest other endpoint that it is not already connected to by using imline.createmask(). Write back if you need a demo on using imline to write lines into the image.
  5 Comments
Walter Roberson
Walter Roberson on 15 Oct 2011
Ah! He could feed in the coordinates of the existing pixels on both ends (or a subset of that), run a cubic spline, and use that to interpolate the connecting line segment. Smoothness solved!
Gova ReDDy
Gova ReDDy on 18 Oct 2011
Does the usage of *cubic spline* will overwrite the existing line or else it fill only the gaps in the line ?
Can you provide some information of how to apply it to line after getting the end points of the line.

Sign in to comment.


Walter Roberson
Walter Roberson on 14 Oct 2011
Depending on how large of a gap you want to fill, imdilate() might be appropriate.

Dr. Seis
Dr. Seis on 18 Oct 2011
If you want to do this in 2D, then I think using "interp2" with the "spline" option will only work for input located on a regular spaced grid. I don't think it is designed to work if there is any irregularity or are any "holes" in the data. However, the solution posted on:
will allow one to run a spline (albeit a biharmonic spline) through data located on an irregular grid (or a regular grid with "holes").
If you ask for the output from the function (given on the page above) to fall on a regular grid corresponding to the same locations as the input data, then those points should be exactly the same and should only fill in the "holes" in your data. This should be the case with any spline for any dimension.
  3 Comments
Stelios Fanourakis
Stelios Fanourakis on 10 Aug 2019
I need this info as well. How to fill gaps with imline.createmask() after finding the endpoints using bwmorph

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!