我尝试在Javascript(特别是p5.js)中重新创建其他人似乎使用Mathematica套件成功完成的效果,如https://mathematica.stackexchange.com/a/39049所示。
我对Mathematica 100%无知,但我发现他们正在使用一种名为GradientOrientationFilter
的方法来创建一个跟随图像渐变方向的笔划模式。
我的结果仍不令人满意。
var img, vectors;
var pixelsToSkip = 2; // for faster rendering we can stroke less lines
var linesLength = 20;
var strokeThickness = 1;
function preload() {
img = loadImage('http://lorempixel.com/300/400/people/1');
img2 = loadImage('http://lorempixel.com/300/400/people/1');
/* you can test in local if the directions are correct using a simple gradient as image
img = loadImage('http://fornace.io/jstests/img/gradient.jpg');
img2 = loadImage('http://fornace.io/jstests/img/gradient.jpg');
*/
}
function setup() {
createCanvas(img.width, img.height);
noLoop();
img.loadPixels();
makeLumas();
makeGradients();
makeVectors();
for ( var xx = 0; xx < img.width; xx = xx + pixelsToSkip) {
for ( var yy = 0; yy < img.height; yy = yy + pixelsToSkip) {
push();
stroke(random(255)); // to color with pixel color change to stroke(img.get(xx, yy));
strokeWeight(strokeThickness);
translate(xx,yy);
rotate( vectors[yy][xx].dir ); // here we use the rotation of the gradient
line(-linesLength/2, 0, linesLength/2, 0);
pop();
}
}
// adding the image in overlay to evaluate if the map is good
// tint(255, 255, 255, 100);
// image(img2,0,0);
}
function draw() {
}
function makeLumas() {
// calculate the luma for each pixel to get a map of dark/light areas ("Rec. 601") https://en.wikipedia.org/wiki/Luma_(video)
lumas = new Array(img.height);
for (var y = 0; y < img.height; y++) {
lumas[y] = new Array(img.width);
for (var x = 0; x < img.height; x++) {
var i = x * 4 + y * 4 * img.width;
var r = img.pixels[i],
g = img.pixels[i + 1],
b = img.pixels[i + 2],
a = img.pixels[i + 3];
var luma = a == 0 ? 1 : (r * 299/1000 + g * 587/1000
+ b * 114/1000) / 255;
lumas[y][x] = luma;
}
}
}
function makeGradients() {
// calculate the gradients (kernel [-1, 0, 1])
var horizontalGradient = verticalGradient = [];
for (var y = 0; y < img.height; y++) {
horizontalGradient[y] = new Array(img.width);
verticalGradient[y] = new Array(img.width);
var row = lumas[y];
for (var x = 0; x < img.width; x++) {
var prevX = x == 0 ? 0 : lumas[y][x - 1];
var nextX = x == img.width - 1 ? 0 : lumas[y][x + 1];
var prevY = y == 0 ? 0 : lumas[y - 1][x];
var nextY = y == img.height - 1 ? 0 : lumas[y + 1][x];
horizontalGradient[y][x] = -prevX + nextX;
verticalGradient[y][x] = -prevY + nextY;
}
}
}
function makeVectors() {
// calculate direction and magnitude
vectors = new Array(img.height);
for (var y = 0; y < img.height; y++) {
vectors[y] = new Array(img.width);
for (var x = 0; x < img.width; x++) {
var prevX = x == 0 ? 0 : lumas[y][x - 1];
var nextX = x == img.width - 1 ? 0 : lumas[y][x + 1];
var prevY = y == 0 ? 0 : lumas[y - 1][x];
var nextY = y == img.height - 1 ? 0 : lumas[y + 1][x];
var gradientX = -prevX + nextX;
var gradientY = -prevY + nextY;
vectors[y][x] = {
mag: Math.sqrt(Math.pow(gradientX, 2) + Math.pow(gradientY, 2)),
dir: Math.atan2(gradientY, gradientX)
}
}
}
}
我在javascript中创建的字段比在Mathematica中创建的字段噪声更大。
http://jsfiddle.net/frapporti/b4zxkcmL/
我对p5.js很陌生,也许我在一段时间里重新发明轮子。也可以随意纠正我。
答案 0 :(得分:2)
对我来说有效的解决方案是在任何像素分析之前模糊,以平滑渐变方向滤镜的结果。
如果您使用的是p5.js,这可能就像img.filter("blur",5);
一样简单。否则,您可以使用您选择的任何其他模糊技术。
您可以在此处查看代码:http://jsfiddle.net/frapporti/b4zxkcmL/21/
如果某人有其他方式,我会在接受标记此答案之前等待。