I need to create an image with three levels as described in the following question: How do i create a rectangular mask at known angles?
Code:
%# Create a logical image of a circle with image size specified as follows:
imageSizeX = 401;
imageSizeY = 301;
[columngrids, rowgrids] = meshgrid(1:imageSizeX, 1:imageSizeY);
%# Next create a logical mask for the circle with specified radius and center
centerY = (imageSizeY/2) + 0.5;
centerX = (imageSizeX/2) + 0.5;
radius = 50;
Img = double( (rowgrids - centerY).^2 + (columngrids - centerX).^2 <= radius.^2 );
%# change image labels to numeric
for ii = 1:numel(Img)
if Img(ii) == 0
Img(ii) = 2; %change label from 0 to 2
end
end
%# plot image
RI = imref2d(size(Img),[0 size(Img, 2)],[0 size(Img, 1)]);
figure, imshow(Img, RI, [], 'InitialMagnification','fit');
%# create the desired angle
phi = 45;
width = 350; % Desired width in pixels
height = 300; % Desired height of bar in pixels
y = centerY - round(radius*cos(phi*pi/180)); % Find the nearest column
y0 = max(1, y-height); % Find where to start the bar
Img(y0:y, 1:width)=3;
figure, imshow(Img, RI, [], 'InitialMagnification','fit');
I have realized that rounding the part that says (radius*cos(phi*pi/180))
to find y
could in most cases create an error in the desired angle. Hence, if I remove the ‘round function’
, I get the actual y value
at the exact point at which the desired angle is formed in the image. Nonetheless, I get the warning as stated above. However, if I go further to apply the line: Img(y0:y, 1:width)=3;
, the code still works, but I notice that Matlab approximates the y value when creating the vector y0:y
(I feel this is the point where I have an issue)
My question then is: is there a way I could get around this such that I create my desired angle accurately and still end up having the bar from y to y0? without having matlab approximate the y value when it is creating the vector y0:y
?
Maybe if i convert to cartesian xy coordinates i could have a chance? Any ideas how to do this conversion? Many thanks for your help!