如何从伪随机二进制序列生成如下图所示的2D周期性模式? [英] How to generate a 2D periodic pattern like below image from pseudorandom binary sequence?

查看:262
本文介绍了如何从伪随机二进制序列生成如下图所示的2D周期性模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从具有以下上下文的伪随机二进制序列中生成二维周期模式:

I want to generate a 2 dimensional periodic pattern from a pseudorandom binary sequence like this one with the following context:

周期模式满足方程式(1)和(2)

A periodic pattern satisfies the equations (1) and (2)

W(x + q0N0,y)= W(x,y); q0,N0> 1(1)

W(x + q0N0, y) = W(x, y); q0, N0 >1 (1)

W(x,y + q1N1)= W(x,y); q1,N1> 1,(2)

W(x, y + q1N1) = W(x, y); q1, N1 > 1, (2)

其中,N0和N1确定重复的周期,q0和q1是水平和垂直方向上的重复数.从伪随机值{−1,1}生成将生成一个矩形的二进制值模式.

where N0 and N1 determine the periodicity of repetitions and q0 and q1 a repetition number on the horizontal and vertical directions. Generation from pseudorandom values {−1, 1} produces a rectangular, binary valued pattern.

推荐答案

一种实现方法是采用一个小的伪随机2D模式(序列)并周期性地重复它,以便始终对相邻的图块进行镜像,从而提供一个流畅的连续感.

One way to achieve that would be to take a small pseudo-random 2D pattern (the sequence) and repeat it periodically so that neighboring tiles are always mirrored giving a sense of smooth continuity.

在用W(x + q0N0, y) = W(x, y)W(x, y + q1N1) = W(x, y);指定了需求之后,很明显,这正是您想要的(没有镜像部分).

After you specified your requirements with W(x + q0N0, y) = W(x, y) and W(x, y + q1N1) = W(x, y); it becomes clear, that this is exactly (without the mirroring part) what you want.

您只需要在两个方向上重复一定次数的随机模式即可.

You simply have to repeat a random pattern a certain number of times in both directions.

示例(类似于您的图像,其中在垂直方向上的周期长度比在水平方向上的长度长)

Example (similar to your image where the period length in the vertical direction is longer than in the horizontal direction)

代码(在Matlab中)

Code (in Matlab)

% base pattern
N0 = 20;
N1 = 5;
base = rand([N0, N1]) > 0.5; % pseudo-random

% periodically repeating the pattern
Q0 = 5;
Q1 = 20;
pattern = zeros([N0*Q0,N1*Q1]);
for q0 = 1 : Q0
    for q1 = 1 : Q1
        pattern((q0-1)*N0+1:q0*N0, (q1-1)*N1+1:q1*N1) = base;
    end
end

% save
imwrite(pattern, 'test.jpg');

% display
imagesc(pattern);
axis image;
colormap(gray);

第一行只是计算一个随机大小为N0 x N1的二进制模式

The first lines just compute a random, binary pattern of a certain size N0 x N1

% base pattern
N0 = 20;
N1 = 5;
base = rand([N0, N1]) > 0.5; % pseudo-random

然后是每个方向上图案重复次数的定义

Then comes the definition of the number of repetitions of the pattern in each direction

Q0 = 5;
Q1 = 20;

最后,在两个嵌套但相当简单的for循环中,重复了基本模式

Finally, in two nested but rather simple for loops, the base pattern is repeated

pattern = zeros([N0*Q0,N1*Q1]);
for q0 = 1 : Q0
    for q1 = 1 : Q1
        pattern((q0-1)*N0+1:q0*N0, (q1-1)*N1+1:q1*N1) = base;
    end
end

计算索引(放置基本模式的位置)满足您的需求方程式

The computation of the indices (where to place the base patterns) fulfills your requirement equations

(q0-1)*N0+1:q0*N0, (q1-1)*N1+1:q1*N1


旧示例(带有镜像)


Old Example (with mirroring)

代码(在Matlab中)

Code (in Matlab)

% base pattern
N = 20;
base = rand(N) > 0.5; % pseudo-random

% multiplying the pattern
M = 4;
pattern = zeros(N*M);
for i = 1 : M
    for j = 1 : M
        b = base;
        % mirroring the base
        if mod(i, 2) == 1
            flip(b, 2);
        end
        if mod(j, 2) == 1
            flip(b, 1);
        end
        pattern((i-1)*N+1:i*N, (j-1)*N+1:j*N) = b;
    end
end

% save
imwrite(pattern, 'test.jpg');

% display
imagesc(pattern);
axis image;
colormap(gray);

有时会在一个或两个方向上翻转(镜像)图案,以模拟图案的某种平滑度(对称性).

The patterns are flipped (mirrored) in one or two direction sometimes to simulates some kind of smoothness (symmetry) of the pattern.

这篇关于如何从伪随机二进制序列生成如下图所示的2D周期性模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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