【问题标题】:How to create diagonal stripes pattern of different angles and thicknesses?如何创建不同角度和厚度的斜条纹图案?
【发布时间】:2017-10-04 09:55:53
【问题描述】:

我想用这些参数创建黑白斜条纹图案:

  • h: 图片高度
  • w: 图片宽度
  • a: 条带与 X 轴的夹角
  • d: 条纹间距
  • t: 条纹粗细

这可以使用正弦光栅来完成,就像我发布的答案 here。但它有两个问题:

  1. 不保证条纹保持连接
  2. 不保证条纹的厚度保持不变

这是一个示例:

diagStripes( [h h], pi/4, 20, 0.05);
diagStripes( [h h], pi/6, 20, 0.03);

然后我实现了另一个受DDA line drawing algorithm 启发的函数。首先它创建一个垂直/水平条纹图案,然后循环移动其行/列以创建对角线图案:

function [ out ] = diagStripes( h, w, a, d, t )
% wrap a between pi/4 and -3*pi/4
a = -wrapTo2Pi(a);
if a<-7*pi/4
    a=a+2*pi;
elseif a<-3*pi/4
    a=a+pi;
end

if a>-(pi/4) % the acute angle between stripes and x axis is greater than pi/4
    dy = round(abs(d/cos(a))); % vertical distance between stripes
    ty = max(1, round(abs(t/cos(a)))); % vertical thickness of stripes
    n = ceil(h/dy); % maximum number of stripes
    out = repmat([false(ty, 1); true(dy-ty, 1)], n, w); % create horizontal stripes
    x = 1:w;
    y = round(tan(a)*x); % calculate shift amount of each column
    for ii=x
        out(:, ii) = circshift(out(:, ii), y(ii), 1);
    end
else % the acute angle between stripes and x axis is less than pi/4
    dx = round(abs(d/sin(a))); % horizontal distance between stripes
    tx = max(1, round(abs(t/sin(a)))); % horizontal thickness of stripes
    n = ceil(w/dx); % maximum number of stripes
    out = repmat([false(1, tx), true(1, dx-tx)], h, n); % create vertical stripes
    y = 1:h;
    x = round(cot(a)*y); % calculate shift amount of each row
    for ii=y
        out(ii, :) = circshift(out(ii, :), x(ii), 2);
    end
end
out = out(1:h, 1:w); % crop the result, out may have more rows or columns than desired values (n*(dx or dy))
end

它完美地满足了我的需求,但我的问题在于它的性能。所以我发布了这个问题,看看是否有更好的方法。

【问题讨论】:

  • 你有没有运行分析器(函数'profile')并识别“慢行”?
  • @G.J 大约 75% 的运行时间花费在 circshift 行中。

标签: image matlab textures


【解决方案1】:

假设只需要与图形相关,我会坚持第一种方法并依赖抗锯齿技术。例如:

out = diagStripes(4 .* [h h], pi/6, 4*20, 0.03)
out = imresize(out, 0.25);

也许您必须保持灰度并在重新缩放后进行二进制转换。从你以前的方法:

binary = out < (cos(pi*0.03)+1)/2;

【讨论】:

    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2013-02-13
    • 2014-01-13
    相关资源
    最近更新 更多