![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/526554/image.png)
Distance between several defects/points
7 views (last 30 days)
Show older comments
Faiz Yusoff
on 21 Feb 2021
Commented: Faiz Yusoff
on 14 Mar 2021
Hello there, i currently doing researach on estimating distance between points. i need help with my code. I need to get the size measurement (diameter) and the distance between each of the defects/ point (pixel convert to mm) in the thermal image and it must be the same as the original real measurement as attached
I used colour threshold in matlab tools to get the defects. I stucked at gray image, region props and function of pdist. thank you, hope to hear feedback.
This is the thermal image
and the real measurement ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/526424/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/526419/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/526424/image.png)
here is the error ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/526429/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/526429/image.png)
the coding looks so not right i know
This is my current code
% read the original image
I = imread('defects1.png');
% call createMask function to get the mask and the filtered image
[BW,maskedRGBImage] = createMask(I);
% plot the original image, mask and filtered image all in one figure
%subplot(1,3,1);imshow(I);title('Original Image');
%subplot(1,3,2);
imshow(BW);title('Mask');
%subplot(1,3,3);imshow(maskedRGBImage);title('Filtered Image');
mask = grayImage > someValue;
mask = bwareafilt(mask, 13); % Take 13 largest blobs.
imshow(mask);
hold on;
props = regionprops(mask, 'Centroid');
xy = certcat(props.Centroid);
for k = 1 : size(xy, 1)
caption = sprintf(' %d', k);
plot(xy(k, 1), xy(k, 2), 'r+', 'MarkerSize', 20);
text(caption, xy(k, 1), xy(k, 2), 'Color', 'r', 'FontWeight', 'bold', 'FontSize', 20);
end
distances = pdist2(xy, xy)
%distancesInPixels = pdist2(xy, xy);
%distancesInCm = distancesInPixels * cmPerPixel;
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 18-Feb-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.049;
channel1Max = 0.160;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.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
0 Comments
Accepted Answer
Image Analyst
on 21 Feb 2021
Try this:
% Initialization steps.
clc; % Clear the command window.
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 = 13;
% read the original image
rgbImage = imread('defects1.png');
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage);
axis('on', 'image');
title('Original RGB Image', 'FontSize', fontSize);
impixelinfo;
% call createMask function to get the mask and the filtered image
[mask,maskedRGBImage] = createMask(rgbImage);
% Display the image.
subplot(2, 2, 2);
imshow(mask);
axis('on', 'image');
impixelinfo;
title('Mask', 'FontSize', fontSize);
subplot(2, 2, 3);
% Take 13 largest blobs.
mask = bwareafilt(mask, 13);
imshow(mask);
hold on;
% Make measurements.
props = regionprops(mask, 'Centroid');
xy = vertcat(props.Centroid);
for k = 1 : size(xy, 1)
x = xy(k, 1);
y = xy(k, 2);
caption = sprintf(' %d', k);
plot(x, y, 'r+', 'MarkerSize', 20);
text(x, y, caption, 'Color', 'r', 'FontWeight', 'bold', 'FontSize', 20);
end
g = gcf;
g.WindowState = 'maximized';
distances = pdist2(xy, xy)
%distancesInPixels = pdist2(xy, xy);
%distancesInCm = distancesInPixels * cmPerPixel;
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 18-Feb-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.049;
channel1Max = 0.160;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/526554/image.png)
18 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!