Detect straight lines from point cloud
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.

採用された回答
Image Analyst
2017 年 12 月 6 日
What I'd do is to
- Crop off or erase all the clutter near the outside edges of the image to leave only crosses.
- Call regionprops to get all the centroids.
- Assuming the crosses are all roughly in horizontal rows, use kmeans() to find 6 centroid clusters from the y centroid values.
- Scan the centroids and get all the centroids within a certain distance of a row.
- Call polyfit() on those centroids.
- call polyval() to get the line all the way across the image.
- 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 件のコメント
Penny
2017 年 12 月 6 日
Thanks very much. It helps a lot. But if I only know the data of these points. Do I need to save them as an image first?
Image Analyst
2017 年 12 月 6 日
I don't know what that means. What is "the data of these points"? I found the centroids of the crosses from an image. Is the image not what you're starting with?
Penny
2017 年 12 月 6 日
Thanks very much. No, I am not starting from the image. I plot the dots using plot function according to the coordinate values of the points. Then I get the figure.
Image Analyst
2017 年 12 月 6 日
Then if you already have the (x,y) coordinates of the crosses, you can skip the first part and just start with the line:
% find 6 means
Penny
2017 年 12 月 6 日
Ok, thank you. But how can I get the xCentroids and yCentroids? The coordinate value are got from other program. I think I need to detect the center points
Image Analyst
2017 年 12 月 6 日
The other program can save the centroid coordinates into a text file, a .mat file, or an Excel workbook.
Penny
2017 年 12 月 7 日
Thanks for your reply. But these dots are exported randomly from a 3D model. Thus, the other program did not know the exact coordinates of the centroids. I think this is the key point to detect the centroids from a set of random data. Do you know how to implement it?
Image Analyst
2017 年 12 月 7 日
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
2017 年 12 月 8 日
kmeans works well. Thank you very much.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Process Point Clouds についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
