Distance between several defects/points

4 vues (au cours des 30 derniers jours)
Faiz Yusoff
Faiz Yusoff le 21 Fév 2021
Commenté : Faiz Yusoff le 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
here is the error
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

Réponse acceptée

Image Analyst
Image Analyst le 21 Fév 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
  18 commentaires
Faiz Yusoff
Faiz Yusoff le 14 Mar 2021
So maybe i just put the variable into the code like this;
mmPerPixel = 1.716565;
distancesInMM = distances * mmPerPixel;
xInMM = x * mmPerPixel;
yInMM = y * mmPerPixel;
allAreasInMM = allAreas * mmPerPixel;
xyInMM = xy * mmPerPixel;
Faiz Yusoff
Faiz Yusoff le 14 Mar 2021
oh i think i made mistake at areas.. Should just follow your code.
How about the xyInMM, is that correct?

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by