Distance between bubble and tube
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rhandrey Maestri
am 17 Sep. 2022
Kommentiert: Image Analyst
am 4 Okt. 2022
hi there!
I am currently doing project on image processing in matlab, in which I'm assigned to calculate the distance between two object automatically. Following the image. Can you tell me how to detect the bubble border(black region) and the tube wall(this black line near the bubble). And then calculate the distance between the border of the bubble and the closest wall pixel near this bubble?
Also if I know that the inside diameter is 15 mm. Can I have this distances in mm? I'd very appreciate your suggestions
Sincrely
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 17 Sep. 2022
Hopefully you tried it and got something like I did:
% Demo by Image Analyst
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 = 22;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'bubble.jpg';
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)
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = grayImage(:, :, 3);
end
% Update 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)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Display the histogram.
subplot(2, 2, 2);
histogram(grayImage(grayImage>0), 256);
grid on;
title('Histogram of Image Gray Levels', 'FontSize', fontSize, 'Interpreter', 'None');
% Threshold the image to get the dark stuff.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
lowThreshold = 0;
highThreshold = 125;
% [lowThreshold, highThreshold, lastThresholdedBand] = threshold(lowThreshold, highThreshold, grayImage)
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Take 5 largest blobs
mask = bwareafilt(mask, 5, 'largest');
% Take 3 smallest blobs
mask = bwareafilt(mask, 3, 'smallest');
% Fill in the bubble so that it will have only 3 sides.
mask = imfill(mask, 'holes');
subplot(2, 2, 3);
imshow(mask)
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Label the mask so that we can get leftmost and rightmost columns of each blob.
labeledImage = bwlabel(mask);
% Scan down getting right edge of left tube wall
% and left edge of right tube wall.
leftTubeEdgeColumn = nan(rows, 1);
rightTubeEdgeColumn = nan(rows, 1);
rightBubbleEdgeColumn = nan(rows, 1);
leftBubbleEdgeColumn = nan(rows, 1);
for row = 1 : rows
% Find left wall of tube.
col = find(labeledImage(row, :) == 1, 1, 'last');
if ~isempty(col)
leftTubeEdgeColumn(row) = col;
end
% Find right wall of tube.
col = find(labeledImage(row, :) == 3, 1, 'last');
if ~isempty(col)
rightTubeEdgeColumn(row) = col;
end
% Find left wall of bubble.
col = find(labeledImage(row, :) == 2, 1, 'first');
if ~isempty(col)
leftBubbleEdgeColumn(row) = col;
end
% Find right wall of bubble.
col = find(labeledImage(row, :) == 2, 1, 'last');
if ~isempty(col)
rightBubbleEdgeColumn(row) = col;
end
end
% Display lines over original image.
subplot(2, 2, 1);
hold on;
y = 1 : rows;
plot(leftTubeEdgeColumn, y, 'r-', 'LineWidth',2);
plot(rightTubeEdgeColumn, y, 'r-', 'LineWidth',2);
plot(leftBubbleEdgeColumn, y, 'r-', 'LineWidth',2);
plot(rightBubbleEdgeColumn, y, 'r-', 'LineWidth',2);
% Get the tube widths
tubeWidths = rightTubeEdgeColumn - leftTubeEdgeColumn;
% Get the mean
meanTubeWidth = mean(tubeWidths);
% It's 15 mm so compute spatial calibration factor.
mmPerPixel = 15 / meanTubeWidth
% Get the widths to the left and right of the bubble in millimeters.
leftWidths = (leftBubbleEdgeColumn - leftTubeEdgeColumn) * mmPerPixel;
rightWidths = (rightTubeEdgeColumn - rightBubbleEdgeColumn) * mmPerPixel;
% Combine to get all widths.
allWidths = [leftWidths; rightWidths];
% Show histogram of widths.
subplot(2, 2, 4);
numberOfBins = 20;
histogram(allWidths, numberOfBins, "Normalization","pdf");
grid on;
title('Histogram of bubble-to-tube spacings', 'FontSize',fontSize);
xlabel('Spacing in millimeters', 'FontSize',fontSize);
ylabel('Relative Frequency', 'FontSize',fontSize);
4 Kommentare
Image Analyst
am 4 Okt. 2022
There is lots of noise in there. Lots of small dark specks. Your original image also had big dark regions on the side, outside the thick dark lines. So I can't just take the largest blob, or 3 largest blobs because I'd get those large outer regions. So I get the 5 largest, and then the 3 smallest to throw out the two large outer regions. Just display the mask after each change in mask and see how it looks as you step through it.
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
imshow(mask)
% Take 5 largest blobs
mask = bwareafilt(mask, 5, 'largest');
imshow(mask)
% Take 3 smallest blobs
mask = bwareafilt(mask, 3, 'smallest');
imshow(mask)
% Fill in the bubble so that it will have only 3 sides.
mask = imfill(mask, 'holes');
subplot(2, 2, 3);
imshow(mask)
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
Weitere Antworten (1)
Image Analyst
am 17 Sep. 2022
Did you try the algorithm I suggested? Looks like not. OK, here try this first.
- Threshold with imbinarize to get black areas as "true" and light areas as false.
- Use bwareafilt(mask, 5) to get the 5 largest blobs.
- Use bwareafilt() again to extract the 3 smallest blobs.
- Call imfill(mask, 'holes');
- Scan down the image row by row finding the edges of the lines and bubble.
- Compute the distances you want. The distance at the top where there is no bubble will let you compute a spatial calibration factor.
I might have time later today if you still need help.
2 Kommentare
Siehe auch
Kategorien
Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!