How to measure at the thread of a bolt?

조회 수: 4 (최근 30일)
Rick Verschuuren
Rick Verschuuren 2020년 10월 1일
댓글: Rick Verschuuren 2020년 10월 5일
hi,
I am with fellow students working on an vision application project for school.
We are using an industrial camera and lens. The biggest issues we are having is the algoritem to measure at the bolt. We need to measure the thread pitch, diameter and the angel of an M10 metric bold. We already have the edges detected using an sobel operation, but how can we pin point the location of the tops of the threads and measure how many pixels are between those points.
We are looking forward to a reply.
Kind regards,
Sander, Bjorn and Rick.
Avans University Breda

채택된 답변

Image Analyst
Image Analyst 2020년 10월 1일
편집: Image Analyst 2020년 10월 1일
What sort of help do you want? I assume you don't want us to just do this project for you and hand it over for you to turn in. What do you want me to do? Approve your algorithm? What is your algorithm? First I'd scan down the rows finding the left edge and right edge of the threads. Then I'd fit a line through them and rotate the image so that the main axis is along a single column. Then I'd scan down again getting the left and right edge. Then I'd use findpeaks(). It should be rather straightforward after that to determine pitch and angle. That is all pretty obvious so you probably already have done that. Not sure, really, what you want or need.
By the way, you don't need the edge detection image at all. That you need to do an edge detection on every image is just a myth propagated by novices who don't know any better. You don't need it here, so don't do it.
I believe there have been similar projects here using rope instead of bolts, so you might search for rope.
  댓글 수: 3
Image Analyst
Image Analyst 2020년 10월 3일
See if this is enough to jumpstart you:
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'binary.png';
% Get the full filename, with path prepended.
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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
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(grayImage);
% 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 = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
% This image needs to be inverted so that the bolt is white
mask = ~imbinarize(grayImage);
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION OF IMAGE
% Fill Holes
mask = imfill(mask, 'holes');
% Extract only largest blob.
mask = bwareafilt(mask, 1, 4); % Connectivity of 4
% Erase down to line 248.
mask(1:248, :) = false;
% Display the mask image.
subplot(2, 2, 2);
imshow(mask);
caption = sprintf('Mask');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Scan down line-by-line to get the left edge and right edge
leftColumns = zeros(rows, 1);
rightColumns = zeros(rows, 1);
for row = 1 : rows
thisRow = mask(row, :);
t = find(thisRow, 1, 'first');
if isempty(t)
% No white found
continue;
end
leftColumns(row) = t;
rightColumns(row) = find(thisRow, 1, 'last');
end
% Delete rows with no white and keep those with white.
goodRows = leftColumns > 0;
leftColumns = leftColumns(goodRows);
goodRows = rightColumns > 0;
rightColumns = rightColumns(goodRows);
% Plot over image.
hold on;
yRight = find(goodRows);
yLeft = find(goodRows);
plot(leftColumns, yLeft, 'r-', 'LineWidth', 2);
plot(rightColumns, yRight, 'r-', 'LineWidth', 2);
% First a line to the left side
xLeft = find(goodRows);
yLeft = leftColumns;
coefficientsLeft = polyfit(xLeft, yLeft, 1);
% Get the fitted line
xFitLeft = 1:rows;
yFitLeft = polyval(coefficientsLeft, xFitLeft);
plot(yFitLeft, xFitLeft, 'm-', 'LineWidth', 2);
% First a line to the right side
xRight = find(goodRows);
yRight = rightColumns;
coefficientsRight = polyfit(xRight, yRight, 1);
% Get the fitted line
xFitRight = 1:rows;
yFitRight = polyval(coefficientsRight, xFitRight);
plot(yFitRight, xFitRight, 'm-', 'LineWidth', 2);
% Get the average slopt of each line
slopeLeft =
slopeRight =
averageSlope = mean([slopeLeft, slopeRight]);
angle = atan(averageSlope);
% Rotate image.
mask = imrotate(mask, angle, 'nearest');
% Display the mask image.
subplot(2, 2, 3);
imshow(mask);
caption = sprintf('Mask Rotated by %f degrees', angle);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
fprintf('Done running %s.m ...\n', mfilename);
Rick Verschuuren
Rick Verschuuren 2020년 10월 5일
Thank you for your reply we are going to look in to it!
kind regards

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by