如何以已知角度创建矩形蒙版? [英] How do i create a rectangular mask at known angles?

查看:157
本文介绍了如何以已知角度创建矩形蒙版?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个合成图像,其中包含一个框中心的圆圈,代码如下。

 % #创建一个圆形的逻辑图像,图像大小指定如下:
imageSizeY = 400;
imageSizeX = 300;

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

%#Next为具有指定半径的圆创建一个逻辑蒙版,并将
centerY = imageSizeY / 2;
centerX = imageSizeX / 2;
半径= 100;

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


%#将图像标签从double更改为数字
for ii = 1:numel(Img)

if Img(ii)== 0
图(ii)= 2; %更改标签从0到2
结束

结束

%#plot image
RI = imref2d(尺寸(Img),[0尺寸( Img,2)],[0 size(Img,1)]);
figure,imshow(Img,RI,[],'InitialMagnification','fit');

现在,我需要创建一个矩形蒙版(标签== 3,行/列尺寸) :1 by imageSizeX)从上到下穿过图像并与圆的边缘成已知角度(见附图)。另外,如何通过imageSizeX?使矩形比1厚?作为另一个选择,我想尝试让矩形停在第350栏。最后,任何想法如何改进分辨率?我的意思是可以在增加/降低分辨率的同时保持图像大小相同。





我不知道如何解决这个问题。我需要任何帮助/意见/建议。非常感谢!。

解决方案

您可以使用 cos 功能查找 x 坐标与正确的角度 phi
首先注意,与phi顶点相交的半径与 x轴的角度之间的角度由下式给出:





x 该顶点的坐标由





所以掩码只需要将该行设置为3.



示例:

  phi = 45; %所需角度,以度为单位
宽度= 350; %所需宽度(以像素为单位)
height = 50; %所需的条形高度,以像素为单位
theta = pi-phi * pi / 180; %半径角
x = centerX + round(radius * cos(theta)); %找到最近的行
x0 = max(1,x-height); %查找从哪里开始吧
图(x0:x,1:宽度)= 3;

结果图像如下:



请注意 max 函数用于处理条形厚度超出范围的情况图片的顶部。



关于解决方案,


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!.

解决方案

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:

这篇关于如何以已知角度创建矩形蒙版?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