How to produce Photoshop stroke effect?

2019-05-18 19:08发布

问题:

I'm looking for a way to programmatically recreate the following effect:

Give an input image:
input http://www.shiny.co.il/shooshx/ConeCarv/q_input.png

I want to iteratively apply the "stroke" effect.
The first step looks like this:
step 1 http://www.shiny.co.il/shooshx/ConeCarv/q_step1.png

The second step like this:
alt text http://www.shiny.co.il/shooshx/ConeCarv/q_step2.png

And so on.

I assume this will involves some kind of edge detection and then tracing the edge somehow.
Is there a known algorithm to do this in an efficient and robust way?

回答1:

Basically, a custom algorithm would be, according to this thread:

Take the 3x3 neighborhood around a pixel, threshold the alpha channel, and then see if any of the 8 pixels around the pixel has a different alpha value from it. If so paint a circle of a given radius with center at the pixel. To do inside/outside, modulate by the thresholded alpha channel (negate to do the other side). You'll have to threshold a larger neighborhood if the circle radius is larger than a pixel (which it probably is).


This is implemented using gray-scale morphological operations. This is also the same technique used to expand/contract selections. Basically, to stroke the center of a selection (or an alpha channel), what one would do is to first make two separate copies of the selection. The first selection would be expanded by the radius of the stroke, whereas the second would be contracted. The opacity of the stroke would then be obtained by subtracting the second selection from the first.

In order to do inside and outside strokes you would contract/expand by twice the radius and subtract the parts that intersect with the original selection.

It should be noted that the most general morphological algorithm requires O(m*n) operations, where m is the number of pixels of the image and n is the number of elements in the "structuring element". However, for certain special cases, this can be optimized to O(m) operations (e.g. if the structuring element is a rectangle or a diamond).