Hi all!
Essentially what I’m trying to do is take an image file, import it into Unity and have unity scan the image for shapes (or black lines that make up shapes) and convert each shape into verticies. I’ve found tutorials on creating 2d mesh’s from vertices but I’d like to be able to pull that information from an image like the one I’ve attached. If anyone can point me in the right direction I’d really appreciate it.
I’m not quite sure how to phrase the question, so I’m sorry if this has been answered somewhere else.
As i’ve understood it, you want to
- detect lines in an image
- create a mesh that closely represents these lines
You might try to do it yourself with something like Canny Edge Detection.
Another possibility would be to convert your images to .svg’s externally and then extract the vertices from that vector image.
This seems to cover svg parsing pretty well.
This sounds quite similar to what was asked over here. I’ve created this WebGL example. The code is on pastebin.
You can fill the shapes and use Marching Squares to generate the outline.
Another way, which was brought up, was the Canny edge detection. You’ll have to fill the shapes, or you’ll get back two contours, when you only want one. OpenCV has all of this for you. Then you can use the contour points as your vertices.
For anyone else looking for another way to do it, here’s another way I figured out. The image can’t have an alpha layer though.
void CountBlackPixels()
{
for (int y = 0; y < texture.height; y++)
{
for (int x = 0; x < texture.width; x++)
{
float Scale = texture.GetPixel(x, y).grayscale;
if (Scale<GrayscaleDefinition)
{
//Pixel is considered black
Vector2 V2 = new Vector2(x,y);
BlackDots.Add(V2);
}
}
}
}
void IdentifyShapes()
{
while (BlackDots.Count > 0)
{
ShapeObj NewShape = new ShapeObj();
List<Vector2> NshapePoints = new List<Vector2>();
Vector2 startingP = BlackDots[0];
NshapePoints.Add(startingP);
BlackDots.Remove(startingP);
Node N = new Node(startingP, true);
List<Node> AvailableNodes = new List<Node>();
AvailableNodes.Add(N);
while (AvailableNodes.Count > 0)
{
AvailableNodes[0].FindNeighbors();
foreach (Vector2 V in AvailableNodes[0].Neighbors)
{
if (BlackDots.Contains(V))
{
NshapePoints.Add(V);
Node Nd = new Node(V, true);
AvailableNodes.Add(Nd);
BlackDots.Remove(V);
}
}
AvailableNodes.RemoveAt(0);
}
NewShape.DataPoints = NshapePoints;
AllShapes.Add(NewShape);
}
}