如何创建斜条纹图案和棋盘图案?
How to create diagonal stripe patterns and checkerboard patterns?
基于 this 问题,我可以通过将它与由此创建的调制信号相乘来确认可以将水平图案强加到矩阵(在本例中是图像)上:
vModulationSignal = 1 + (0.5 * cos(2 * pi * (signalFreq / numRows) * [0:(numRows - 1)].'));
如果有人能解释为什么上述调制信号有效,那就太好了。
现在我想创建对角线图案,例如:
以及像这样的交叉(方格)图案:
使用类似的 vModulationSignal
创建调制信号的代码摘录
numRows = size(mInputImage, 1);
numCols = size(mInputImage, 2);
signalFreq = floor(numRows / 1.25);
vModulationSignal = 1 + (0.5 * cos(2 * pi * (signalFreq / numRows) * [0:(numRows - 1)].'));
mOutputImage = bsxfun(@times, mInputImage, vModulationSignal);
我尝试创建十字交叉信号的代码摘录
numRows = size(mInputImage, 1);
numCols = size(mInputImage, 2);
signalFreq1 = floor(numRows / 1.25);
signalFreq2 = floor(numCols / 1.25);
vModulationSignal1 = 1 + (0.5 * cos(2 * pi * (signalFreq / numRows) * [0:(numRows - 1)].'));
vModulationSignal2 = 1 + (0.5 * cos(2 * pi * (signalFreq / numRows) * [0:(numRows - 1)].'));
mOutputImage = bsxfun(@times, mInputImage, vModulationSignal);
figure();
imshow(mOutputImage);
对于横、竖、斜条纹:
fx = 1 / 20; % 1 / period in x direction
fy = 1 / 20; % 1 / period in y direction
Nx = 200; % image dimension in x direction
Ny = 200; % image dimension in y direction
[xi, yi] = ndgrid(1 : Nx, 1 : Ny);
mask = sin(2 * pi * (fx * xi + fy * yi)) > 0; % for binary mask
mask = (sin(2 * pi * (fx * xi + fy * yi)) + 1) / 2; % for gradual [0,1] mask
imagesc(mask); % only if you want to see it
只需相应地选择fx
和fy
(横条纹设置fy=0
,竖条纹设置fx=0
,斜条纹设置fx,fy
)。顺便提一句。条纹的周期(以像素为单位)正好是
period_in_pixel = 1 / sqrt(fx^2 + fy^2);
对于棋盘格图案:
f = 1 / 20; % 1 / period
Nx = 200;
Ny = 200;
[xi, yi] = ndgrid(1 : Nx, 1 : Ny);
mask = sin(2 * pi * f * xi) .* sin(2 * pi * f * yi) > 0; % for binary mask
mask = (sin(2 * pi * f * xi) .* sin(2 * pi * f * yi) + 1) / 2; % for more gradual mask
imagesc(mask);
这里每个x,y方向的黑白方块个数为:
number_squares_x = 2 * f * Nx
number_squares_y = 2 * f * Ny
如果您知道图像的大小和所需的正方形数量,则可以使用它来计算参数 f。
将蒙版与图像相乘:
现在这很容易。掩码是合乎逻辑的(白色 = 真,黑色 = 假)。现在您只需决定要保留哪一部分(白色或黑色部分)。
将你的图像与蒙版相乘
masked_image = original_image .* mask;
保留蒙版中的白色区域并且
masked_image = original_image .* ~mask;
相反。
这实际上是 Trilarion 答案的扩展,可以更好地控制条纹外观:
function out = diagStripes( outSize, stripeAngle, stripeDistance, stripeThickness )
stripeAngle = wrapTo2Pi(-stripeAngle+pi/2);
if (stripeAngle == pi/2) || (stripeAngle == 3*pi/2)
f = @(fx, fy, xi, yi) cos(2 * pi * (fy * yi)); % vertical stripes
elseif (stripeAngle == 0)||(stripeAngle == pi)
f = @(fx, fy, xi, yi) cos(2 * pi * (fx * xi)); % horizontal stripes
else
f = @(fx, fy, xi, yi) cos(2 * pi * (fx * xi + fy * yi)); % diagonal stripes
end
if numel(outSize) == 1
outSize = [outSize outSize];
end;
fx = cos(stripeAngle) / stripeDistance; % period in x direction
fy = sin(stripeAngle) / stripeDistance; % period in y direction
Nx = outSize(2); % image dimension in x direction
Ny = outSize(1); % image dimension in y direction
[yi, xi] = ndgrid((1 : Ny)-Ny/2, (1 : Nx)-Nx/2);
mask = (f(fx, fy, xi, yi)+1)/2; % for gradual [0,1] mask
out = mask < (cos(pi*stripeThickness)+1)/2; % for binary mask
end
outSize
是一个二元或一元向量,以像素为单位给出输出图像的尺寸,stripeAngle
以弧度为单位给出条纹的斜率,stripeDistance
是中心之间的距离以像素为单位的条纹数,stripeDistance
是 [0 .. 1]
中的浮点值,它给出了(白色)背景中(黑色)条纹的覆盖百分比。
还有 other question 生成自定义棋盘图案的答案。
基于 this 问题,我可以通过将它与由此创建的调制信号相乘来确认可以将水平图案强加到矩阵(在本例中是图像)上:
vModulationSignal = 1 + (0.5 * cos(2 * pi * (signalFreq / numRows) * [0:(numRows - 1)].'));
如果有人能解释为什么上述调制信号有效,那就太好了。
现在我想创建对角线图案,例如:
以及像这样的交叉(方格)图案:
使用类似的 vModulationSignal
创建调制信号的代码摘录
numRows = size(mInputImage, 1);
numCols = size(mInputImage, 2);
signalFreq = floor(numRows / 1.25);
vModulationSignal = 1 + (0.5 * cos(2 * pi * (signalFreq / numRows) * [0:(numRows - 1)].'));
mOutputImage = bsxfun(@times, mInputImage, vModulationSignal);
我尝试创建十字交叉信号的代码摘录
numRows = size(mInputImage, 1);
numCols = size(mInputImage, 2);
signalFreq1 = floor(numRows / 1.25);
signalFreq2 = floor(numCols / 1.25);
vModulationSignal1 = 1 + (0.5 * cos(2 * pi * (signalFreq / numRows) * [0:(numRows - 1)].'));
vModulationSignal2 = 1 + (0.5 * cos(2 * pi * (signalFreq / numRows) * [0:(numRows - 1)].'));
mOutputImage = bsxfun(@times, mInputImage, vModulationSignal);
figure();
imshow(mOutputImage);
对于横、竖、斜条纹:
fx = 1 / 20; % 1 / period in x direction
fy = 1 / 20; % 1 / period in y direction
Nx = 200; % image dimension in x direction
Ny = 200; % image dimension in y direction
[xi, yi] = ndgrid(1 : Nx, 1 : Ny);
mask = sin(2 * pi * (fx * xi + fy * yi)) > 0; % for binary mask
mask = (sin(2 * pi * (fx * xi + fy * yi)) + 1) / 2; % for gradual [0,1] mask
imagesc(mask); % only if you want to see it
只需相应地选择fx
和fy
(横条纹设置fy=0
,竖条纹设置fx=0
,斜条纹设置fx,fy
)。顺便提一句。条纹的周期(以像素为单位)正好是
period_in_pixel = 1 / sqrt(fx^2 + fy^2);
对于棋盘格图案:
f = 1 / 20; % 1 / period
Nx = 200;
Ny = 200;
[xi, yi] = ndgrid(1 : Nx, 1 : Ny);
mask = sin(2 * pi * f * xi) .* sin(2 * pi * f * yi) > 0; % for binary mask
mask = (sin(2 * pi * f * xi) .* sin(2 * pi * f * yi) + 1) / 2; % for more gradual mask
imagesc(mask);
这里每个x,y方向的黑白方块个数为:
number_squares_x = 2 * f * Nx
number_squares_y = 2 * f * Ny
如果您知道图像的大小和所需的正方形数量,则可以使用它来计算参数 f。
将蒙版与图像相乘:
现在这很容易。掩码是合乎逻辑的(白色 = 真,黑色 = 假)。现在您只需决定要保留哪一部分(白色或黑色部分)。
将你的图像与蒙版相乘
masked_image = original_image .* mask;
保留蒙版中的白色区域并且
masked_image = original_image .* ~mask;
相反。
这实际上是 Trilarion 答案的扩展,可以更好地控制条纹外观:
function out = diagStripes( outSize, stripeAngle, stripeDistance, stripeThickness )
stripeAngle = wrapTo2Pi(-stripeAngle+pi/2);
if (stripeAngle == pi/2) || (stripeAngle == 3*pi/2)
f = @(fx, fy, xi, yi) cos(2 * pi * (fy * yi)); % vertical stripes
elseif (stripeAngle == 0)||(stripeAngle == pi)
f = @(fx, fy, xi, yi) cos(2 * pi * (fx * xi)); % horizontal stripes
else
f = @(fx, fy, xi, yi) cos(2 * pi * (fx * xi + fy * yi)); % diagonal stripes
end
if numel(outSize) == 1
outSize = [outSize outSize];
end;
fx = cos(stripeAngle) / stripeDistance; % period in x direction
fy = sin(stripeAngle) / stripeDistance; % period in y direction
Nx = outSize(2); % image dimension in x direction
Ny = outSize(1); % image dimension in y direction
[yi, xi] = ndgrid((1 : Ny)-Ny/2, (1 : Nx)-Nx/2);
mask = (f(fx, fy, xi, yi)+1)/2; % for gradual [0,1] mask
out = mask < (cos(pi*stripeThickness)+1)/2; % for binary mask
end
outSize
是一个二元或一元向量,以像素为单位给出输出图像的尺寸,stripeAngle
以弧度为单位给出条纹的斜率,stripeDistance
是中心之间的距离以像素为单位的条纹数,stripeDistance
是 [0 .. 1]
中的浮点值,它给出了(白色)背景中(黑色)条纹的覆盖百分比。
还有 other question 生成自定义棋盘图案的答案。