【转帖】VFP实现验证码去噪二值化算法
											网上看到一段vfp代码,可以将验证码图片的背景去掉,得到验证码字符。可用于车牌自动识别,但是可惜我把代码重新打了一遍,并没有成功,不知道原因,哪位大侠可以看看哪里出了问题???附文章出处: https://www.
程序代码:
declare integer GdipSaveImageToStream in gdiplus.dll;
    long nlmage,;
    integer stream,;
    string csidEncoder,;
    long encoderparams
declare integer GdipCreateBitmapFromScan0 in gdiplus.dll; 
    integer nwidth,;
    integer nheight,;
    integer nstride,;
    integer npixelformat,;
    string @cscan0,;
    integer @nlmage
declare integer GdipGetImageHeight in gdiplus.dll;
    integer img,;
    integer @imgheight
declare integer GdipGetImageWidth in gdiplus.dll;
    integer img,;
    integer @imagewidth
declare integer GdipDrawImageRect in gdiplus.dll;
    integer ngraphics,;
    integer nImage,;
    single,;
    single,;
    single,;
    single
declare integer GdipGetImageGraphicsContext in gdiplus.dll;
    integer nImage,;
    integer @ngraphics
declare integer GdipLoadImageFromFile in gdiplus.dll;
    string wfilename,;
    integer @nImage
declare integer GdipDeleteGraphics in gdiplus.dll;
    integer graphics
declare long GdipDisposeImage in gdiplus.dll;
    long image
declare long CreateStreamOnHGlobal in ole32.dll;
    long hGlobal,;
    long fDeleteOnRelease,;
    long @ppstm && 创建字符流指针
declare integer GetProcessHeap in win32api
declare integer HeapAlloc in win32api long, long, long 
declare integer HeapFree in win32api integer,integer,integer
declare integer ReleaseStgMedium in ole32.dll string pmedium
store 0 to nimage, lnheight,lnwidth,newbitmap,ngraphics
clsidEncoder=0h00F47C55041AD3119A730000f81EF32E &&BMP
picfile=getpict()
GdipLoadImageFromFile(strconv(picfile+chr(0),5),@nimage)
GdipGetImageHeight(nimage,@lnheight)&&获得图像高度
GdipGetImageWidth(nimage,@lnwidth) &&获取图像宽度
pixelformat_32bppaRGB=0x0026200A
GdipCreateBitmapFromScan0(lnwidth,lnheight,0,pixelformat_32bppaRGB,0,@newbitmap)
GdipGetImageGraphicsContext(newbitmap,@ngraphics)
GdipDrawImageRect(ngraphics,nimage,0,0,lnwidth,lnheight)
GdipDisposeImage(nimage) &&此时nimage已没用,销毁
streamsize=lnheight*lnwidth*4+54 &&计算内存大小
&& 申请内存
hprocheap=GetProcessHeap()
Ihmemptr=HeapAlloc(hprocheap,0,streamsize) &&申请
IHStream=0
CreateStreamOnHGlobal(Ihmemptr,1,@IHStream) &&创建字符流指针
GdipSaveImageToStream(newbitmap,IHStream,@clsidEncoder,0)
Ihmemptr=Ihmemptr+54 &&像素开始地址
mR=0
mG=0
mB=0
RUM=10 &&二值化颜色容差
for i=0 to streamsize-54 step 4 &&统计平均颜色
    B=asc(sys(2600,Ihmemptr+i,1))
    G=asc(sys(2600,Ihmemptr+i+1,1))
    R=asc(sys(2600,Ihmemptr+i+2,1))
    mR=mR+R
    mG=mG+G
    mB=mB+B
endfor
mRGB=int((mR+mG+mB)/(lnheight*lnwidth*3)) &&自动二值化阀值
if mRGB<128 &&二值化阀值调节
    mRGB=mRGB+RUM
endif
NUM=mRGB &&自动对比度阀值
for i=0 to lnwidth-1
    for ii=0 to lnheight-1
        Ihmemptrtmp=Ihmemptr+(ii*lnwidth+i)*4 &&第i,ii点像素的地址
        B=asc(sys(2600,Ihmemptr+i,1))
        G=asc(sys(2600,Ihmemptr+i+1,1))
        R=asc(sys(2600,Ihmemptr+i+2,1))
        if int((R+G+B)/3)>mRGB
            sys(Ihmemptrtmp,4,0hFFFFFF00)
        endif
        if (R+G+B)/3>128
            uR=(255-R)/255
            uG=(255-G)/255
            uB=(255-B)/255
            R2=R+NUM*uR
            G2=G+NUM*uG
            B2=B+NUM*uB
            R=icase(R2>255,255,R2<0,0,R2)
            G=icase(G2>255,255,G2<0,0,G2)
            B=icase(B2>255,255,B2<0,0,B2)
        else
            R2=R-NUM
            G2=G-NUM
            B2=B-NUM
            R=icase(R2>255,255,R2<0,0,R2)
            G=icase(G2>255,255,G2<0,0,G2)
            B=icase(B2>255,255,B2<0,0,B2)
        endif
        if int((R+G+B)/3)>mRGB
            sys(Ihmemptrtmp,4,0hFFFFFF00)
        else
            sys(Ihmemptrtmp,3,0hFFFFFF00)
        endif
    endfor
endfor
strtofile(sys(2600,Ihmemptr-54,streamsize),"6.bmp") &&保存到本地
HeapFree(hprocheap,0,Ihmemptr)
ReleaseStgMedium(0h04000000+bintoc(Ihmemptr,"4rs")+0h00000000)
GdipDisposeImage(newbitmap)
GdipDeleteGraphics(ngraphics)
附上验证码测试图片
					
				
			
											
	    
											
