Filling gaps in between lines to make it a continuous line

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

 Respuesta aceptada

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 comentarios

I got this error, pls help:
Error in imline (line 64) hLine = imline(gca,[10 100],[10 100]); % Second argument defines line endpoints.
You forgot to give the actual error message. Probably, you don't have the Image Processing Toolbox. Type ver on the command line to check if you have it.
I have Image Processing Toolbox Version 8.0 still can't run the full demo, because the error in line 64
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 el 23 de Jun. de 2022
Editada: Duaa Abu Sadaa el 23 de Jun. de 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

Iniciar sesión para comentar.

Más respuestas (3)

Image Analyst
Image Analyst el 15 de Oct. de 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 comentarios

I did consider the possibility of connecting the endpoints, but then I noticed that the lines being joined are not really straight, and I got side-tracked into considerations of extending the existing curvature to reach the endpoint, and considerations of smoothness of the transition to the joining segment (probably obvious corners at the join are not very desirable.)
I think it was about at that point that my mind said, ".... Hmmmm, Donut!!" and promptly discarded the train of thought on the grounds that it was interfering with the procurement of junk food. ;-)
Yeah, you're right. It may or may not be an issue with him.
True. There _are_ some people immune to the siren call of donuts. :)
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!
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.

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 14 de Oct. de 2011
Depending on how large of a gap you want to fill, imdilate() might be appropriate.
Dr. Seis
Dr. Seis el 18 de Oct. de 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 comentarios

I just want to fill the gaps in between the line segments..
So,Image Anlayst can you explain how to use imline.createmask() after finding the endpoints using bwmorph
I need this info as well. How to fill gaps with imline.createmask() after finding the endpoints using bwmorph
See attached demo and adapt as needed.

Iniciar sesión para comentar.

Preguntada:

el 14 de Oct. de 2011

Editada:

el 23 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by