Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating string art from image

I am relatively new to python. I would like to make some string-art portraits. I was watching this video which really intrigued me:

https://youtu.be/RSRNZaq30W0?t=56

enter image description here

I understand that to achieve this, I would first need to load the image, then do some edge-detection and then use some form of Delaunay triangulation but have no idea where to even start.

I looked up some sample code for OpenCV and figured out how to do basic edge-detection. How do I then convert those to points? And then what sort of algorithm would I need to "fill in" the different gradients?

I don't even know if this is the right approach to achieve this. Could someone please point me in the right direction and perhaps give me some sample code to get started? I would really appreciate it very much.

like image 491
Bruno Avatar asked May 05 '19 18:05

Bruno


People also ask

What kind of thread do you use for string art?

You can use sewing thread for string art, but it is only advised to do so if you want a very delicate looking outcome. Other options include embroidery floss or thin yarn, which are slightly thicker than sewing thread and are easier to work with. However, it really does come down to the look you are wanting to achieve.


1 Answers

Edge detection or triangulation is less important in this application. The core part is to understand the pseudo-code at 1:27 of the video. The final product uses a single string at wrap around different nails in particular way, so that: darker areas in original image have less string density, and brighter areas have more strings crossing over.

The initial preparation is to:

  • generate an edge dection version of the image (A)
  • generate a blurred version of the image (B)

Then the first step is to create random positions for the nails. Apparently to achieve a good outcome, if a random-generated nail is close enough to the 'edge' of a black-white image, you should 'snap' it to the edge, so that later the strings wrapping around these edge nails will create an accurate boundary just like in the original picture. Here you use the image A) to adjust your nails. For example, just perform some potential minimization:

  1. Add small random position change to the nails. If a nail now gets close enough to a white point (edge) in image A), directly change to that position.

  2. Compute the potential. Make sure your potential function penalizes two points that come too close. Repeat 1) 100 times to pick one with lowest potential.

  3. Iterate 1) and 2) 20 times

Next you decide how you want the strings to wrap around the nails.

  • Starting from a point A, look at some neighboring points (within certain radius) B1, B2, B3, etc. Imagine if you attach a string with certain width from A to Bi, it visually changes your string image P in a slight way. Render line segment A-B1 on P to get P1, render A-B2 on P to get P2, etc.
  • Find the best Bi so that the new image Pi looks closer to the original. You can just do a pixel-wise comparison between the string image and the original picture, and use this measurement to score each Bi. The video author used a blurred image B) to get rid of textures that may randomly impact his scoring algorithm.
  • Now the optimal Bi becomes the new A. Find its neighbors and loop over. The algorithm may stop if adding any new strings only negatively impacts the score.

There are cases where bright areas in a photo are widely separated, so any white strings crossing the dark gap will only decrease the score. Use your judgement to tweak the algorithm to workaround those non-convex scenarios.

like image 165
dchneric Avatar answered Sep 19 '22 23:09

dchneric