Kevin
Kevin

Reputation: 99

Painting stroke generation algorithm for robot arm

I am writing a code that generate start and end points of strokes of a picture (Raster images) to let robot arm paint.

I have wrote an algorithm but with too many overlapping strokes: https://github.com/Evrid/Painting-stroke-generation-for-robot-arm-or-CNC-machine

The input of my algorithm:

enter image description here

and the output (which is mirrored and re-assigned to the colors I have) with 50 ThresholdOfError (you can see the strokes are overlapping): enter image description here

Things to notice are:

*The strokes needs to be none overlapping (if overlapping then have too many strokes)

*Painting have different colors, the same color better draw together

*The stroke size is like rectangles

*Some coloring area are disconnected, like below only yellow from a sun flower: enter image description here

I am not sure which algorithm should I use, here is some possible ones I have thought about:

Method 1.Generate 50k (or more) random direction and position large size rectangles, if its area overlap the same color area and not overlapping other rectangles, then keep it, then decrease generated rectangle size and after a couple rounds keep decreasing again

Method 2.Extract certain color first then generate random direction and position large size rectangles (we have less area and calculation time)

Method 3.Do edge detection first, then rectangles are generated with direction along the edge, if its area overlap the same color area and not overlapping other rectangles, then keep it, then decrease generated rectangle size and after a couple rounds keep decreasing again

Method 4: Generate random circle, let the pen draw points instead (but may result too many points)

Any suggestions about which algorithm I should use?

Upvotes: 2

Views: 337

Answers (2)

Spektre
Spektre

Reputation: 51873

I would start with:

  1. Quantize your image to your palette

    so reduce colors to your palette first see:

  2. segmentate your image by similar colors

    for this you can use flood fill or growth fill to create labels (region index) in form of ROI

    see Fracture detection in hand using image proccessing

  3. for each ROI create infill path with thick brush

    this is simple hatching you do this by generating zig zag like path with "big" brush width in major direction of ROI so use either AABB or OBB or PCA to detect major direction (direction with biggest size of ROI) and just AND it with polygon ROI

  4. for each ROI create outline path with "thin" brush

    IIRC this is also called contour extraction, simply select boundary pixels of selected ROI then you can use A* on ROI boundary to sort the pixels into 2 halves (or more if complex shape with holes or thin parts) so backtrack the pixels and then reorder them to form a closed loop(s)

    this will preserve details on boundary (while using infill with thick brush)

Something like this:

ROI

In case your colors are combinable you can use CMY color space and Substractive color mixing and process each C,M,Y channel separately (max 3 overlapping strokes) to have much better color match.

If you want much better colors you can also add dithering however that will slow down the painting a lot as it requires much much more path segments and its not optimal for plotter with tool up/down movement (they are better for printing heads or printing triggered without additional movements ...). To partially overcome this issue you could use partial dithering where you can specify the amount of dithering created (leading to less segments)

there are a lot of things you can improve/add to this like:

  • remove outline from ROI (to limit the overlaps and prevent details overpaint)
  • do all infills first and then all outlines
  • set infill brush width based on ROI size
  • adjust infill hatching pattern to better match your arm kinematics
  • order ROIs so they painted faster (variation of Traveling Sailsman problem TSP)
  • infill with more than just one brush width to preserve details near borders

Upvotes: 1

ravenspoint
ravenspoint

Reputation: 20596

Suggest you use the flood fill algorithm.

When the entire picture has been covered, sort the rectangles by color.

Upvotes: 1

Related Questions