How do i create a rectangular mask at known angles?

狂风中的少年 提交于 2019-12-04 06:30:35

问题


I have created a synthetic image that consists of a circle at the centre of a box with the code below.

%# Create a logical image of a circle with image size specified as follows:
imageSizeY = 400;
imageSizeX = 300;

[ygv, xgv] = meshgrid(1:imageSizeY, 1:imageSizeX);

%# Next create a logical mask for the circle with specified radius and center
centerY = imageSizeY/2;
centerX = imageSizeX/2;
radius  = 100;

Img   = double( (ygv - centerY).^2 + (xgv - centerX).^2 <= radius.^2 );


%# change image labels from double 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');  

Now, i need to create a rectangular mask (with label == 3, and row/col dimensions: 1 by imageSizeX) across the image from top to bottom and at known angles with the edges of the circle (see attached figure). Also, how can i make the rectangle thicker than 1 by imageSizeX?. As another option, I would love to try having the rectangle stop at say column 350. Lastly, any ideas how I can improve on the resolution? I mean is it possible to keep the image size the same while increasing/decreasing the resolution.

I have no idea how to go about this. Please i need any help/advice/suggestions that i can get. Many thanks!.


回答1:


You can use the cos function to find the x coordinate with the correct angle phi. First notice that the angle between the radius that intersects the vertex of phi has angle with the x-axis given by:

and the x coordinate of that vertex is given by

so the mask simply needs to set that row to 3.

Example:

phi = 45;       % Desired angle in degrees
width = 350;    % Desired width in pixels
height = 50;    % Desired height of bar in pixels
theta = pi-phi*pi/180;    % The radius angle
x = centerX + round(radius*cos(theta)); % Find the nearest row
x0 = max(1, x-height); % Find where to start the bar
Img(x0:x,1:width)=3;

The resulting image looks like:

Note that the max function is used to deal with the case where the bar thickness would extend beyond the top of the image.

Regarding resolution, the image resolution is determined by the size of the matrix you create. In your example that is (400,300). If you want higher resolution simply increase those numbers. However, if you would like to link the resolution to a higher DPI (Dots per Inch) so there are more pixels in each physical inch you can use the "Export Setup" window in the figure File menu.

Shown here:



来源:https://stackoverflow.com/questions/42662789/how-do-i-create-a-rectangular-mask-at-known-angles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!