VB image processing, (three) several commonly used filters to achieve 1

Source: Internet
Author: User
Tags integer
The filter is preceded by the application of the two-time linear interpolation.
This article to tell you about sharpening, softening, diffusion, carving these several filters to achieve.

One, sharpen

Sharpening the algorithm is very simple, is to compare the adjacent several pixels, the current pixel plus and the surrounding pixels of the difference on it.
Here I give an example:
A B C D
E F G H
I J K L
M N O P
Suppose there is a picture, 4*4, a total of 16 pixels, respectively, represented by A--l.
We first look at this picture, only the middle of the f,g,j,k four pixels of the "neighbor" is full.


For the sake of simplicity, we only deal with these 4 pixels, because in the actual picture because the size of the picture is a lot of pixels, so the surrounding a circle of pixels do not handle does not affect the final effect.
Calculate the difference first: Delta= F-(a+b+c+e+g+i+j+k)/8
(A+B+C+E+G+I+J+K)/8 is the average of the pixels around F,
Multiply this mean by a factor and add it to f, and you get a new F value:
F=f + Delta * Alpha
This factor alpha is sharpening, and changing this factor can result in different sharpening effects. But generally are made relatively small, such as: 0.3
So, we just use two loops to traverse the entire image pixel value (remove the boundary) can get a sharpening effect.


But you may find that the value of the previous point is not the original value when dealing with the following points.
For example, when dealing with g, you need to use the value of F, and F has been changed, and the change of F is related to the value of G, which will become a circular reference. To avoid the whole problem, here's an improved approach:
A B C D
E F G H
I J K L
M N O P
Let's start with point A, and change the difference calculation method to:
Delta= A-(b+e+f)/3
F=f + Delta * Alpha
All pixels are scanned from left to right, from top to bottom, so that the pixels that have been processed are not encountered in the calculation, and the entire process is speeded up by reducing the pixels involved in the operation.

According to our VB image processing, (a) pixel acquisition and output has been obtained in the array of pixels. We can write this:
Public Sub Sharp (Optional ByVal sharpdgree as single = 0.3)
Dim X as Long
Dim Y as Long
Dim Ix as Long
Dim Iy as Long
Dim Diff as Long
Dim Diff1 as Long
Dim Div1 as Single
Dim Div2 as Single
Dim Max as Long
On Error GoTo Errline

Max = 255
Done = False
Timefilter = timeGetTime
Templatesize = 1
Sensitivity = Sensitivity * 9
DIV1 = 1 + sharpdgree
Div2 =-SHARPDGREE/3
For X = 0 to OutPutWid-1
For Y = 0 to OutPutHei-1
RR = colout (0, X, Y) * DIV1
GG = Colout (1, X, Y) * DIV1
BB = Colout (2, X, Y) * DIV1
Ix = X + 1
Iy = Y + 1
R = colout (0, Ix, Iy)
R = r + colout (0, X, Iy) + colout (0, Ix, Y)
G = Colout (1, Ix, Iy)
g = g + colout (1, X, Iy) + colout (1, Ix, Y)
B = Colout (2, Ix, Iy)
B = B + colout (2, X, Iy) + colout (2, Ix, Y)
R = R * Div2
g = g * Div2
b = b * DIV2
RR = RR + R
GG = GG + G
bb = BB + B
If RR < 0 Then rr = 0
If RR > Max Then RR = max
If GG < 0 Then GG = 0
If GG > Max Then GG = max
If bb < 0 Then bb = 0
If bb > Max Then BB = max
Colout (0, X, Y) = RR
Colout (1, X, Y) = GG
Colout (2, X, Y) = BB
Next
Next

Done = True
Timefilter = Timegettime-timefilter
Exit Sub
Errline:
Done = True
MsgBox Err.Description
End Sub

Because a new value greater than 255 or less than 0 occurs during the calculation of new pixels, it must be judged after the calculation is complete.

The global variables that are used:
Public Timefilter as Long ' is used to record the time spent in filter processing
Dim RR as Long ' for saving red components
Dim GG as Long ' for preserving green components
Dim BB as Long ' for storing blue components

Second, the softening of

The softening algorithm is similar to sharpening, but in contrast, the current point is replaced with the average of several points around it.
A B C D
E F G H
I J K L
M N O P
Calculation method:
f= (a+b+c+e+f+g+i+j+k)/9
g= (b+c+d+f+g+h+j+k+l)/9
...
...
Specific procedures, I do not wordy here, we just have to change the program on the above can be a little.

Three, diffuse, produce a kind of effect like watercolor.
The algorithm is simple, which is to replace the current point with the immediate points around it.
A B C D
E F G H
I J K L
M N O P
The F point can be arbitrarily selected from the a,b,c,e,g,i,j,k around it.
The G-Spot can be arbitrarily selected from the b,c,d,f,h,j,k,l around it.
The J-point can be arbitrarily selected from the e,f,g,i,k,m,n,o around it.
The K-point can be arbitrarily selected from the f,g,h,j,l,n,o,p around it.
As for choosing which point, you can select it with a random number.

Four, carving
Subtracts adjacent two pixels, gets the difference plus 127 as the new value
A B C D
E F G H
I J K L
M N O P
If we were to "carve" in the direction from left to right,
a=b-a+127
b=c-b+127
c=d-c+127
...
If we were to "carve" in the direction from the top down,
a=e-a+127
b=f-b+127
c=g-c+127
...
Of course, we can also from the more direction of "carving" such as: down to the left, upper right, upper left, lower right ... And so on, there are altogether 8 directions to choose from.
The other 127, is the "carving" effect after the brightness. We can write the direction and brightness of the carving as parameters in the process
Public Sub Emboss (Optional embossdirection As Integer, Optional lighteness As Integer)
...

These filter algorithms are relatively simple, it is easy to use VB to achieve.


Given the length of the relationship this time here, next time to talk with you about the effect of pencil and wood carving. Please continue to pay attention to
For the first two period, please refer to the following:
"VB image processing, (a) pixel acquisition and output"
"VB image processing, (ii) Two linear interpolation application"

(here just said my own in the process of writing the method used, there are a lot of deficiencies.) And because in the paste up when made some changes, there may be some mistakes, please do not hesitate to enlighten me, will you use a better way to provide, I would appreciate. )




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.