网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
08月01日漏签0天
vb吧 关注:156,099贴子:1,166,220
  • 看贴

  • 图片

  • 吧主推荐

  • 游戏

  • 19回复贴,共1页
<<返回vb吧
>0< 加载中...

求助,VB实现在图片框(picture1)中快速取色的算法和方法。。。

  • 只看楼主
  • 收藏

  • 回复
  • a90620038
  • 递归爆栈
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
近期在编程开发中遇到了技术难题。。。
——————
根据需要,要求在 尺寸为 600*1000(像素)的区域内进行遍历找色
图片已经加载到 pinture1 (尺寸为 600*1000 像素)中
目前 已知有两种方法取色和找色
方法1:
private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Command1_Click()
For iy = 0 To 599
For ix = 0 To 999
ysz = GetPixel(GetWindowDC(Picture1.hwnd), ix, iy)
If ysz="03020F" then
print ix
print iy
exit sub
end if
Next
DoEvents
Next
End Sub
'GetPixel似乎是最慢的方法,约耗时几分钟
——————————————————————————
方法2,使用point方法
Private Sub Command2_Click()
Picture1.ScaleMode = 3
For iy = 0 To 599
For ix = 0 To 999
ysz = Hex(Val(Picture1.point(ix, iy)))
If ysz="3020F" then
print ix
print iy
exit sub
end if
Next
DoEvents
Next
End Sub
’point方法快了很多,找色大约需要6秒
——————————————
根据程序需要,希望改进为 在1秒内完成找色!!!!!


  • 璐村惂鐢ㄦ埛_0748V5Z馃惥
  • 网络通信
    11
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
最好避开显示设备,直接在数据文件中找


