Creating image mask of pizza-shape
18 views (last 30 days)
Show older comments
Hello
I have a 128x128 image, I want to create a mask of 128x128, where I have a pizza-shape or atleast triangular-shape mask. For example, the mask coordinates to be (0,0), (0,1), and (0.1,1).
Each mask corresponds to an hour basically in the 24 hour cycle. I know how to extract rectangular masks, but I have no idea how to extract trianglular masks, especially corresponding to each hour. We may deal with this in polar coordinates first as (0,0), (0,1), and (15,1), where each hour is 15 degree.
Please help me with this.
2 Comments
Answers (2)
Image Analyst
on 26 Jul 2020
Attached is my demo. Adapt as needed.
% Code to mask a gray level image with a sector (pie piece shape) of a circle.
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;
% Read in the image. It can be either RGB color or gray scale.
theImage = imread('cameraman.tif'); % Gray scale demo image.
% theImage = imread('peppers.png'); % Color demo image.
subplot(2,2,1);
imshow(theImage, []);
axis on;
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(theImage);
% Create the sector coordinates.
xCenter = columns / 2; % Let's have the center/tip of the sector be at the middle of the image.
yCenter = rows / 2;
% Define the angle of the sector to go from 0 to 120 degrees (2*pi/3 radians).
theta = linspace(0, -120, 1000);
radius = 100;
x = radius * cosd(theta) + xCenter; % Using cosd() which takes arguments in degrees, not radians.
y = radius * sind(theta) + yCenter; % Using sind() which takes arguments in degrees, not radians.
% Tack on the center since so far we are only along the perimeter.
x = [xCenter, x, xCenter];
y = [yCenter, y, yCenter];
% Plot the sector over the image in the graphics overlay.
hold on; % Don't blow away the image by calling plot()!
plot(x, y, 'r-', 'LineWidth', 2);
axis square;
grid on;
% Create mask as a binary image.
mask = poly2mask(x,y,size(theImage, 1), size(theImage, 2));
subplot(2,2,2);
imshow(mask, []);
axis on;
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Mask the image using bsxfun() function.
% This should work with a color image also.
maskedGrayImage = bsxfun(@times, theImage, cast(mask, class(theImage)));
subplot(2,2,3);
imshow(maskedGrayImage, []);
axis on;
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Crop the image
topRowY = floor(min(y))
leftColumnX = floor(min(x))
width = ceil(max(x)) - leftColumnX
height = ceil(max(y)) - topRowY
croppedImage = imcrop(maskedGrayImage, [leftColumnX, topRowY, width, height]);
% Display the cropped image.
subplot(2,2,4);
imshow(croppedImage, []);
axis on;
title('Cropped Image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
0 Comments
KSSV
on 26 Jul 2020
- I will use ocr of MATLAB https://in.mathworks.com/help/vision/examples/recognize-text-using-optical-character-recognition-ocr.html. This will recognise the hours/ numbers in the image along with locations.
- Mean of all the locations will give me centre.
- Now you know the center and location of each hour number, you can get your triangle.
0 Comments
See Also
Categories
Find more on Modify Image Colors in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!