Option Explicit求高手给代码加个注释。 约详细越好。
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 Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbAlpha As Byte '透明通道
End Type
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 BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
'图片文件头
Dim BI As BITMAPINFO
Dim BI1 As BITMAPINFO
'在图片1中查找图片2,是否找出全部
Public Function FindPic(P1 As VB.PictureBox, P2 As VB.PictureBox, Optional FindAll As Boolean = False) As Integer()
Dim W As Long, H As Long, I As Long, J As Long
Dim W2 As Long, H2 As Long, I2 As Long, J2 As Long
Dim zPic() As Byte, fPic() As Byte
Dim R As Byte, G As Byte, B As Byte
'1 获得图片2数据
W2 = ScaleX(P2.Picture.Width, vbHimetric, vbPixels)
H2 = ScaleY(P2.Picture.Height, 8, 3)
With BI.bmiHeader
.biSize = Len(BI.bmiHeader)
.biWidth = W2
.biHeight = -H2
.biBitCount = 32
.biPlanes = 1
End With
ReDim zPic(3, W2 - 1, H2 - 1)
I = GetDIBits(P2.hdc, P2.Picture.Handle, 0, H2, zPic(0, 0, 0), BI, 0)
'Debug.Print I
'如果在这里处理一下,图像大的话,可能会快一点。
'2 获得图片1数据
W = ScaleX(P1.Picture.Width, vbHimetric, vbPixels)
H = ScaleY(P1.Picture.Height, 8, 3)
With BI1.bmiHeader
.biSize = Len(BI1.bmiHeader)
.biWidth = W
.biHeight = -H
.biBitCount = 32
.biPlanes = 1
End With
For J2 = 0 To H2 - 2 '循环判断小图片
For I2 = 0 To W2 - 2
P2.PSet (I2, J2), RGB(zPic(2, I2, J2), zPic(1, I2, J2), zPic(0, I2, J2))
VBA.DoEvents
Next I2
Next J2
P2.Refresh
ReDim fPic(3, W - 1, H - 1)
I = GetDIBits(P1.hdc, P1.Picture.Handle, 0, H, fPic(0, 0, 0), BI1, 0)
'Debug.Print I
'分析查找
For J = 0 To H - H2 - 1
For I = 0 To W - W2 - 1
VBA.DoEvents
For J2 = 0 To H2 - 2 '循环判断小图片
For I2 = 0 To W2 - 2
If fPic(2, I + I2, J + J2) <> zPic(2, I2, J2) Then GoTo ExitLine: 'R
If fPic(1, I + I2, J + J2) <> zPic(1, I2, J2) Then GoTo ExitLine: 'G
If fPic(0, I + I2, J + J2) <> zPic(0, I2, J2) Then GoTo ExitLine: 'B
Next I2
Next J2
'Debug.Print "发现:", I, J
Load Shape1(Shape1.Count)
With Shape1(Shape1.Count - 1)
.Move I, J
.BorderWidth = 2
.Visible = True
End With
ExitLine:
Next I
Next J
Label1.Caption = "找到" & Shape1.Count - 1 & "个区域完全相同。"
End Function
Private Sub Cmd1_Click()
Dim Ps() As Integer '找到图片的位置数组
Ps = FindPic(Pic1, Pic2)
End Sub
Private Sub Form_Load()
With Shape1(0)
.Height = Pic2.Height
.Width = Pic2.Width
End With
End Sub
这是个自动找图的找图的源码。 源码已经上传。http://download.csdn.net/source/3039410 这是源码地址。
程序有两个picture控件。 一个显示大图。一个显示小图。 点击按钮后能在大图中快速找到小图。 我在picture1里随便截取图片。 放在picture2里,能快速找到图片。将两幅图片替换后就找不到图片了。不知道什么原因。 求高手解答。
14 个解决方案
#1
#2
问问题还要扣资源分
#3
有些痛苦。。。
#4
哥们你不是来问问题的,是来赚钱的
#5
不懂。刚弄的这。等我去看看能改不。
#6
csdn真不好操作。
#7
其实CSDN随着时代的变迁含义已经发生变化了。
#8
找我自己发的帖子都找了几分钟。
#9
#10
这种读取像素逐一比较的用处不大。因为真实情况是,小图和大图并非每个像素对应的。
需要使用模式识别算法去匹配。
需要使用模式识别算法去匹配。
#11
怎么模糊匹配。能给个大致的思路么。
#12
搜 图像匹配,图形匹配 那些。灰度匹配好像是一个
#13
OpenCV
#14
找到模糊匹配的了。谢谢大家了。
#1
#2
问问题还要扣资源分
#3
有些痛苦。。。
#4
哥们你不是来问问题的,是来赚钱的
#5
不懂。刚弄的这。等我去看看能改不。
#6
csdn真不好操作。
#7
其实CSDN随着时代的变迁含义已经发生变化了。
#8
找我自己发的帖子都找了几分钟。
#9
#10
这种读取像素逐一比较的用处不大。因为真实情况是,小图和大图并非每个像素对应的。
需要使用模式识别算法去匹配。
需要使用模式识别算法去匹配。
#11
怎么模糊匹配。能给个大致的思路么。
#12
搜 图像匹配,图形匹配 那些。灰度匹配好像是一个
#13
OpenCV
#14
找到模糊匹配的了。谢谢大家了。