How to get pixel value inside a circle
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
jaeyoung gwak
el 20 de Mzo. de 2023
Comentada: Image Analyst
el 21 de Mzo. de 2023
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.
0 comentarios
Respuesta aceptada
Matt J
el 20 de Mzo. de 2023
Editada: Matt J
el 20 de Mzo. de 2023
drawcircle() returns an object with a createMask method. Using the mask produced by createMask(), you can do,
mean(yourImage(mask))
5 comentarios
Image Analyst
el 21 de Mzo. de 2023
You're welcome. By the way, did you see the full demo (below) I created for you?
Más respuestas (1)
Image Analyst
el 20 de Mzo. de 2023
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);
0 comentarios
Ver también
Categorías
Más información sobre Image Processing Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!