2025-08-01 03:07:19
广告
不感兴趣
开通SVIP免广告
  • 菠萝蜜
  • 暴力枚举
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
'全循环1.6s,随机中断应该在0.8s左右。
Option Explicit
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Command1_Click()
Dim iy, ix, ysz, a, t
t = Timer
a = GetWindowDC(Picture1.hwnd)
For iy = 0 To 599
For ix = 0 To 999
ysz = Hex(GetPixel(a, ix, iy))
If ysz = "03020F" Then
'Print ix
'Print iy
'Exit Sub
End If
Next
DoEvents
Next
Debug.Print Timer - t
End Sub


  • a90620038
  • 递归爆栈
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
测试


  • a90620038
  • 递归爆栈
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
感谢几位大神提供思路,这些天经过研究
在此分享最新方法


  • a90620038
  • 递归爆栈
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
VB极速找色法:
'添加这些API
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 Type BitMapInfoHeader ''文件信息头——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
——————————————————————
‘’添加以下函数
Public Function speedgetcor(obj As PictureBox, scx As Integer, scy As Integer) '获取 图片框 scx,scy位置的颜色值
Dim ix As Integer
Dim iy As Integer
Dim iWidth As Integer '以像素为单位的图形宽度
Dim iHeight As Integer '以像素为单位的图形高度
Dim bytGray As Byte
Dim bytThreshold As Byte
Dim bits() As Byte '三维数组,用于获取原彩色图像中各像素的RGB数值以及存放转化后的灰度值
Dim bitsBW() As Byte '三维数组,用于存放转化为黑白图后各像素的值
'获取图形的宽度和高度
iWidth = obj.ScaleWidth '/ Screen.TwipsPerPixelX
iHeight = obj.ScaleHeight '/ Screen.TwipsPerPixelY
obj.Picture = obj.Image
'创建并初始化一个bitMapInfo自定义类型
Dim bi24BitInfo As BitMapInfo
With bi24BitInfo.bmiHeader
.biBitCount = 32
.biCompression = 0&
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = iWidth
.biHeight = iHeight '/ Screen.TwipsPerPixelY
Debug.Print iWidth;
Debug.Print iHeight;
Debug.Print Len(bi24BitInfo.bmiHeader)
End With
'重新定义数组大小
ReDim bits(3, 0 To iWidth - 1, 0 To iHeight - 1) As Byte
ReDim bitsBW(3, 0 To iWidth - 1, 0 To iHeight - 1) As Byte
'使用GetDIBits方法一次性获取obj中各点的rgb值,比point方法或getPixel函数逐像素获取像素rgb要快出一个数量级
lrtn = GetDIBits(obj.hdc, obj.Picture.Handle, 0&, iHeight, bits(0, 0, 0), bi24BitInfo, 0&)
'数组的三个维度分别代表像素的RGB分量、以图形左下角为原点的X和Y坐标。
'具体说来,这时bits(0,2,3)代表从图形左下角数起横向第2个纵向第3个像素的Blue值,而bits(1,2,3)和bits(2,2,3)分别的Green值和Red值.
'Debug.Print "lrtn:" & lrtn
iht = iHeight - 1
speedgetcor = Hex(Val(RGB(bits(2, scx, iht - scy), bits(1, scx, iht - scy), bits(0, scx, iht - scy))))
End Function
Public Function speedfindcor(obj As PictureBox, spx1 As Integer, spy1 As Integer, spx2 As Integer, spy2 As Integer, cor As Variant) As Variant
Dim ix As Integer
Dim iy As Integer
Dim iWidth As Integer '以像素为单位的图形宽度
Dim iHeight As Integer '以像素为单位的图形高度
Dim bytGray As Byte
Dim bytThreshold As Byte
Dim bits() As Byte '三维数组,用于获取原彩色图像中各像素的RGB数值以及存放转化后的灰度值
Dim bitsBW() As Byte '三维数组,用于存放转化为黑白图后各像素的值
'获取图形的宽度和高度
iWidth = obj.ScaleWidth '/ Screen.TwipsPerPixelX
iHeight = obj.ScaleHeight '/ Screen.TwipsPerPixelY
obj.Picture = obj.Image
'创建并初始化一个bitMapInfo自定义类型
Dim bi24BitInfo As BitMapInfo
With bi24BitInfo.bmiHeader
.biBitCount = 32
.biCompression = 0&
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = iWidth
.biHeight = iHeight '/ Screen.TwipsPerPixelY
Debug.Print iWidth;
Debug.Print iHeight;
Debug.Print Len(bi24BitInfo.bmiHeader)
End With
'重新定义数组大小
ReDim bits(3, 0 To iWidth - 1, 0 To iHeight - 1) As Byte
ReDim bitsBW(3, 0 To iWidth - 1, 0 To iHeight - 1) As Byte
'使用GetDIBits方法一次性获取obj中各点的rgb值,比point方法或getPixel函数逐像素获取像素rgb要快出一个数量级
lrtn = GetDIBits(obj.hdc, obj.Picture.Handle, 0&, iHeight, bits(0, 0, 0), bi24BitInfo, 0&)
'数组的三个维度分别代表像素的RGB分量、以图形左下角为原点的X和Y坐标。
'具体说来,这时bits(0,2,3)代表从图形左下角数起横向第2个纵向第3个像素的Blue值,而bits(1,2,3)和bits(2,2,3)分别的Green值和Red值.
'Debug.Print "lrtn:" & lrtn
iht = iHeight - 1
For isy = spy1 To spy2
For isx = spx1 To spx2
tempcor = Hex(Val(RGB(bits(2, isx, iht - isy), bits(1, isx, iht - isy), bits(0, isx, iht - isy))))
If tempcor = cor Then
speedfindcor = isx & "," & isy
Exit Function
End If
Next
DoEvents
Next
End Function
Public Function getx(str As Variant) As Variant
If Left(str, 1) = "(" Then
getx = Mid(str, 2, InStr(1, str, ",") - 2)
Exit Function
End If
If Left(str, 1) <> "(" Then
getx = Mid(str, 1, InStr(1, str, ",") - 1)
Exit Function
End If
End Function
Public Function gety(str As Variant) As Variant
If Right(str, 1) = ")" Then
gety = Mid(str, InStr(1, str, ",") + 1, Len(str) - (InStr(1, str, ",") + 1))
Exit Function
End If
If Right(str, 1) <> ")" Then
gety = Mid(str, InStr(1, str, ",") + 1, Len(str) - InStr(1, str, ","))
Exit Function
End If
End Function


  • a90620038
  • 递归爆栈
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
调用方法
例1:
取得图片框(100,100)坐标出的颜色值
MsgBox speedgetcor(Picture1, 100, 100)
例2:
在图片框中找出 “3020F” 颜色的坐标(图片的尺寸为 1000*600)
MsgBox speedfindcor(Picture1, 0, 0, 999, 599, "3020F")


  • a90620038
  • 递归爆栈
    7
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
例3:
分解上面得到的坐标
分解横坐标
msgbox getx(speedfindcor(Picture1, 0, 0, 999, 599, "3020F"))
分解纵坐标
msgbox gety(speedfindcor(Picture1, 0, 0, 999, 599, "3020F"))


2025-08-01 03:01:19
广告
不感兴趣
开通SVIP免广告
  • lnkj8
  • 啥也不懂
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这个可以用于对屏幕取色吗?


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 19回复贴,共1页
<<返回vb吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示