老代码备忘,我对图像处理不是太懂。

注:部分代码引援自网上,话说我到底自己写过什么代码。。。

 Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hbitmap As Long, _
ByVal dwCount As Long, _
lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hbitmap As Long, _
ByVal dwCount As Long, _
lpBits As Any) As Long
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
Private Declare Function SetDIBits Lib "gdi32" (ByVal hdc 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
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, _
ByVal lpDeviceName As String, _
ByVal lpOutput As String, _
lpInitData As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Private Type 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 Private Sub Command1_Click()
Dim pic As StdPicture
Set pic = LoadPicture("D:\My Documents\Downloads\119562132_21n.jpg") Dim w As Long
Dim h As Long
With pic
w = ScaleX(.Width, vbHimetric, vbPixels)
h = ScaleY(.Height, vbHimetric, vbPixels)
End With Dim hdc As Long
hdc = CreateDC("DISPLAY", vbNullString, vbNullString, &)
Call SelectObject(hdc, pic.Handle) Dim bits() As Byte
ReDim bits(, w, h) As Byte
Dim bi As BitMapInfo
With bi.bmiHeader
.biBitCount = &
.biCompression = &
.biPlanes = &
.biSize = Len(bi.bmiHeader)
.biWidth = w
.biHeight = h
End With
Call GetDIBits(hdc, pic.Handle, , h, bits(, , ), bi, &) '灰度化
Dim x As Long
Dim y As Long
Dim g As Byte
For x = To w
For y = To h
'灰度公式:Gray=R×0.299+G×0.587+B×0.114
'貌似有更好的方案:g=(bits(0, ix, iy) ^ 2.2 * 0.0722 + bits(1, ix, iy) ^ 2.2 * 0.7152 + bits(2, ix, iy) ^ 2.2 * 0.2126) ^ (1 / 2.2)
'不过,肉眼看不出差别来 (>_<)
g = bits(, x, y) * 0.114 + bits(, x, y) * 0.587 + bits(, x, y) * 0.299
bits(, x, y) = g
bits(, x, y) = g
bits(, x, y) = g
Next
Next Picture1.Picture = Picture1.Image
Call SetDIBits(Picture1.hdc, Picture1.Picture.Handle, &, h, bits(, , ), bi, &)
Picture1.Picture = Picture1.Image Dim threshold As Byte
threshold = GetThreshold(bits, w, h) '二值化,阈值通过[最大类间方差法(Otsu)]取得
For x = To w
For y = To h
If bits(, x, y) > threshold Then
bits(, x, y) =
bits(, x, y) =
bits(, x, y) =
Else
bits(, x, y) =
bits(, x, y) =
bits(, x, y) =
End If
Next
Next Picture2.Picture = Picture2.Image
Call SetDIBits(Picture2.hdc, Picture2.Picture.Handle, &, h, bits(, , ), bi, &)
Picture2.Picture = Picture2.Image Erase bits
Call DeleteDC(hdc)
Set pic = Nothing
End Sub Private Function GetThreshold(ByRef Pixels() As Byte, _
ByVal Width As Long, _
ByVal Height As Long) As Byte
'最大类间方差法(Otsu)
'这个函数是我根据百度文库一个文档里提供的C代码翻译过来的
'@http://wenku.baidu.com/link?url=wVl9A7eZiRddxpaCPPLcAIb-VDlyrV__-Zfw6j6o50FEUochgV9G_zRVsMHVDxN2ilOUXiRbSSM-as_ELJpjxnWEvERlABlvVoVK6-FDQpW
Dim hist() As Long
Dim x As Long
Dim y As Long
Dim i As Long For i = To : hist(i) = : Next
For y = To Height
For x = To Width
hist(Pixels(, x, y)) = hist(Pixels(, x, y)) +
Next
Next Dim p() As Double
Dim ut As Double
Dim uk As Double
Dim sigma As Double
Dim mk As Double
Dim maxk As Byte
Dim maxs As Double
Dim total As Long
Dim EPSTLON As Double
EPSILON = 0.000001 '10 ^ -6 total = Width * Height
ut =
For i = To
p(i) = hist(i) / total
ut = ut + i * hist(i)
Next
ut = ut / total
wk =
uk =
maxs =
For i = To
uk = uk + i * p(i)
wk = wk + p(i)
If wk <= EPSTLON Or wk >= (# - EPSTLON) Then
Else
sigma = (ut * wk - uk)
sigma = (sigma * sigma) / (wk * (# - wk))
If sigma > maxs Then
maxs = sigma
maxk = i
End If
End If
Next
GetThreshold = maxk
End Function

上张图,看看效果:

原图:

VB6之图像灰度与二值化的更多相关文章

  1. [iOS OpenCV的使用,灰度和二值化]

    看网上方法很多,但版本都不够新,我看了网上一些知识,总结了下,来个最新版Xcode6.1的. 最近主要想做iOS端的车牌识别,所以开始了解OpenCV.有兴趣的可以跟我交流下哈. 一.Opencv的使 ...

  2. OpenCV:图像的普通二值化

    首先我们来看看图像二值化的过程,opencv一共有好几种不同的二值化算法可以使用,一般来说图像的像素,亮度等条件如果超过了某个或者低于了某个阈值,就会恒等于某个值,可以用于某些物体轮廓的监测: 导包: ...

  3. opencv-python图像二值化函数cv2.threshold函数详解及参数cv2.THRESH_OTSU使用

    cv2.threshold()函数的作用是将一幅灰度图二值化,基本用法如下: #ret:暂时就认为是设定的thresh阈值,mask:二值化的图像 ret,mask = cv2.threshold(i ...

  4. openCV_java 图像二值化

    较为常用的图像二值化方法有:1)全局固定阈值:2)局部自适应阈值:3)OTSU等. 局部自适应阈值则是根据像素的邻域块的像素值分布来确定该像素位置上的二值化阈值.这样做的好处在于每个像素位置处的二值化 ...

  5. OpenCV_基于局部自适应阈值的图像二值化

    在图像处理应用中二值化操作是一个很常用的处理方式,例如零器件图片的处理.文本图片和验证码图片中字符的提取.车牌识别中的字符分割,以及视频图像中的运动目标检测中的前景分割,等等. 较为常用的图像二值化方 ...

  6. 【转】Emgu CV on C# (五) —— Emgu CV on 局部自适应阈值二值化

    局部自适应阈值二值化 相对全局阈值二值化,自然就有局部自适应阈值二值化,本文利用Emgu CV实现局部自适应阈值二值化算法,并通过调节block大小,实现图像的边缘检测. 一.理论概述(转载自< ...

  7. [转载+原创]Emgu CV on C# (五) —— Emgu CV on 局部自适应阈值二值化

    局部自适应阈值二值化 相对全局阈值二值化,自然就有局部自适应阈值二值化,本文利用Emgu CV实现局部自适应阈值二值化算法,并通过调节block大小,实现图像的边缘检测. 一.理论概述(转载自< ...

  8. Opencv实现图像的灰度处理,二值化,阀值选择

    前几天接触了图像的处理,发现用OPencv处理确实比較方便.毕竟是非常多东西都封装好的.可是要研究里面的东西,还是比較麻烦的,首先,你得知道图片处理的一些知识,比方腐蚀,膨胀,仿射,透射等,还有非常多 ...

  9. Java基于opencv实现图像数字识别(三)—灰度化和二值化

    Java基于opencv实现图像数字识别(三)-灰度化和二值化 一.灰度化 灰度化:在RGB模型中,如果R=G=B时,则彩色表示灰度颜色,其中R=G=B的值叫灰度值:因此,灰度图像每个像素点只需一个字 ...

随机推荐

  1. bzoj 4765 普通计算姬(树状数组 + 分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=4765 很nice的一道题啊(可能是因为卡了n久终于做出来了 题意就是给你一棵带点权的有根树,sum( ...

  2. while循环 操作列表与字典

    1.在列表间移动元素 #!/usr/bin/env python #filename=list.py num1 = [1,3,5,7,9,11,13,15] num2 = [] while num1: ...

  3. glmnetUtils: quality of life enhancements for elastic net regression with glmnet

    The glmnetUtils package provides a collection of tools to streamline the process of fitting elastic ...

  4. mxnet:结合R与GPU加速深度学习(转)

    近年来,深度学习可谓是机器学习方向的明星概念,不同的模型分别在图像处理与自然语言处理等任务中取得了前所未有的好成绩.在实际的应用中,大家除了关心模型的准确度,还常常希望能比较快速地完成模型的训练.一个 ...

  5. JVM的内存区域划分以及垃圾回收机制详解

    在我们写Java代码时,大部分情况下是不用关心你New的对象是否被释放掉,或者什么时候被释放掉.因为JVM中有垃圾自动回收机制.在之前的博客中我们聊过Objective-C中的MRC(手动引用计数)以 ...

  6. Java经典编程题50道之十二

    企业发放的奖金根据利润提成:利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成, 高于10万元的部分 ,可提成7.5%:20万到40万之间 ...

  7. Idea中执行TestNg报错

    今天在Idea中使用TestNg过程中报错: java.lang.AbstractMethodError: org.testng.remote.RemoteTestNG$DelegatingTestR ...

  8. Html5模拟通讯录人员排序(sen.js)

    // JavaScript Document var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_2 = & ...

  9. java之内部类

    最近学了java,对内部类有一点拙见,现在分享一下 所谓内部类(nested classes),即:面向对象程序设计中,可以在一个类的内部定义另一个类. 内部类不是很好理解,但说白了其实也就是一个类中 ...

  10. android打电话

    一.安卓中,TelephonyManager是电话管理器,它管理着电话的所有服务. 1.如图,为一个简单的打电话应用,他是通过调用系统API实现的,必须添加权限 2.先看清单文件,此处添加权限 < ...