Cone angle of spray

5 views (last 30 days)
Lucio Cercone
Lucio Cercone on 28 Mar 2019
Edited: Image Analyst on 16 Jul 2021
Hello to everybody,
I want to calculate spray angle of this imageprova.JPG and i use this code (that I found here) but this code evaluates an angle of 0 degrees. Can someone help me???? This is the final image prova_figura_matlab.jpg
function test
clc;
close all;
workspace; % Make sure the workspace panel with all the variables is showing.
format longg;
format compact;
fontSize = 18;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a standard MATLAB color demo image.
folder = pwd;
baseFileName = 'prova.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% If it's really color, then convert to gray scale.
grayImage = grayImage(:,:,2);
end
% Display the original image.
subplot(2, 3, 1);
imshow(grayImage);
axis on;
title('Original Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
% Suppress bins at 0 and 255 because they're so tall we can't see the rest of it.
pixelCount(1) = 0;
pixelCount(end) = 0;
subplot(2, 3, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% [lowThreshold, highThreshold, lastThresholdedBand] = threshold(133, 255, grayImage);
% Threshold the image
thresholdValue = 50;
binaryImage = grayImage < thresholdValue;
% Fill holes
% binaryImage = imfill(binaryImage, 'holes');
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage);
axis on;
title('Binary Image', 'FontSize', fontSize);
%---------------------------------------------------------------------------
% Extract the largest area using our custom function ExtractNLargestBlobs().
numberToExtract = 1;
binaryImage = ExtractNLargestBlobs(binaryImage, numberToExtract);
%---------------------------------------------------------------------------
% % Fill any holes that might be present
binaryImage = imfill(binaryImage, 'holes');
% Do an opening to smooth out the edges.
se = strel('disk', 3, 0);
binaryImage = imopen(binaryImage, se);
% Display the image.
subplot(2, 3, 4);
imshow(binaryImage);
axis on;
title('Biggest Blob', 'FontSize', fontSize);
drawnow;
% Go down the image line by line finding the width
widths = zeros(1, rows);
leftEdge = zeros(1, rows);
rightEdge = zeros(1, rows);
for row = 1 : rows
thisRow = binaryImage(row, :);
leftIndex = find(thisRow, 1, 'first');
if ~isempty(leftIndex)
% There's something there
leftEdge(row) = leftIndex;
rightEdge(row) = find(thisRow, 1, 'last');
widths(row) = rightEdge(row) - leftEdge(row);
end
end
% plot the widths
subplot(2, 3, 5);
plot(1:rows, widths, 'b-', 'LineWidth', 2);
grid on;
title('Widths as a function of line number', 'FontSize', fontSize);
axis on;
% Now find min width at top and max width.
% This could be an algorithm all by itself, but since this is just a simple demo,
% I'm going to hard code in values that I can see from the plot of the widths.
% I'm going to fit between rows 10 and 60
x = 10:60;
% Get the left angle.
y = leftEdge(x);
leftCoefficients = polyfit(x, y, 1);
leftAngle = atand(leftCoefficients(1))
subplot(2, 3, 6);
plot(x, y, 'b-', 'LineWidth', 2);
hold on;
y = rightEdge(x);
rightCoefficients = polyfit(x, y, 1);
rightAngle = atand(rightCoefficients(1))
plot(x, y, 'r-', 'LineWidth', 2);
grid on;
coneAngle = abs(leftAngle) + abs(rightAngle);
% Plot the fitted lines
yLeftFit = polyval(leftCoefficients, x);
plot(x, yLeftFit, 'b-', 'LineWidth', 2);
yRightFit = polyval(rightCoefficients, x);
plot(x, yRightFit, 'r-', 'LineWidth', 2);
legend('left', 'right', 'Location', 'east');
message = sprintf('Done with demo.\nThe cone angle is %.1f degrees.', coneAngle);
uiwait(helpdlg(message));
%==============================================================================================
% Function to return the specified number of largest or smallest blobs in a binary image.
% If numberToExtract > 0 it returns the numberToExtract largest blobs.
% If numberToExtract < 0 it returns the numberToExtract smallest blobs.
% Example: return a binary image with only the largest blob:
% binaryImage = ExtractNLargestBlobs(binaryImage, 1);
% Example: return a binary image with the 3 smallest blobs:
% binaryImage = ExtractNLargestBlobs(binaryImage, -3);
function binaryImage = ExtractNLargestBlobs(binaryImage, numberToExtract)
try
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
[labeledImage, numberOfBlobs] = bwlabel(binaryImage);
blobMeasurements = regionprops(labeledImage, 'area');
% Get all the areas
allAreas = [blobMeasurements.Area];
if numberToExtract > length(allAreas);
% Limit the number they can get to the number that are there/available.
numberToExtract = length(allAreas);
end
if numberToExtract > 0
% For positive numbers, sort in order of largest to smallest.
% Sort them.
[sortedAreas, sortIndexes] = sort(allAreas, 'descend');
elseif numberToExtract < 0
% For negative numbers, sort in order of smallest to largest.
% Sort them.
[sortedAreas, sortIndexes] = sort(allAreas, 'ascend');
% Need to negate numberToExtract so we can use it in sortIndexes later.
numberToExtract = -numberToExtract;
else
% numberToExtract = 0. Shouldn't happen. Return no blobs.
binaryImage = false(size(binaryImage));
return;
end
% Extract the "numberToExtract" largest blob(a)s using ismember().
biggestBlob = ismember(labeledImage, sortIndexes(1:numberToExtract));
% Convert from integer labeled image into binary (logical) image.
binaryImage = biggestBlob > 0;
catch ME
errorMessage = sprintf('Error in function ExtractNLargestBlobs().\n\nError Message:\n%s', ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end

Answers (1)

Image Analyst
Image Analyst on 16 Jul 2021
Edited: Image Analyst on 16 Jul 2021
That's code I wrote for another question. You probably need to invert your mask:
binaryImage = grayImage > thresholdValue;

Community Treasure Hunt

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

Start Hunting!