How to detect point on a surface?
Ältere Kommentare anzeigen
Hello,
Performing mechanical tests, I take pictures of the a component after a fixed period of time (e.g. 10min). I've drawn two points on the surface of the component using a common marker and I would like to evaluate how this distance changes in all the pictures taken.
I've taken into account two approaches:
- Morphlogical (erosion and dilation) extracting object centroids using regionprops;
- Colour based segmentation: k-means method and extracting object centroids using regionprops.
Both methods extract the two regions and evaluate the centroids and their distance. The main problem I'm facing is the scatter in results due to the different contours underlined in each photo. Is a better approach available to solve my problem? Which could be the maximum accuracy achievable?
Algorithm for morphological approach:
- Image cropping imcrop;
- Anisotropic filtering;
- Converting to grayscale;
- imhistmatch considering the first photo as a refernce;
- Gamma correction;
- Constrast enhancement using adapthisteq;
- Thresholding using Otsu's method;
- Binarization;
- bwareaopen/bwareafilt to remove noise objects;
- extraction of objects using regionprops.
Pre-prcessing algorithm for k-means:
- Image cropping imcrop;
- imhistmatch considering the first photo as a refernce;
- anisotropic filtering;
- gamma correction;
- Contrast enhancement following the example in the link for rgb images;
- extraction of objects centroid using regionprops.

To reduce the scatter, I've tried to paint the surface before making the red dots, however I don't get much better results.

I hope you can help me and thank you in advance.
Giuseppe
Antworten (2)
Afsoon Hatami
am 13 Dez. 2020
0 Stimmen
Hi..have you found your answer? I have the same problem and I want to find the path of a droplet in different images
1 Kommentar
Image Analyst
am 13 Dez. 2020
Doesn't look too hard. Pretty easy in fact if the drop is red and the background is gray. Start a new question and attach your original images (at least two so we can detect how far the centroid moved).
Image Analyst
am 13 Dez. 2020
Bearbeitet: Image Analyst
am 13 Dez. 2020
Doesn't look too hard. Pretty easy in fact if the drop is red and the background is gray. Attach your original images (at least two so we can detect how far the centroid moved). Try the Color Thresholder on the Apps tab of the tool ribbon to do the initial color segmentation.
% Demo to find red droplets on a gray background.
% By Image Analyst, December 8, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image of colorbar.
folder = [];
baseFileName = 'red spots.png';
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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image full size.
subplot(2, 1, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Reference Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% 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.
hFig1.Name = 'Demo by Image Analyst';
% Mask to find the red blobs.
[mask, maskedRGBImage] = createMask(rgbImage);
% Take 2 largest blobs.
mask = bwareafilt(mask, 2);
% Fill holes.
mask = imfill(mask, 'holes');
% Display the test image full size.
subplot(2, 1, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf('Mask Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Measure area, diameter, and centroid.
props = regionprops(mask, 'Area', 'EquivDiameter', 'Centroid');
% Get the areas
allAreas = [props.Area]
allDiameters = [props.EquivDiameter]
xy = vertcat(props.Centroid)
% Plot the centroids over the mask image.
hold on;
for k = 1 : length(props)
x(k) = props(k).Centroid(1);
y(k) = props(k).Centroid(2);
plot(x(k), y(k), 'r+', 'lineWidth', 2, 'markerSize', 50);
end
fprintf('Done running %s.m ...\n', mfilename);
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 13-Dec-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = RGB;
% Define thresholds for channel 1 based on histogram settings
channel1Min = 63.000;
channel1Max = 255.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 68.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 71.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

Kategorien
Mehr zu Agriculture finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!