Detect straight lines from point cloud

10 views (last 30 days)
Hi everyone. I have a set of point cloud. I want to detect the straight lines from the point cloud. Here is the figure of the point cloud. How do I detect the green lines? Thanks very much.

Accepted Answer

Image Analyst
Image Analyst on 6 Dec 2017
What I'd do is to
  1. Crop off or erase all the clutter near the outside edges of the image to leave only crosses.
  2. Call regionprops to get all the centroids.
  3. Assuming the crosses are all roughly in horizontal rows, use kmeans() to find 6 centroid clusters from the y centroid values.
  4. Scan the centroids and get all the centroids within a certain distance of a row.
  5. Call polyfit() on those centroids.
  6. call polyval() to get the line all the way across the image.
  7. Call "hold on" and plot() to plot the line over the image.
Here's a start:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
%===============================================================================
% Get the name of the first image the user wants to use.
baseFileName = '2_Ink_LI.jpg';
folder = fileparts(which(baseFileName)); % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 1); % Take red channel.
end
% Erase edges
grayImage([1:150, 750:end], :) = 255;
grayImage(:, [1:150, 750:end]) = 255;
% Display the image.
subplot(2, 2, 2);
imshow(grayImage, []);
axis on;
caption = sprintf('Gray Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% % Display the histogram of the image.
% subplot(2, 2, 3);
% histogram(grayImage, 256);
% caption = sprintf('Histogram of Gray Image');
% title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% grid on;
% drawnow;
%=======================================================================================
binaryImage = grayImage < 128;
% Display the masked image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;
% Get the bounding box of all cups
props = regionprops(binaryImage, 'Centroid');
centroids = [props.Centroid]
xCentroids = centroids(1:2:end)';
yCentroids = centroids(2:2:end)';
% find 6 means
[indexes, rowcenters] = kmeans(yCentroids, 6)
% Plot lines at centers
hold on;
for k = 1 : length(rowcenters)
line([1, columns], [rowcenters(k), rowcenters(k)], 'Color', 'r');
end
% Display the original image.
subplot(2, 2, 4);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
% Fit lines though each point.
xFit = 1 : columns;
for k = 1 : length(rowcenters)
% Find out which points belong to class k.
theseIndexes = indexes == k;
theseX = xCentroids(theseIndexes)
theseY = yCentroids(theseIndexes)
coefficients = polyfit(theseX, theseY, 1);
yFit = polyval(coefficients, xFit);
plot(xFit, yFit, 'g-');
end
  9 Comments
Image Analyst
Image Analyst on 7 Dec 2017
Not without some real data. Why did you post that image if it had nothing at all to do with the data? There are lots of clustering and classification routines in MATLAB. Why don't you try knnsearch() or classify() or kmeans(). It really depends on what form your data are in and whether you know how many clusters there are or if you have no idea how many clusters there are or could be.
Penny
Penny on 8 Dec 2017
kmeans works well. Thank you very much.

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!