javascript - 确定Javascript中黑色和白色像素之间的距离

标签 javascript image-processing

我计划在这个项目中使用 JavaScript(但我愿意使用其他东西)。我在 javascript 中加载图像,当我在图像上放置一个点时,我想计算从放置的点到第一个黑色或灰色像素的 x 和 y 距离。

Example Image

所以我将红点放在图像上,然后我想向用户显示从所选点到第一个黑色像素的 x、y 距离。距离可以以像素为单位(我不介意)。这可能吗?有人可以帮我吗?

谢谢!

最佳答案

另一种方法是再次使用 getImageData 函数,正如 @Joseph the Dreamer 所建议的,但您可以执行以下操作而不是按方向搜索:

// the context to the canvas which holds your map
var ctx {...};

var point = {x:x, y:y};
// this gets a 3 by 3 bitmap data from your canvas with the centre being your point
var search = ctx.getImageData(x - 1, y - 1, 3, 3);

var match = false;
while(!match)
{
    // iterate over the search array looking for a black or grey pixel
    // and add the co ordinates of the matches into another array

    // if we found matches in this loop, calculate the shortest length match
    // and then break out of the loop

    // otherwise get a bigger sample data to search through and do this loop again
    // you could optimise this by skipping the pixels you looked through
    // in the previous loop
}

关于javascript - 确定Javascript中黑色和白色像素之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16054963/

相关文章:

c++ - 在opencv中将2张图像与透明蒙版结合起来

android - 图像格式RGB888和ARGB8888的区别

javascript - 第一个函数作为异步函数的柯里函数不起作用

python - 用于图像缩放的双三次插值

image-processing - FernDescriptorMatch - 如何使用它?怎么运行的?

javascript - FP 中的代数效应是什么意思?

image-processing - 1x2和1x3图像梯度核滤波器定义有什么区别

javascript - NodeJS应用的问题

javascript - 当快速悬停在导航链接上时,jquery mouseleave 事件没有被触发?

javascript - 使用 Jquery 获取与条件匹配的第一个前一个元素