|Page (1) of 1 - 01/29/02||email article||print page|
Creating Photoshop Filters, Part 5Convolving images with the cnv function
If you remember your terminology, you'll recall that we left off with two functions and four operators. Operators are things like multiplication, division, addition and subtraction. Functions we covered included "src" for evaluating and manipulating pixel positions and "rnd" for creating noise effects. Convolve, expressed as "cnv," works almost the same way as these last two but produces radically different effects.
Don't worry. Just as in the previous installments, we're not going to be doing any real programming. Just playing with some numbers and learning to mind our parenthetical expressions.
Convolution less convoluted
The first thing we're going to do here is a little function that can generate a whole lot of effects, from blurs to more psychedelic effects. This is a function that looks at a pixel then looks at its surrounding pixels and does something to the whole lot of them.
For the purpose of writing expressions involving the convolve functions, pixels are represented as follows:
In this diagram, you see nine pixels, each represented by a gray box. The way each one is represented in a convolve expression is like this: cnv(Top Left, Top Center, Top Right, Middle Left, Center, Middle Right, Bottom Left, Bottom Center, Bottom Right, Divisor)
That is to say, you assign a value for each pixel surrounding the center pixel, and the values are divided by the divisor.
So, for example, if you were to write the expression cnv(0,1,0,1,4,1,0,1,0,8), the Top Left pixel would be given the value 0; the Top Center pixel would have the value 1; the Top Right pixel would have the value 0; and so forth. The last number in the expression, in this case 8, is the divisor.
First let's just try a simple application of this principle by blurring the image. This is accomplished like by placing the above sample expression in each of the R, G and B channels in the Filter Factory: cnv(0,1,0,1,4,1,0,1,0,8). (Remember, you can copy and paste expressions from here to the Filter Factory, so you don't have to type things manually.)
That wasn't so tough. So how could we take this function a step further? Well, we don't want to put our sliders to waste, do we? So let's just substitute slider values for regular, old numbers. We only have eight sliders, though, so we'll have to repeat them for now. So try: cnv(ctl(0),ctl(1),ctl(2),ctl(3),ctl(3),ctl(3),ctl(4),ctl(5),ctl(6),ctl(7)) and play around with the sliders. Notice that the divisor (ctl(7)) has the most impact on the way the other sliders behave. This is because any values you select in the first six sliders are added together and divided by the last.
You might also want to switch positions of the sliders so that no individual channels share a slider, as in the example below.
Using the above expressions and tweaking around with the sliders, I was able to get the following result. (In case you can't guess, the top image is the original.)
Convolution more convoluted
So that's pretty fancy. But we can also bring in one of the functions we learned in Part 1 to make the thing do some even funkier stuff. (And I do mean funk-ay.) The function we used in Part 1 was "src" to tell Filter Factory what to do with the individual pixels and their positions in each channel. So now let's try wrapping this src function around our cnv function. That is to say, with a source function, we have src(x,y,channel). (Channel is represented as a number 0 through three for red, green, blue and alpha, respectively.) Instead of x and y, let's use the convolve expression. It will basically be cnv(expression,expression,channel). Try this one, remembering to separate each expression with a comma and remembering to put the correct channel number at the end:
R: src(cnv(ctl(0),ctl(0),ctl(0),ctl(0),ctl(0),ctl(0),ctl(0),ctl(0),ctl(0),ctl(6)), cnv(ctl(1),ctl(1),ctl(1),ctl(1),ctl(1),ctl(1),ctl(1),ctl(1),ctl(1),ctl(7)),0)
G: src(cnv(ctl(2),ctl(2),ctl(2),ctl(2),ctl(2),ctl(2),ctl(2),ctl(2),ctl(2),ctl(6)), cnv(ctl(3),ctl(3),ctl(3),ctl(3),ctl(3),ctl(3),ctl(3),ctl(3),ctl(3),ctl(7)),1)
B: src(cnv(ctl(4),ctl(4),ctl(4),ctl(4),ctl(4),ctl(4),ctl(4),ctl(4),ctl(4),ctl(6)), cnv(ctl(5),ctl(5),ctl(5),ctl(5),ctl(5),ctl(5),ctl(5),ctl(5),ctl(5),ctl(7)),2)
Here's what it might look like, depending upon your slider settings:
Again, ctl(6) and ctl(7), otherwise known as sliders 7 and 8, will have the most impact on the final result, since the other sliders are added up and divided by them. Tweak around with it for some psychedelic effects.
Next week we'll examine the convolve function more with some additional mathematical functions. Then, in our final installment, we'll look at ways to use conditional expressions. If you have questions in the meantime, feel free to e-mail me at the address below or post in one of the forums listed below.
Dave Nagel is the producer of Creative Mac and Digital Media Designer; host of several World Wide User Groups, including Synthetik Studio Artist, Adobe Photoshop, Adobe InDesign, Adobe LiveMotion, Creative Mac and Digital Media Designer; and executive producer of the Digital Media Net family of publications. You can reach him at [email protected].
Related Keywords:Creating Photoshop Filters, Part 5 Convolving images with the cnv function
Source:Digital Media Online. All Rights Reserved