VB image processing, (2) Application of secondary linear interpolation

xiaoxiao2021-03-06  40

Last time, the use of DIB methods to acquire image pixels. From this beginning, the image will be processed if the resulting pixel is used. There are many ways to enlarge image interpolation, the most important secondary linear interpolation and three linear interpolation. This time I announced the algorithm of the secondary linear interpolation used in my own program to everyone, I hope that friends who have to use VB to write similar programs will be helpful. Please refer to the API, data type, and global variables used in the program. "" VB image processing, (1) acquisition and output of pixels

Public Sub ZoomImage (ByVal OutPutWidth As Long, ByVal OutputHeight As Long) Dim I As LongDim L As LongDim X As LongDim Y As LongDim Xb As LongDim Yb As LongDim Xe As LongDim Ye As LongDim M As IntegerDim N As IntegerDim CurR As LongDim CurG As LongDim CurB As LongDim NxtR As IntegerDim NxtG As IntegerDim NxtB As IntegerDim DR As SingleDim DG As SingleDim DB As SingleDim DRt As SingleDim DGt As SingleDim DBt As SingleDim Xratio As SingleDim Yratio As SingleDim CurStep As SingleDim NxtStep As SingleDim NegN As SingleOn Error GoTo ErrLineIf Not CanZoom Then Exit SubDone = FalseOutPutWid = OutPutWidth - 1OutPutHei = OutputHeight - 1I = (Bits / 8) - 1ReDim ColTmp (I, InPutWid, OutPutHei) 'processing start scaling in the Y direction, this intermediate result is stored in the array ReDim ColOut (I , OutputWid, Outputhei xratio = OutputWid / InputWidyratio = 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 coolmp (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 coolmp (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 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 NextDone = TrueTimeZoom = timeGetTime - TimeZoomCanPut = TrueExit SubErrLine : MsgBox err.descriptionEND SUB global variable definition: DIM colTmp () AS BYTE 'Used to Save Intermediate Variable Dim OutputHei As Long' To Interpolated Target Height Dim OutputWid As Long 'To Interpret the Target Width PUBLIC TIMEZOOM AS Long' Interpolation Operation Use time

Briefly explain the secondary linear interpolation algorithm. (To illustrate the algorithm itself, we only calculate the red component of this picture, because the calculation method of the red green blue color is identical to the same way, we have a very simple picture, the picture has only 4 pixels (2 * 2) a BC D Now we have to interpolate this image to 9 pixels: 3 * 3A Ab Bac Abcd BDC CD D, the above cases represent the original pixels, lowercase letters represent new pixels worthwhile. If you want to see this picture, everyone has this algorithm. AB = (A B) / 2 CD = (C D) / 2ac = (A C) / 2BD = (B D) / 2ABCD = (AB CD) / 2 = (A B C D) / 4 Detection: AB = a (ba) / 2 CD = C (DC) / 2 ... very simple, right, first from one direction involving only two original pixels. Let's assume a first calculation level. When the interpolation is perpendicular to the vertical direction, because AB and CD have been calculated in front, the calculation of ABCD is also different from calculation AC and BD. It is possible to have a friend who has thought of the original image to 4 * 4 or 5 * 5. A ab1 ab2 bac1 AB1CD11 AB2CD21 BD1AC2 AB1CD12 AB2CD22 BD2C CD1 CD2 D

Defrive: ab1 = a (ba) * 1/3 AB2 = a (ba) * 2/3 = AB1 (BA) / 3 CD1 = C (DC) * 1/3 CD1 = C (DC) * * 2/3 = CD1 (DC) / 3 ... Take A and B as an example, first seek the difference (AB) of the original pixel, then calculate the increment increment (AB) / 3, then every new point is The value of the previous point plus this increment is.

Here we assume that a = 100, b = 255 magnification is 3, the horizontal direction is interpolated first calculated the difference in raw pixels: (b-a) = 255-100 = 155

Then calculate the incremental incrementation of each step in the horizontal direction: (ab) / 3 = 155/3 = 51.7 Here we use a variable DRT to record this increment (here only using red to do an example) AB1 = A DRT = 100 51.7 = 151Ab2 = ab1 DRT = 151 51.7 = 202

Ok, in fact, the secondary linear algorithm is such a thing and is not complicated. Maybe there is a question about the code I give. Very simple algorithm why you want to write so much code.

In fact, the answer is simple: in order to improve the speed.

" " And "-" in VB are always the fastest, "*" is more than "/" and "/". No matter what type of variables are like this.

Let's analyze my procedure again. Decompose the interpolation in my program into two separate parts. First turn the A BC D to: a ab1 ... ABN BC CD1 ... CDN D becomes a ab1 ... ABN BAC1 ............. DB1 ... .. .............................. BDNC CD1 ... CDN D These two direction interpolation algorithms are identical to Xratio and Yratio A variable is used to record magnification in the horizontal direction and the vertical direction. So this process can also make the image scaling not in accordance with the original aspect ratio. Ok, add this module and global variable to the previously established engineering module. The change button code: sub command1_click () With picture1 .ScaleMode = 3 .BorderStyle = 0 DibGet .hdc, 0, 0, .scalewidth, .scaleheight ZoomImage, .scalewidth * 2, .scaleheight * 2End Withpicture2.AutoRedraw = Truedibput Picture2.hdcpicture2.Refreshend Sub Is the image has been zoomed twice? Is the speed very slow? what? very slow? Compile to EXE and run it first. The following is the rendering:

Original image:

Secondary linear interpolation is 5 times: about the secondary linear interpolation, it will say this, the next time you will say some basic filter effect: sharpening, softening, spread, carving, please continue to pay attention

Other articles: VB image processing, (1) acquisition and output of pixels

(Here, I just said that I used the method of writing the program, there is a lot of shortcomings. Because some modifications are made in the post, there may be some errors, please enlighten you, you will use you. A better way to provide it, I will be grateful.)

VB image processing, (b) Application VB image processing, (3) Several common filter implementation 1VB image processing, (4) implementation 2 VB image processing of several common filters, (5) image Color correction

VB image processing, (six) brightness contrast adjustment

VB image processing, (7) an algorithm for a neighboring mean filter (dust, noise)

转载请注明原文地址:https://www.9cbs.com/read-74582.html

New Post(0)