MATLAB Answers

spot diameter in imported image

6 views (last 30 days)
adi
adi on 9 Sep 2020
Commented: adi on 9 Sep 2020
hello,
so i have an IR Laser spot image i captured and i want to calculate its diameter.
the diameter should be calculated from where pixels values are equal 13% from the max pixel value.
so the spot contains only pixels that their values are greater than 13%* max value
how can i do this?
here is an example of what i have:
Thank you!

  2 Comments

KALYAN ACHARJYA
KALYAN ACHARJYA on 9 Sep 2020
That menas we can consider only those pixels having 13% from the max pixel value, suppose the centre pixel is maximum, which is 255, then outer boundary of the spot consider untill the pixel value greater than 255x13/100, right? Are that for all directions, or any one, which may satisfy the condition first one?
adi
adi on 9 Sep 2020
It will be satisfying to have the calculations for x,y directions. Outer boundary is:outside the spot boundary are pixels with value smaller than 13%*max. And inside are pixels with bigger value than 13%*max

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 9 Sep 2020
Try this:
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;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'laserSpot.jpeg';
% Get the full filename, with path prepended.
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);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
% This image has JPEG artifacts that make the top and left columns be artificially high.
% Erase those parts. Might not have to do this if the image is not JPG format.
grayImage(1:3, :) = 0; % Blacken top 3 rows.
grayImage(:, 1:3) = 0; % Blacken left 3 columns.
% Display histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of original gray image', 'FontSize', fontSize);
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION OF IMAGE
% Get max value
maxGL = max(grayImage(:))
threshold = 0.13 * maxGL;
mask = grayImage > threshold;
mask = imfill(mask, 'holes');
% Extract only largest blob.
mask = bwareafilt(mask, 1, 4); % Connectivity of 4
% Display the mask image.
subplot(2, 2, 3);
imshow(mask);
caption = sprintf('Mask with a threshold of %.1f, (13%% of %d)', threshold, maxGL);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Measure Area and Equivalent Circular Diameter.
props = regionprops(mask, 'Area', 'EquivDiameter', 'Centroid');
allAreas = [props.Area]
ecd = props.EquivDiameter % Equivalent circular diameter.
% Display the original image again so we can overlay graphics on it.
subplot(2, 2, 4);
imshow(grayImage, []);
caption = sprintf('Original Image with Overlays. ECD = %.1f pixels', ecd);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Plot boundary of mask over image.
boundaries = bwboundaries(mask);
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
hold on;
plot(x, y, 'g-', 'LineWidth', 2);
end
% Show a circle on it.
viscircles([props.Centroid(1), props.Centroid(2)], ecd/2);

  1 Comment

adi
adi on 9 Sep 2020
Wow.. works amazing thanks!!

Sign in to comment.

More Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 9 Sep 2020
Edited: KALYAN ACHARJYA on 9 Sep 2020
Here are the all pixels, which are greater than 13%*max
image_data=rgb2gray(imread('image_test5.jpeg'));
max_pix=max(image_data(:));
dia_data=image_data>max_pix*0.13;
image_data(~dia_data)=255;
imshow(image_data);
What Next?

  3 Comments

adi
adi on 9 Sep 2020
hi, thank you very much for your answer.
but how can i calculate the diameter of this?
KALYAN ACHARJYA
KALYAN ACHARJYA on 9 Sep 2020
One way: 1. Get the Euclidian distance in multiple directions (Centre to Boundary)
2. Average the Distances
Can we consider that as the radius of the spot.
Note: It's just an appoximation, as there are no distinct boundaary which we can consider as the boundary of the of laser spot.
Please see the answer provided by @Image Analyst Sir
adi
adi on 9 Sep 2020
Thank u its really helpful!!

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!