How to obtain an angular segment with its vertex on the center from a circular image?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I have a circular image. I need to extract eight equal angular segment from it. Can anyone help me with the matlab code for this?
Accepted Answer
Image Analyst
on 1 May 2014
xCenter = 12;
yCenter = 10;
theta = 0 : 0.01 : 2*pi;
radius = 5;
x = radius * cos(theta) + xCenter;
y = radius * sin(theta) + yCenter;
plot(x, y);
axis square;
xlim([0 20]);
ylim([0 20]);
grid on;
Adjust the starting and ending theta to be what you need them to be, then pass x and y into poly2mask() to get a binary image which can then multiply by your image.
mask = poly2mask(x,y,size(rgbImage, 1), size(rgbImage, 2));
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, class(rgbImage)));
7 Comments
Jyothis Jose
on 2 May 2014
I tried this one. But for 0 to pi/4 I am getting only a small segment not an angular segment with its vertex on the center of the circular image. Please suggest me a solution. Thank You
Image Analyst
on 2 May 2014
How about you post your code and I fix it?
On second thought, that might be more work than doing it myself. Here, run this code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
%===============================================================================
% Read in a standard MATLAB color demo image.
baseFileName = 'peppers.png';
folder = fileparts(which(baseFileName));
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Make pie sector.
xCenter = columns/2;
yCenter = rows/2;
theta = 0 : 0.01 : pi/2;
radius = min([rows/2, columns/2]);
x = radius * cos(theta) + xCenter;
y = radius * sin(theta) + yCenter;
% Add point at center to beginning and end of array and shift to center.
x = [xCenter, x, xCenter];
y = [yCenter, y, yCenter];
subplot(2, 2, 2);
plot(x, y);
title('Sector Plot', 'FontSize', fontSize);
axis square;
grid on;
mask = poly2mask(x,y, size(rgbImage, 1), size(rgbImage, 2));
% Display the mask image.
subplot(2, 2, 3);
imshow(mask);
title('Mask Image', 'FontSize', fontSize);
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, class(rgbImage)));
% Display the mask image.
subplot(2, 2, 4);
imshow(maskedRgbImage);
title('Masked Color Image', 'FontSize', fontSize);

Jyothis Jose
on 2 May 2014
Thank You so much sir. It worked well on my image. It helped me a lot in doing my project. Thank You once again........
Image Analyst
on 2 May 2014
Please mark the answer as "Accepted" then. Thanks in advance.
How to apply it for grayscale Image?
It's the same code, using bsxfun. Change the ending angle if you don't want a full circle.
xCenter = 128;
yCenter = 128;
theta = 0 : 0.01 : 2*pi/3;
radius = 100;
x = radius * cos(theta) + xCenter;
y = radius * sin(theta) + yCenter;
plot(x, y);
axis square;
xlim([0 20]);
ylim([0 20]);
grid on;
% Tack on the center
x = [xCenter, x, xCenter];
y = [yCenter, y, yCenter];
% Read in gray scale image. Actually can be anything - RGB or gray scale.
grayImage = imread('cameraman.tif');
subplot(2,2,1);
imshow(grayImage, []);
axis on;
% Create mask
mask = poly2mask(x,y,size(grayImage, 1), size(grayImage, 2));
subplot(2,2,2);
imshow(mask, []);
axis on;
% Mask the image using bsxfun() function
maskedGrayImage = bsxfun(@times, grayImage, cast(mask, class(grayImage)));
subplot(2,2,3:4);
imshow(maskedGrayImage, []);
axis on;

More Answers (0)
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)