How to create diagonal stripe patterns and checkerboard patterns?

随声附和 提交于 2019-12-12 05:29:37

问题


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

回答1:


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.




回答2:


This is actually an extension of Trilarion's answer that gives better control on stripes appearance:

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 is a two or one element vector that gives the dimensions of output image in pixels, stripeAngle gives the slope of stripes in radians, stripeDistance is the distance between centers of stripes in pixels and stripeDistance is a float value in [0 .. 1] that gives the percent of coverage of (black) stripes in (white) background.

There're also answers to the other question for generating customized checkerboard patterns.



来源:https://stackoverflow.com/questions/34043381/how-to-create-diagonal-stripe-patterns-and-checkerboard-patterns

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