Detect straight lines from point cloud
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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?
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.
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
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?
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에서 k-Means and k-Medoids Clustering에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
