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

xiaoxiao2021-03-06  41

I have always wanted to write an image processing software. Find a variety of image processing on the network. But it is often not a deep theory, which is the routine written in language such as C , which is very inconvenient. In fact, many times, I think is only a description, or pseudo code, which is both conducive to understanding, and also facilitates rewriting into any language. Recently, I wrote a small procedure of other image processing in some image processing you learned. Make a "packaging" that the processing skills you have mastered, I feel the inconvenience collection of information and learn some bites in VB. It will provide the algorithm to everyone for reference. I hope to have some friends who have been looking for online with me. To process an image, you first get the pixel value of the image, and the Picture control provided by the VB itself can open a lot of types of pictures, but it provides the Point method to read the pixel is too slow. The speed of using getpixel this API will not go, because the PionT method itself is a package for getpixel. To quickly get a relatively fast image in Picture is using a DIB method, of course, there is a DDB method, but use the DDB method, you need to consider the separate processing of images of different color depths, on the implementation of the program It is relatively complicated, and the use DIB method does not have to be, and it is limited slower than the DDB method.

Procedure 1: Get all pixels of images that open in the Picture control. Public Sub DibGet (ByVal IdSource As Long, XBegin As Long, ByVal YBegin As Long, ByVal XEnd As Long, ByVal YEnd As Long) Dim iBitmap As LongDim iDC As LongDim I As LongDim Dim W As LongDim H As LongOn Error GoTo ErrLineDone = FalseTimeGet = timeGetTimeInPutWid = xEnd - XBeginInPutHei = YEnd - YBeginW = InPutWid 1H = InPutHei 1I = (Bits / 8) - 1ReDim ColVal (I, InPutWid, InPutHei) With bi24BitInfo.bmiHeader .biBitCount = Bits .biCompression = 0 & .biPlanes = 1 .biSize = Len (bi24BitInfo.bmiHeader) .biWidth = W .biHeight = HEnd WithiBitmap = GetCurrentObject (IdSource, 7 &) GetDIBits IdSource, iBitmap, 0 &, H, ColVal (0, 0, 0), bi24BitInfo, 0 & DeleteObject iBitmapDone = TrueTimeGet = TimegetTime - TimeGetexit SuberRline: MsgBox "Error Number:" & Err.Number & ":" The set of parameters used in this process is just some of the parameters set and the API call, and does not involve algorithms. Procedure 2: Process of image output: Public Sub Dibput (Byval IDDESTINATION As Long DIM W AS Longdim H AS Long

On Error GoTo ErrLineDone = FalseTimePut = timeGetTimeW = OutPutWid 1H = OutPutHei 1With bi24BitInfo.bmiHeader .biWidth = W .biHeight = H LineBytes = ((W * Bits 31) And & HFFFFFFE0) / 8 .biSizeImage = LineBytes * HEnd WithSetDIBitsToDevice IdDestination , 0, H, COLout (0, 0), Bi24Bitinfo.bmiheader, 0 DONE = TRUETIMEPUT = TimegetTime - TimePuteXit SuberRline: MsgBox Err.DescriptionEND SUB Explains the process The global variables and data structures in the middle arrival, and the definition of the API.

API defines: delete a DCPrivate Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long delete an object Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long Select current object Private Declare Function GetCurrentObject Lib "gdi32 "(ByVal hdc As Long, ByVal uObjectType As Long) As Long Gets DIBPrivate Declare Function GetDIBits Lib" gdi32 "(ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitmapInfo, BYVAL WUSAGE AS long) As long Get System Time Private Declare Function TimegetTime LIB "Winmm.dll" () As long

Data structure definition: Private Type BitMapInfoHeader 'file information header --BITMAPINFOHEADER biSize As Long biWidth As Long biHeight As Long biPlanes As Integer biBitCount As Integer biCompression As Long biSizeImage As Long biXPelsPerMeter As Long biYPelsPerMeter As Long biClrUsed As Long biClrImportant As Long End TypePrivate Type RGBQuad rgbBlue As Byte rgbGreen As Byte rgbRed As Byte 'rgbReserved As ByteEnd TypePrivate Type BitMapInfo bmiHeader As BitMapInfoHeader bmiColors As RGBQuadEnd Type three data structures are indispensable in the DIB. We don't have to study, just copy the paste directly in order. Global variables used in the process: private const bits as long = 32 'color depth, here all images processes public done as boolean' for marking a process to end public timeget as long 'for recording input Process Treatment Time Public TimePut As Long 'Time DIM COLVAL () AS BYTE' for recording output process processing is used to store pixel value DIM coluout () as Byte 'input from DIB is used to store DIB output. Pixel Value DIM INPUTHEI AS Long 'The height of the input image is used to record the width of the input image. The dynamic array colval () and colord () do this make sense because we are not just to input and output images, and the pixels are also processed in the middle. Including image scaling, color adjustment, sharpening, softening, etc., using two different arrays to store data more conducive to the implementation of the program. Some sex-eager friends may have passed the program to the project, but it will find that the image cannot be output at all. This is because the images you have obtained with Dibget are still in colval (), you need to put them in the colord () this array, and the Dibput process can work.

Here, there is another process for array overall mobile data: public sub copyData (Byval W As Long, BYVAL H AS Long) DIM LENGTH AS Longdim i as longdim l aski = BITS / 8L = I - 1 Length = (W 1 &) * (H 1 &) * Iredim Colout (L, W, H) CopyMemory Colout (0, 0, 0), Colval (0, 0, 0), Length Subapi Definition: Private Declare Sub CopyMemory LIB "Kernel32" Alias ​​"RTLMoveMemory" (PDEST AS Any, PSRC As Any, Byval Bytelen As Long) This time we can try the effect: Turn your monitor to 32 color. Put all previous APIs and variables to a new module to create a new module, plus two Picture controls: Pictrue1, Picture2 A button Command1 loads a picture in Pictrue1 Write the following code: SUB command1_click () with picture1 .ScaleMode = 3 .BorderStyle = 0 DibGet .hdc, 0,0, .scalewidth, .scaleheightEnd WithCopyData InPutHei, InPutWidpicture2.AutoRedraw = TrueDibPut picture2.hdcpicture2.refreshend sub

When you run, the button presses, the picture in PICTREU1 is displayed immediately in Picture2.

At this time, you may say that you will post it for such a half day? Isn't it possible with PaintPicture? Yes, if you just have a picture, you really don't have to trouble, but the image processing section you want later will use the pixel value obtained by the front door. So, this is just a beginning, I really want to talk about something is still behind. Please continue to pay attention.

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

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)

(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.)

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

New Post(0)