Average Hue for Multiple ROIs

3 views (last 30 days)
Matthew
Matthew on 29 Jan 2024
Commented: Matthew on 29 Jan 2024
Hi,
I'm learning MATLAB on the fly within my job, so am very much a novice. I have an image of 9x circles in a 3x3 fashion. So far I have created a binary mask and have applied it to disregard the background and to only include these circles. From here I converted my new image from RGB to a HSV image, and separated the individual component.
I was able to determine an average Hue value, but this is for the entire image. It would be more useful if I could get Hue values for each of the 9 individual circles. Is there a way to do this without me creating individual masks and applying them seperately?
I've attached part of the code below which I've seen in a previous answer, however is errors for the "isInsideCircle = distances <= Rad ;" line due to "Matrix dimensions must agree"
N.B. The images are screengrabs so please ignore the inconsistency of dimensions and positions. I added them just as a visual aid.
Variables
Rad = [110;110...] - This is the radius for all 9 circles
k=1:9 (Number of circles)
C(k,1) and C(k,2) are the X and Y coordinates for Circle k's centre. - This array has all coordinates for 9 circle centres.
XCo and YCo are the dimensions of my image (3712x5568)
for k = 1:numel(Rad)
ROI2 = images.roi.Circle(gca); % create an ROI object
ROI2.Radius = Rad(k); % specify its size and location
ROI2.Center = C(k,:);
for XCo = 1:3712
for YCo = 1:5568
distances(XCo,YCo) = sqrt( (XCo - C(k,1)).^2 + (YCo - C(k,2)).^2 );
isInsideCircle = distances <= Rad ;
counts = isInsideCircle ;
end
end
end

Answers (1)

Milan Bansal
Milan Bansal on 29 Jan 2024
Edited: Milan Bansal on 29 Jan 2024
Hi Matthew,
I understand that you want to calculate the average hue value for each of the 9 circles in the image without creating the individual masks and applying them separately. Also
It is required to calculate the individual mask to get the average hue values for each circle, however "meshgrid" can be used to calculate the distances of pixels from the centre of each circle instead of iterating through all the X and Y coordinates in the entire image.
Please refer to the steps and pseudo code in the code snippet below:
% Extract the hue channel from the HSV Image
hueChannel = hsvImg(:,:,1);
% Initialize the array for average hue values
averageHues = zeros(1, 9);
% Create grid of coordinates
[X, Y] = meshgrid(1:3712, 1:5568);
% Loop through each circle to calculate the average hue
for k = 1:9
% Calculate the squared distance from the center of the kth circle
distSquared = (X - centers(k,1)).^2 + (Y - centers(k,2)).^2;
% Find the indices of pixels that lie within the kth circle
circleIndices = distSquared <= Rad(k)^2;
% Calculate the average hue for the kth circle
averageHues(k) = mean(hueChannel(circleIndices));
end
Please refer to the following documentation link to learn more about "meshgrid".
Hope this helps!
  1 Comment
Matthew
Matthew on 29 Jan 2024
Hi,
Thank you for your reply! Your code appears to function, however some of the values are very low/zero.
I managed to create masks for the individual tablets, and have made a very inefficient code to average the Hue. My only worry is this gives me different values compared to when using your code.
Tab1=BW2;
Circle1=images.roi.Circle(gca);
Circle1.Radius=110;
Circle1.Center=C(1,:);
TabMask1=createMask(Circle1, Tab1);
Tab1(TabMask1)=1;
Tab1(~TabMask1)=0;
Tab1RGB=bsxfun(@times, TabRGB1, cast(Tab1, 'like', TabRGB1));
Tab1HSV=rgb2hsv(Tab1RGB);
[Hue1, Sat1, Val1] =imsplit(Tab1HSV);
MeanHue(1,1)=mean(Hue1(:));
This set of code is repeated for each tablet. "MeanHue" is using my code and "averageHues" is using the code you supplied. Do you know which data set is accurate?
N.B. BW2 is the binary mask I supplied in my original post.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!