spot diameter in imported image

4 次查看(过去 30 天)
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 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2020-9-9
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 2020-9-9
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

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2020-9-9
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 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020-9-9
编辑:KALYAN ACHARJYA 2020-9-9
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 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2020-9-9
编辑:KALYAN ACHARJYA 2020-9-9
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 2020-9-9
Thank u its really helpful!!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by