简体   繁体   English

从Canvas中的Canny边缘检测创建形状

[英]Create shapes from Canny Edge Detection in Canvas

有没有人知道如何从画布中的Canny边缘检测创建形状?

I assume here you already have the Canny edge detection implemented based on the way the question is formulated - 我假设你已经根据问题的表达方式实施Canny边缘检测 -

You can use an approach such as this (written in Java but should be easy enough to translate into JavaScript) and/or perhaps some limited use of line-fitting approaches (statistical). 您可以使用诸如此类的方法(用Java编写,但应该很容易转换为JavaScript)和/或可能使用线条拟合方法 (统计)。

The essence is that you will have to find out which pixels are connected and create polygon objects/arrays yourselves based on the result of the edge detection. 其实质是你必须根据边缘检测的结果找出连接的像素并创建多边形对象/数组。

Once you have the connected pixels you can use point reduction algorithms such as the Ramer–Douglas–Peucker algorithm ( JavaScript implementation here ) to avoid the polygons to contain every single point of similar sloped lines and so forth. 一旦连接了像素,就可以使用点缩减算法,例如Ramer-Douglas-Peucker算法( 此处JavaScript实现 ),以避免多边形包含类似斜线的每一个点,依此类推。

You will run into a variety of challenges though such as short segmented lines due to too much noise in the original image or "weak lines", clusters of "lines" which makes it hard to find out how to connect them as polygons. 您将遇到各种各样的挑战,例如由于原始图像中的噪声太大而导致的短分段线或“弱线”,“线”的集群使得很难找到如何将它们连接为多边形。

I don't know of any libraries for this, however you could: 我不知道有任何库,但你可以:

  • Use getImageData() to access a byte[] of pixel data 使用getImageData()访问像素数据的byte []
  • Implement your own convolution filter on top of that data (examples for this may exist online) 在该数据之上实现您自己的卷积过滤器(此示例可能在线存在)

In this way you can find areas of high contrast (edges.) 通过这种方式,您可以找到高对比度的区域(边缘)。

EDIT I agree with Ken -- I may have misread the question. 编辑我同意肯 - 我可能误解了这个问题。

In addition to Ken's answer, if you know what kinds of shapes you're looking for then you might like to look at the Hough Transform which is well suited to detecting lines, ellipses and other shapes that can be geometrically defined using only a few parameters. 除了Ken的答案,如果你知道你正在寻找什么样的形状,那么你可能想看看Hough变换 ,它非常适合检测线条,椭圆和其他形状,只需几个参数即可几何定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM