How to get pixel value inside a circle

Hi.
I'm having problem of gaining a pixel values inside a circle.
I used drawcircle function to draw the circle.
Here's the image I want to show you.
As you see, there are three black cicles.
what i want to do is get a mean value inside the black circle.
Thanks.

 Accepted Answer

Matt J
Matt J on 20 Mar 2023
Edited: Matt J on 20 Mar 2023
drawcircle() returns an object with a createMask method. Using the mask produced by createMask(), you can do,
mean(yourImage(mask))

5 Comments

Thank you so much.
I have a question for it though.
Image(mask) comes out like n x 1 uint8
But i don't know why it comes out in one row. If i use color image doesn't it should come out in three row?
This is mask row.
I think it's x,y coordinate. but when in Image(mask), this comes out.
I don't know how this values comes out.
I didn't use mean function but it comes out like this.
Thanks.
But i don't know why it comes out in one row.
You mean one column, not one row. Indexing with a single logical index always produces a column vector as output.
If i use color image doesn't it should come out in three row?
If you want the mean of each color channel, you will have to do each channel separately:
R=Image(:,:,1); Rmean=mean(R(mask));
G=Image(:,:,2); Gmean=mean(G(mask));
B=Image(:,:,3); Bmean=mean(B(mask));
Or, you could process all channels at once by taking a slightly different approach,
RGBmean = sum(Image.*mask,[1,2])./nnz(mask)
Or you could use the more convenient imsplit
[R, G, B] = imsplit(rgbImage);
Rmean=mean(R(mask));
Gmean=mean(G(mask));
Bmean=mean(B(mask));
Thanks!
My problem is solved
You're welcome. By the way, did you see the full demo (below) I created for you?

Sign in to comment.

More Answers (1)

Try this. Adapt it to drawing multiple circles and showing them all should be no problem.
% Demo to show how drawcircle can be used to draw a circle on the image, and crop out that circular region to a new image and compute the mean RGB values and area.
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 = 20;
%-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% rgbImage = imread('peppers.png');
% rgbImage = imread('coloredchips.png');
rgbImage = imread('peacock.jpg');
hFig = figure;
imshow(rgbImage);
hFig.WindowState = 'maximized';
%-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% Ask user to draw circle.
button = 'Redraw';
while contains(button, 'Redraw', 'IgnoreCase',true)
uiwait(helpdlg('Draw a circle'));
% User draws a circle. It exits as soon as they lift the mouse.
hCircle = drawcircle('Color', 'r')
% Get the coordinates in the form [xLeft, yTop, width, height].
xyCenter = hCircle.Center
radius = hCircle.Radius
% Get Mask image.
mask = hCircle.createMask;
% Delete the ROI object.
delete(hCircle);
% and replace it with a circle in the graphical overlay.
hVC = viscircles(xyCenter, radius, 'Color', 'r', 'LineWidth', 2);
% Ask user if the circle is acceptable.
message = sprintf('Is this good?');
button = questdlg(message, message, 'Accept', 'Redraw', 'Reject and Quit', 'Accept');
if contains(button, 'Quit','IgnoreCase',true)
delete(hVC); % Delete the circle from the overlay.
roiPosition = [];
break;
elseif contains(button, 'Redraw','IgnoreCase',true)
% OPTIONAL If you want to delete the prior one before drawing the next one.
delete(hVC);
elseif contains(button, 'Accept','IgnoreCase',true)
break;
end
end
% If you want to delete the circle from the overlay, do this:
delete(hVC); % Delete the circle from the overlay.
%-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% OPTIONAL: Crop out the circle into a new image.
props = regionprops(mask, 'BoundingBox');
croppedImage = imcrop(rgbImage, props.BoundingBox);
% Display original image
subplot(2, 1, 1);
imshow(rgbImage);
axis('on', 'image')
title('Original Image', 'FontSize', fontSize)
% Display circle over original image.
hold on;
hVC = viscircles(xyCenter, radius, 'Color', 'r', 'LineWidth', 2);
% Display cropped image.
subplot(2, 1, 2);
imshow(croppedImage);
axis('on', 'image')
title('Cropped Image', 'FontSize', fontSize)
% Display circle over cropped image.
xyCenterCropped = [size(croppedImage, 2), size(croppedImage, 1)] / 2;
hold on;
hVC = viscircles(xyCenterCropped, radius, 'Color', 'r', 'LineWidth', 2);
%-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% OPTIONAL: Get the area and the mean RGB values.
[R, G, B] = imsplit(rgbImage);
Rmean=mean(R(mask));
Gmean=mean(G(mask));
Bmean=mean(B(mask));
area = nnz(mask);
% Update title to show the area and RGB values.
caption = sprintf('Cropped Image. Mean RGB values = [%.2f, %.2f, %.2f]. Area = %d pixels.',...
Rmean, Gmean, Bmean, area)
title(caption, 'FontSize', fontSize);
% Close down figure.
% close(hFig);

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!