如何创建斜条纹图案和棋盘图案? [英] How to create diagonal stripe patterns and checkerboard patterns?

查看:228
本文介绍了如何创建斜条纹图案和棋盘图案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于



这样的十字交叉(方格)模式:





使用类似的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 / x方向的周期
fy = 1/20; %1 / y方向的期间
Nx = 200; x方向的%图像尺寸
Ny = 200; y方向上的%图像尺寸
[xi,yi] = ndgrid(1:Nx,1:Ny);
mask = sin(2 * pi *(fx * xi + fy * yi))> 0;二进制掩码的百分比
掩码=(sin(2 * pi *(fx * xi + fy * yi))+ 1)/ 2;逐渐[0,1]掩码的百分比
imagesc(掩码); %只有你想看到它

只需选择 fx fy 相应(水平条纹设置 fy = 0 fx = 0 表示垂直条纹, fx,fy 等于斜条纹)。顺便说一句。条纹的周期(以像素为单位)正好

  period_in_pixel = 1 / sqrt(fx ^ 2 + fy ^ 2); 

棋盘格局

  f = 1/20; %1 /期间
Nx = 200;
Ny = 200;
[xi,yi] = ndgrid(1:Nx,1:Ny);
mask = sin(2 * pi * f * xi)。* sin(2 * pi * f * yi)> 0;二进制掩码的百分比
掩码=(sin(2 * pi * f * xi)。* sin(2 * pi * f * yi)+ 1)/ 2; %渐变面膜
imagesc(面具);

此处每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; 

相反。


Based on this question, I can confirm that horizontal patterns can be imposed onto a matrix (which in this case is an image), by multiplying it with a modulation signal created with this:

vModulationSignal = 1 + (0.5 * cos(2 * pi * (signalFreq / numRows) * [0:(numRows - 1)].'));

It would also be great if someone could explain to why the above modulation signal works.

Now I want to create diagonal patterns such as :

And criss-cross (checkered) patterns such as this:

using a similar vModulationSignal


Code Excerpt where the modulation signal is created

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);

Code Excerpt where I'm trying to create the criss cross signal

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);

解决方案

For horizontal, vertical, diagonal stripes:

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

just choose fx and fy accordingly (set fy=0 for horizontal stripes, fx=0 for vertical stripes and fx,fy equal for diagonal stripes). Btw. the period of the stripes (in pixels) is exactly

period_in_pixel = 1 / sqrt(fx^2 + fy^2);

For checkerboard patterns:

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);

Here the number of black and white squares per x, y direction is:

number_squares_x = 2 * f * Nx
number_squares_y = 2 * f * Ny

And if you know the size of your image and the number of squares that you want, you can use this to calculate the parameter f.

Multiplying the mask with the image:

Now that is easy. The mask is a logical (white = true, black = false). Now you only have to decide which part you want to keep (the white or the black part).

Multiply your image with the mask

masked_image = original_image .* mask;

to keep the white areas in the mask and

masked_image = original_image .* ~mask;

for the opposite.

这篇关于如何创建斜条纹图案和棋盘图案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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