VB image processing, (a) pixel acquisition and output

Source: Internet
Author: User
Tags arrays definition data structures goto integer
Always want to write an image processing of their own software. On the network to find a variety of image processing technology articles.
But find often not advanced theory, is to use C + + and other languages written out of the routine, very inconvenient.
In fact, most of the time, I want to find a description, or pseudo code, both for understanding, but also conducive to rewriting the version of any language.
Recently, I have learned some of the image processing of knowledge to write a imagecast small program. I have mastered the processing skills of a "packaging", feeling the inconvenience of data collection and learning VB in the dribs and drabs. The algorithm is provided to everyone for reference. I hope that some of the same as me on the internet to find friends have a master.

To process an image, first get the pixel value of the image, while the picture control provided by VB itself can open many types of pictures, but the point method it provides is too slow to read pixels. And the use of GetPixel this API speed is also faster than where to go, because the Piont method itself is for the getpixel of a package.
A quick way to get an image open in the picture in VB is to use the Dib method, there are, of course, the DDB approach, but using the DDB method you also need to consider the different color depth of the image processing, in the implementation of the program is relatively complex, and the use of the DIB method is not necessary, And the processing speed is also slower than the DDB method is limited.


Process one: Gets all the pixels of an image that is opened 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 Long
Dim IDC as Long
Dim I as Longdim
Dim W as Long
Dim H as Long

On Error GoTo Errline
Done = False
Timeget = timeGetTime
Inputwid = Xend-xbegin
Inputhei = Yend-ybegin
W = Inputwid + 1
H = Inputhei + 1

I = (Bits \ 8)-1
ReDim Colval (I, Inputwid, Inputhei)
With Bi24bitinfo.bmiheader
. biBitCount = Bits
. bicompression = 0&
. biplanes = 1
. bisize = Len (Bi24bitinfo.bmiheader)
. Biwidth = W
. Biheight = H
End With

Ibitmap = Getcurrentobject (Idsource, 7&)
GetDIBits Idsource, Ibitmap, 0&, H, Colval (0, 0, 0), Bi24bitinfo, 0&
DeleteObject Ibitmap
Done = True
Timeget = Timegettime-timegetexit Sub
Errline:
MsgBox "Error number:" & Err.Number & ":" & Err.Description
End Sub
In this process, only some parameter settings and API calls are used, and no algorithms are involved.

Process two: The process of image output:
Public Sub dibput (ByVal iddestination as Long)
Dim W as Long
Dim H as Long

On Error GoTo Errline
Done = False
Timeput = timeGetTime

W = Outputwid + 1
H = Outputhei + 1

With Bi24bitinfo.bmiheader
. Biwidth = W
. Biheight = H
Linebytes = ((W * Bits +) and &hffffffe0) \ 8
. biSizeImage = linebytes * H
End With
SetDIBitsToDevice iddestination, 0, 0, W, h, 0, 0, 0, H, colout (0, 0, 0), Bi24bitinfo.bmiheader, 0

Done = True
Timeput = Timegettime-timeput
Exit Sub
Errline:
MsgBox Err.Description
End Sub


The following explains the global variables and data structures that are in the process, and the definition of the API.

API definition:
Delete a DC
Private 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 the current object
Private Declare Function getcurrentobject Lib "GDI32" (ByVal hdc as Long, ByVal Uobjecttype as long) as long
Get DIB
Private 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 Type

Private Type Rgbquad
Rgbblue as Byte
Rgbgreen as Byte
Rgbred as Byte
' Rgbreserved as Byte
End Type

Private Type Bitmapinfo
Bmiheader as Bitmapinfoheader
Bmicolors as Rgbquad
End Type
These three data structures are indispensable in the DIB. We don't have to delve into it, just copy and paste it in order and use it directly.

Global variables to be used in the procedure:
Private Const Bits as Long = 32 ' color depth, where all images are processed according to 32 bits
Public do as Boolean ' is used to mark whether a process ends
Public Timeget As Long is used to record the time spent in processing the input process
Public timeput As Long is used to record the time spent in processing of an output process
Dim Colval () as Byte is used to store the pixel values entered from the DIB
Dim colout () as Byte is used to store the pixel values to the DIB output
Dim Inputhei as Long ' for recording the height of the input image
Dim Inputwid as Long ' for recording the width of the input image
Dim Bi24bitinfo as Bitmapinfo ' define BMP information

As you can see, I used two different dynamic arrays Colval () and Colout () in the input and output.
This makes sense, because we are not just trying to input and output images, but also to process pixels in the middle.
including image scaling, color adjustment, sharpening, softening and so on processing, using two different arrays to store data is more conducive to the implementation of the program.

Some impatient friends may have put the program into the project to try, but will find that the image can not be output at all.
This is because when you use Dibget to get images that are still in colval (), you need to put them in the colout () array, dibput the process to work.

Here is a procedure for moving data in an array as a whole:
Public Sub CopyData (ByVal W as Long, ByVal H as Long)
Dim Length as Long
Dim I as Long
Dim L as Long
I = Bits \ 8
L = I-1
Length = (W + 1&) * (H + 1&) * I
ReDim colout (L, W, H)
CopyMemory colout (0, 0, 0), colval (0, 0, 0), Length
End Sub

API definition:
Private Declare Sub copymemory Lib "kernel32" Alias "RtlMoveMemory" (pdest as any, psrc as any, ByVal Bytelen as Long)

At this point, we can try the effect:
Tune your monitor to 32-bit color.
Paste all of the previous API and variable definitions into a new module
Create a new form, plus two picture controls: Pictrue1, picture2 a button Command1
Load a picture in pictrue1
Write the following code in Command1:
Sub Command1_Click ()
With Picture1
. Scalemode=3
. Borderstyle=0
Dibget. Hdc,0,0,.scalewidth,.scaleheight
End With
CopyData Inputhei, Inputwid
Picture2. Autoredraw=true
Dibput PICTURE2.HDC
Picture2.refresh
End Sub


Run, the button is pressed, the picture in the PICTREU1 is immediately displayed in the Picture2.

At this point, you may say, after so long to put a picture? Is it okay to use paintpicture?
Yes, if it's just a picture, it doesn't have to be such a hassle, but the image processing section that we're going to talk about will use the pixel values from the front door. So, this is just the beginning, and what I really want to talk about is still in the back. Please continue to pay attention.

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