Last time I talked about using the Dib method to get the pixel of the image. From this start, you will use the pixels you have already obtained to process the image.
There are many methods for image interpolation amplification, the most important of which are two linear interpolation and three linear interpolation.
This time I put the program used in the two linear interpolation algorithm published to everyone, I hope you want to use VB to write a similar program friend Help.
The API, data type, and global variables defined in the program refer to the previous article:
"VB image processing, (a) pixel acquisition and output"
Public Sub zoomimage (ByVal outputwidth as Long, ByVal outputheight as Long)
Dim I as Long
Dim L as Long
Dim X as Long
Dim Y as Long
Dim Xb as Long
Dim Yb as Long
Dim Xe as Long
Dim Ye as Long
Dim M as Integer
Dim N as Integer
Dim CurR as Long
Dim Curg as Long
Dim Curb as Long
Dim NXTR as Integer
Dim NXTG as Integer
Dim NXTB as Integer
Dim DR as Single
Dim DG as Single
Dim DB as Single
Dim DRt as Single
Dim DGt as Single
Dim DBt as Single
Dim Xratio as Single
Dim Yratio as Single
Dim Curstep as Single
Dim Nxtstep as Single
Dim Negn as Single
On Error GoTo Errline
If not canzoom Then Exit Sub
Done = False
Outputwid = OutPutWidth-1
Outputhei = OutputHeight-1
I = (Bits \ 8)-1
ReDim coltmp (I, Inputwid, Outputhei) ' first scaling from the y direction, the result is saved in this middle array
ReDim colout (I, Outputwid, Outputhei)
Xratio = Outputwid/inputwid
Yratio = Outputhei/inputhei
Timezoom = timeGetTime
Negn = 1/int (yratio + 1)
For X = 0 to Inputwid
CurR = Colval (0, X, 0)
Curg = Colval (1, X, 0)
Curb = Colval (2, X, 0)
Curstep = 0
Nxtstep = 0
For Y = 0 to InPutHei-1
Nxtstep = Curstep + yratio
Yb = Curstep
Ye = Nxtstep
N = Ye-yb
Coltmp (0, X, Yb) = CurR
Coltmp (1, X, Yb) = Curg
Coltmp (2, X, Yb) = Curb
M = Y + 1
NXTR = Colval (0, X, M)
NXTG = Colval (1, X, M)
NXTB = Colval (2, X, M)
If N > 1 Then
DRt = (nxtr-curr) * negn
DGt = (Nxtg-curg) * negn
DBt = (nxtb-curb) * negn
DR = 0
DG = 0
DB = 0
For L = Yb + 1 to Ye-1
Dr = Dr + DRt
DG = DG + DGt
db = db + DBt
Coltmp (0, X, L) = CurR + DR
Coltmp (1, X, L) = Curg + DG
Coltmp (2, X, L) = Curb + DB
Next
End If
Curstep = Nxtstep
CurR = nxtr
Curg = Nxtg
curb = Nxtb
Next
Coltmp (0, X, outputhei) = Nxtr
Coltmp (1, X, outputhei) = Nxtg
Coltmp (2, X, outputhei) = Nxtb
Next
Negn = 1/int (xratio + 1)
For Y = 0 to Outputhei
CurR = coltmp (0, 0, Y)
Curg = coltmp (1, 0, Y)
Curb = coltmp (2, 0, Y)
Curstep = 0
Nxtstep = 0
For X = 0 to InPutWid-1
Nxtstep = Curstep + xratio
Xb = Curstep
Xe = Nxtstep
N = XE-XB
Colout (0, Xb, Y) = CurR
Colout (1, Xb, Y) = Curg
Colout (2, Xb, Y) = Curb
M = X + 1
NXTR = coltmp (0, M, Y)
NXTG = coltmp (1, M, Y)
NXTB = Coltmp (2, M, Y)
If N > 1 Then
DRt = (nxtr-curr) * negn
DGt = (Nxtg-curg) * negn
DBt = (nxtb-curb) * negn
DR = 0
DG = 0
DB = 0
For L = Xb + 1 to Xe-1
Dr = Dr + DRt
DG = DG + DGt
db = db + DBt
Colout (0, L, Y) = CurR + DR
Colout (1, L, Y) = Curg + DG
Colout (2, L, Y) = Curb + DB
Next
End If
Curstep = Nxtstep
CurR = nxtr
Curg = Nxtg
curb = Nxtb
Next
Colout (0, Outputwid, Y) = Nxtr
Colout (1, outputwid, Y) = Nxtg
Colout (2, Outputwid, Y) = Nxtb
Next
Done = True
Timezoom = Timegettime-timezoom
Canput = True
Exit Sub
Errline:
MsgBox Err.Description
End Sub
Global variable definition:
Dim coltmp () as Byte is used to save interpolation intermediate variables
Dim Outputhei as Long ' to be interpolated to the target height
Dim Outputwid as Long ' to be interpolated to the target width
The time used by the public timezoom as long ' interpolation operation
Briefly explain the two-time linear interpolation algorithm.
(To illustrate the algorithm itself, we only calculate the red component of the picture, because the red-green-blue Three colors are calculated exactly the same way)
Suppose we have a very simple picture, the picture is only 4 pixels (2*2)
A B
C D
Now we're going to interpolate this picture to 9 pixels: 3*3
A AB B
AC ABCD BD
C CD D
The uppercase letters represent the original pixels, and the lowercase letters represent the new pixels that are worth inserting.
Presumably see this figure, we already have this algorithm in mind.
ab= (a+b)/2
Cd= (c+d)/2
Ac= (A+C)/2
Bd= (b+d)/2
Abcd= (AB+CD)/2= (A+B+C+D)/4
deduced: ab= A + (b-a)/2
Cd=c + (D-C)/2
...
It's very simple, right, to start with a new pixel that involves only two original pixels in one direction. Here we assume that the horizontal direction is calculated first.
In the vertical interpolation, because AB and CD are already in the front, so the ABCD calculation and calculate AC and BD no different.
It's possible for a friend to have thought of inserting the original image into the 4*4 or 5*5 method.
A AB1 ab2 B
AC1 ab1cd11 ab2cd21 BD1
AC2 ab1cd12 ab2cd22 Bd2
C CD1 CD2 D
deduced: AB1 = A + (b-a) * 1/3
AB2 = A + (b-a) * 2/3 =ab1+ (B-A)/3
CD1 = C + (d-c) * 1/3
CD1 = C + (d-c) * 2/3 =cd1+ (D-C)/3
...
Take A and B for example, first find the difference of the original pixel (a-b) and then calculate the increment of each step (a-b)/3
Then each new point is the value of the previous point plus the increment.
Here we assume the a=100, the b=255 magnification is 3, the horizontal direction interpolation
First calculate the difference of the original pixel: (b-a) = 255-100 =155
Then calculate the increment of each step in the horizontal direction: (a-b)/3=155/3 = 51.7
Here we use a variable DRT to record this increment (here is only a red example)
AB1 = A + DRt = 100+51.7 =151
AB2 = ab1 + DRt = 151+51.7 = 202
OK, actually two times linear algorithm is such a thing, not complex.
Maybe a friend of mine will have questions about the code I give you. A very simple algorithm why write so much code.
In fact, the answer is simple: to improve speed.
In VB "+" and "-" is always the fastest, "*" than "/" and "\" fast. No matter what type of variable it is.
Now let's analyze my program.
In my program, the two-direction interpolation is decomposed into two separate parts.
First put
A B
C D
Become:
A AB1...ABN B
C Cd1...cdn D
and become
A AB1...ABN B
Ac1 ... db1....
... ............ ...
AcN ... BdN.. .....
C Cd1...cdn D
These two-directional interpolation algorithms are exactly the same.
The two variables, xratio and yratio, are used to record the magnification in both horizontal and vertical directions.
So this process can also make the image scaling not in accordance with the original aspect ratio.
OK, add this module and the global variable to the last built project module.
Change the code in the button to:
Sub Command1_Click ()
With Picture1
. Scalemode=3
. Borderstyle=0
Dibget hdc, 0, 0,. ScaleWidth, ScaleHeight
Zoomimage,. ScaleWidth * 2,. ScaleHeight * 2
End With
Picture2. Autoredraw=true
Dibput PICTURE2.HDC
Picture2.refresh
End Sub
Has the image been magnified to twice times the original? Isn't that a slow speed?
What the? Very slow? First compile into EXE and then run it.
About two times linear interpolation that's it, the next time you're going to say some basic filter effects: sharpening, softening, spreading, carving
Please continue to pay attention to
(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. )
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.