Calculate intensity of bright and dark blobs in the image

조회 수: 8 (최근 30일)
Himanshu Mishra
Himanshu Mishra 2023년 5월 8일
편집: Image Analyst 2023년 5월 8일
In the attached image, I want to calculate the average intensity of the bright blobs as well as the dark blobs inside the bright blobs.
For example, in this image, there are 8 bright blobs. In top row, third from the left blob, there are 4 dark spots. I need to calculate the average intensity of the 8 bright blobs along with the average intensity of the dark spots inside those bright blobs.
Any help would be appreciated.

채택된 답변

Image Analyst
Image Analyst 2023년 5월 8일
편집: Image Analyst 2023년 5월 8일
It's a generic, general purpose demo of how to threshold an image to find blobs, and then measure things about the blobs, and extract certain blobs based on their areas or diameters.
If you have any more questions, then attach your image HERE (not some third party web site that we can't even get to) with the paperclip icon after you read this:
Basically
mask = grayImage > someThresholdValue;
mask = bwareafilt(mask, 8); % Take 8 largest.
meanBrightIntensity = mean(grayImage(mask))
Similar for dark blobs:
mask = grayImage < someDarkThresholdValue;
mask = bwareafilt(mask, 4); % Take 4 largest.
meanDarkIntensity = mean(grayImage(mask))
If you want to interactively/visually select the threshold, see my thresholding utility:

추가 답변 (1개)

Image Analyst
Image Analyst 2023년 5월 8일
Try this:
% 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 = 18;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = pwd;
baseFileName = 'Image009_ch00_SV.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);
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Green Channel Image', 'FontSize', fontSize, 'Interpreter', 'None');
% 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.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the max channel.
% grayImage = max(grayImage, [], 3);
% Extract the green channel.
grayImage = grayImage(:, :, 2);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 2);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Green Channel Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Threshold to create mask of the bright spots
lowThreshold = 31;
highThreshold = 255;
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage)
brightMask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Take the largest 8 blobs only.
brightMask = bwareafilt(brightMask, 8);
subplot(2, 2, 3);
imshow(brightMask);
impixelinfo;
axis('on', 'image');
title('Bright Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get the mean gray level.
meanBrightGrayLevel = mean(grayImage(brightMask));
caption = sprintf('Mean of bright areas = %.1f gray levels.', meanBrightGrayLevel)
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Get the convex hull of hte bright blobs so we can restrict the dark areas to within the bright areas.
brightMask = bwconvhull(brightMask, "objects");
grayImage(~brightMask) = 0; % Erase outside of the bright areas.
%--------------------------------------------------------------------------------------------------------
% Threshold to create mask of the dark spots
lowThreshold = 18;
highThreshold = 26;
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage)
darkMask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Take the largest 4 blobs only.
darkMask = bwareafilt(darkMask, 4);
% Fill any holes inside the dark blobs.
darkMask = imfill(darkMask, 'holes');
subplot(2, 2, 4);
imshow(darkMask);
impixelinfo;
axis('on', 'image');
title('Dark Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get the mean gray level.
meanDarkGrayLevel = mean(grayImage(darkMask));;
caption = sprintf('Mean of dark areas = %.1f gray levels.', meanDarkGrayLevel)
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
It will require adaptation of course.
  댓글 수: 4
Himanshu Mishra
Himanshu Mishra 2023년 5월 8일
I had actually reached this point using your image segmentation tutorial. The next step is where I am stuck. How to individually count the blobs and the dark spots inside them. You have indeed helped a lot. I will keep trying. Thanks.
Image Analyst
Image Analyst 2023년 5월 8일
편집: Image Analyst 2023년 5월 8일
Well since you said there were 8 and 4, I extracted exactly that many blobs. If there is some variable number, then you might want to change bwareafilt to give it some size range of valid blob areas. Then it could find however many there are instead of some fixed number.
You can also use bwareaopen to throw out blobs smaller than a certain number of pixels in area. What is the smallest genuine blob you want to consider?

댓글을 달려면 로그인하십시오.

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by