Finding Circle Boundary Pixels Coordinates and RGB Intensity Values from An RGB Input Image in Matlab

1k views Asked by At

I would like to get pixels coordinates and RGB Intensity values from points that only lie on the specified circle boundary in a given RGB input image without drawing the circle and considering Its specified color. As far as I did is just draw the circle on a particular point. Any help? Thanks.

yellow = uint8([255 255 0]); 
% Create the shape inserter object.
shapeInserter = vision.ShapeInserter('Shape','Circles','BorderColor','Custom','CustomBorderColor',yellow);
% Read RGB input image.
I = imread('3.jpg'); 
% Define the circle dimensions
x1=80;
y1=80;

circle = int32([x1 y1 3]); %  [x1 y1 radius]

% Draw the circle and display the result.
J = step(shapeInserter, I, circle);
imshow(J);
1

There are 1 answers

2
rayryeng On

You don't need the Computer Vision toolbox for that. Simply define your centre coordinate of your circle and the specified radius, then use a combination of meshgrid and logical indexing to determine (x,y) coordinates as well as the corresponding RGB values along the perimeter of the circle.

Something like this:

%// Define parameters
x1 = 80; y1 = 80;
radius = 3;
tol = 0.1;

%// Get dimensions of image
rows = size(I,1); cols = size(I,2);

%// Define grid of coordinates
[x,y] = meshgrid(1:cols, 1:rows);

%// Define mask for valid pixels
mask = ((x - x1).^2 + (y - y1).^2) >= (radius - tol)^2;
mask = mask & ((x - x1).^2 + (y - y1).^2) <= (radius + tol)^2;

%// Get row and column locations
col = x(mask); row = y(mask);

%// Get pixel values
R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
red = R(mask); green = G(mask); blue = B(mask);

These statements:

mask = ((x - x1).^2 + (y - y1).^2) >= (radius - tol)^2;
mask = mask & ((x - x1).^2 + (y - y1).^2) <= (radius + tol)^2;

defines the equation of the circle of the form:

(x - x0)^2 + (y - y0)^2 == r^2

The centre coordinate is defined at (x0, y0) with a radius of r. However, due to discrete coordinates in the image, you'll need to get values within some tolerance of the radius. I set this tolerance to be 0.1. This equation defines the boundary of the circle. Therefore, we want to find those locations in the image that allow the above expression to be true.

Once you're done, we can get the row and column locations of the pixels along the boundary and finally we can use the logical mask itself to index into each channel and grab the corresponding red, green and blue pixels for each of the locations that are defined along the boundary.


Here's an example with a radius of 30, centered at (100,100) with rows and columns set to 300 respectively. We visualize what the mask looks like. White denotes that we sample at this point and black means we don't:

enter image description here