Circular window

13 views (last 30 days)
Khawaja Asim
Khawaja Asim on 5 Dec 2011
Commented: Image Analyst on 31 Jan 2015
Hi I need to use sliding circular window of radius "r" for feature extraction on an image. I want to divided this circular in equal 6 quadrants, each other 60 degree. So that calculations can be performed separately for each quadrant.
Can anyone guide me about creating quadrants in a circle.
{Note: Circular window is created by own logic, NOT by any built-in function}
  1 Comment
Daniel Armyr
Daniel Armyr on 5 Dec 2011
Do you want to make 2-dimensional kernels that look like 1/6th of a circle? That is fairly easy, if that is what you want.
Or is it something else?

Sign in to comment.

Accepted Answer

David Young
David Young on 5 Dec 2011
EDIT: Deleted request for additional information. Modified reference to original question. Added suggested code.
This relates to your previous question. The answers there remain relevant.
Pedantry corner: you can't divide a circle into 6 quadrants, you can only divide it into 4 quadrants. You can divide it into 6 sectors though.
In your code, you use loops to set individual pixels. The basic idea is fine, and you can get it to work, but there are more powerful ways to write such operations in MATLAB, and it's worth knowing about these, so first, I'll give a modified version of your code to pick out the circular region of the image. It looks like this, taking over from your code right after the call to rgb2gray:
sz=size(image);
x2=340; y2=360; % circular region parameters
r=100;
% Make a logical image with the selected circular region set to 1, the rest
% to zero
[xgrid, ygrid] = meshgrid(1:sz(2), 1:sz(1));
x = xgrid - x2; % offset the origin
y = ygrid - y2;
circlemask = x.^2 + y.^2 <= r.^2;
% Use the mask to select part of the image
circle_image = double(image) .* circlemask;
imshow(circle_image, []);
Except for the fact the result is of class 'double', this does the same as your code. Note how the logical operator <= can be used to select part of a region, and also note that the operations make use of MATLAB's inbuilt "parallel" operations to do things to the whole of the arrays.
Now, we can extend this to doing quadrants. It's a matter choosing the parts of the circle that lie between radii at specific angles. Angles are computed using atan2, and again we can compute the angles everywhere in the array, and then use logical operators to pick the quadrants:
angle = atan2(y, x);
quadrant1 = circlemask & angle > -pi/4 & angle <= pi/4;
quadrant2 = circlemask & angle > pi/4 & angle <= 3*pi/4;
quadrant3 = circlemask & (angle > 3*pi/4 | angle <= -3*pi/4);
quadrant4 = circlemask & angle > -3*pi/4 & angle <= -pi/4;
A quadrant is defined by the pixels inside the circle, and also with an angle between specific values. The only peculiarity is for the third quadrant - this is because of the wraparound of angles, which has to be somewhere and happens to be in this quadrant. See the diagram in doc atan2, which should make it clear what is happening.
Adjusting the angle thresholds will let you pick out any sectors at any angles you wish.
You can pick out part of the image within a quadrant as before: here's one example:
quad1_image = double(image) .* quadrant1;
imshow(quad1_image, []);
  9 Comments
Junaid Ansari
Junaid Ansari on 31 Jan 2015
Helpful. Thanks
Image Analyst
Image Analyst on 31 Jan 2015
There are also numerous FAQ entries dealing with circles. Mostly in this section of the FAQ: http://matlab.wikia.com/wiki/FAQ#Math.2FAlgorithms

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!