Você está na página 1de 4

Photoshop and other graphics packages support custom image filters.

By specifying a small
table of numbers, many simple effects can be achieved. Depending on the graphics
application, the filter dialog should look something like this.

The custom filter dialog (Photoshop)

The dialog consists of a table of 25 weights, a scale value, and an offset value. These values are
all integers. Fractional numbers (e.g. 2.5) are not allowed.

The filter visits each pixel in the image sequentially, and for each pixel, a filtering formula is
applied. The current pixel in the process is represented by the middle box in the table. The box
immediately to the right represents the pixel immediately to the right of the current pixel. The
box below center represents the pixel below the current pixel, and so on. Each one of these
boxes has a number value indicating a weight. If the box is empty a weight of zero is assumed
(which means it's not used in the calculation).

The current pixel and its 24 neighbors are multiplied by their corresponding weights, and
these products are then added together. This value is divided by the "scale" value, and finally
the "offset" is added at the end. This value is the result of the filter, and it is done separately for
each channel in the image (e.g. red, green, and blue) and for each pixel in the image. The
results of the filter are stored in a separate image buffer until the process is complete, so the
pixel values used in the calculation are always from the original image, never from previously
filtered pixels. In other words, there is no "feedback" in the filtering process.
Consider the following example filter:

i=1 i=2 i=3 i=4 i=5 resultx,y = ( (weight3,3 × pixelx,y) + (weight4,4 × pixelx+1,y+1) ) /
scale + offset
j=1
resultx,y = ( (1 × pixelx,y) + (−1 × pixelx+1,y+1) ) / 1 + 128

j=2 resultx,y = pixelx,y − pixelx+1,y+1 + 128

j=3 1

j=4 −1

j=5

Scale: 1 — Offset: 128

You can read this formula as, the filtered pixel at the coordinates (x,y) is the value of the
original pixel at (x,y) minus the value of the original pixel at (x-1, y-1) plus 128. The unfilled
parts of the table are not included in the calculation. For the mathematically inclined, the
general equation for a custom filter can be expressed in the following form.

Custom filter formula

where i is each column of the filter table and j is each row. The weighti,j is the value entered in
the table at cell (i,j).
for how it works. Here are a few general rules for creating a custom filter that doesn't just
produce a mess. These are only guidelines, so feel free to bend the rules.

• The scale should usually be equal to the sum of all the weights.
• If the sum of the weights divided by the scale is 1, then the offset should be 0.
• If the sum of the weights is 0 then the offset should be 128.
• If the sum of the weights divided by the scale is −1 then the offset should be 255. The
image will be inverted from what it would be if the sum were +1.
• The sum of the weights divided by the scale should generally not be more than +1 or
less than −1. Otherwise the image will come out very dark or very bright.
• The scale can't be less than 1 (you can't divide by zero).
• If the filtered image is very dark, the scale is probably too high. If it's very light, the
scale is probably too low.

Lastly, here are a few examples of working custom filters and a sample image showing what
the effect looks like.

Original image. This filter is


the trivial case, only using the
value of the middle pixel.

Scale: 1 — Offset: 0
Gaussian blur. Averages all the
nearby pixels, giving more
weight to those in the middle.
A regular, non-Gaussian blur
would have all the values set to
1. Note the scale is set to 35,
which is the sum of all the
weights.

1 1 1 1 1

1 2 2 2 1

1 2 3 2 1

1 2 2 2 1

1 1 1 1 1

Scale: 35 — Offset: 0

Sharpen.

Você também pode gostar