Appearance
图色方法
函数列表
1. 创建QImage图形处理对象 (QImage.new)
说明: 创建一个QImage图形处理对象
函数: QImage.new()
参数: 无
返回值:
| 返回值类型 | 说明 |
|---|---|
| userdata | 返回一个QImage的userdata对象 |
示例:
lua
local image = QImage.new()
if image ~= nil then
print("创建QImage对象成功")
else
print("创建QImage对象失败")
end2. 创建一个空白QImage画布 (QImage.create)
说明: 创建一个空白QImage画布
函数: image:create(width,height,argb)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| width | 整数 | 画布的宽度 |
| height | 整数 | 画布的高度 |
| argb | 整数 | 画布的颜色例如:0xffffffff (白色) |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 布尔类型 |
示例:
lua
local img = QImage.new()
local ret = img:create(500,500,0xffffffff)
local savepath = getWorkPath() .. "/white.png"
local savepath1 = getWorkPath() .. "/yellow.png"
if ret then
img:save(savepath)
print("创建白色画布完成,保存路径:",savepath)
end
ret = img:create(100,100,0xffffff00)
if ret then
img:save(savepath1)
print("创建黄色黄布完成,保存路径:",savepath1)
end3. 截取系统屏幕到QImage对象中 (QImage.snapShot)
说明: 截取系统屏幕到QImage对象中
函数: image:snapShot(left, top, right, bottom)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| left | 整数 | 截图区域左边界坐标 |
| top | 整数 | 截图区域上边界坐标 |
| right | 整数 | 截图区域右边界坐标 |
| bottom | 整数 | 截图区域下边界坐标 |
说明: 如果left top right bottom都为0或者不填写就表示全屏截图
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型,true表示截图成功,false表示截图失败 |
示例:
lua
local image = QImage.new()
if image ~= nil then
local result = image:snapShot(0, 0, 100, 100)
if result then
print("截图成功")
else
print("截图失败")
end
else
print("创建QImage对象失败")
end4. 从本地图片文件加载到QImage对象中 (QImage.load)
说明: 从本地图片文件加载到QImage对象中
函数: image:load(filepath)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| filepath | 字符串 | 图片文件的路径字符串 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型,true表示加载成功,false表示加载失败 |
示例:
lua
local image = QImage.new()
local filepath = getWorkPath() .. "/test.png"
if image ~= nil then
local result = image:load(filepath)
if result then
print("图片加载成功")
else
print("图片加载失败,请检查文件路径")
end
else
print("创建QImage对象失败")
end5. 将QImage对象保存到本地图片文件 (QImage.save)
说明: 将QImage对象保存到本地图片文件
函数: image:save(filepath)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| filepath | 字符串 | 图片文件的保存路径字符串 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型,true表示保存成功,false表示保存失败 |
示例:
lua
local image = QImage.new()
local filepath = getWorkPath() .. "/test.png"
if image ~= nil then
local snapResult = image:snapShot(0, 0, 100, 100)
if snapResult then
local saveResult = image:save(filepath)
if saveResult then
print("图片保存成功")
else
print("图片保存失败,请检查路径和权限")
end
else
print("截图失败")
end
else
print("创建QImage对象失败")
end6. 获取QImage对象的宽度 (QImage.width)
说明: 获取QImage对象的宽度
函数: image:width()
参数: 无
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 返回一个整数,表示图像的宽度(像素) |
示例:
lua
local image = QImage.new()
if image ~= nil then
local result = image:snapShot(0,0,200,200)
if result then
local width = image:width()
print("图片宽度:" .. width .. "像素")
else
print("图片加载失败")
end
else
print("创建QImage对象失败")
end7. 获取QImage对象的高度 (QImage.height)
说明: 获取QImage对象的高度
函数: image:height()
参数: 无
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 返回一个整数,表示图像的高度(像素) |
示例:
lua
local image = QImage.new()
if image ~= nil then
local result = image:snapShot(0, 0, 100, 100)
if result then
local height = image:height()
print("图片高度:" .. height .. "像素")
local width = image:width()
print("图片尺寸:" .. width .. "x" .. height)
else
print("截图失败")
end
else
print("创建QImage对象失败")
end8. 对图像进行二值化处理 (QImage.binarize)
说明: 对图像进行二值化处理
函数: image:binarize(threshold, maxValue, method)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| threshold | 整数 | 阈值,用于区分前景和背景的灰度值界限 |
| maxValue | 整数 | 最大值,当像素值超过阈值时被设置的值 |
| method | 整数 | 二值化方法,可选值:0: BINARY - 标准二值化;1: BINARY_INV - 反二值化;2: OTSU - 大津算法;3: ADAPTIVE_MEAN - 自适应均值阈值;4: ADAPTIVE_GAUSSIAN - 自适应高斯阈值 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型,true表示二值化成功,false表示失败 |
示例:
lua
local image = QImage.new()
local filepath = getWorkPath() .. "/test.png"
if image ~= nil then
local result = image:snapShot(0,0,200,200)
if result then
local binarizeResult = image:binarize(128, 255, 0)
if binarizeResult then
print("二值化处理成功")
image:save(filepath)
else
print("二值化处理失败")
end
else
print("图片加载失败")
end
else
print("创建QImage对象失败")
end9. 缩放图像尺寸 (QImage.scale)
说明: 缩放图像尺寸
函数:
按比例缩放: image:scale(scaleFactor)
按尺寸缩放: image:scale(width, height)参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| scaleFactor | 浮点数 | 缩放比例,如0.5表示缩小一半,2.0表示放大两倍 |
| width | 整数 | 目标宽度 |
| height | 整数 | 目标高度 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型,true表示缩放成功,false表示失败 |
示例:
lua
local image = QImage.new()
local scaled_half = getWorkPath() .. "/scaled_half.png"
local scaled_fixed = getWorkPath() .. "/scaled_fixed.png"
if image ~= nil then
if image:snapShot(0,0,200,200) then
local result1 = image:scale(0.5)
if result1 then
print("按比例缩放成功")
image:save(scaled_half)
end
image:snapShot(0,0,200,200)
local result2 = image:scale(200, 150)
if result2 then
print("按尺寸缩放成功")
local newWidth = image:width()
local newHeight = image:height()
print("新尺寸:" .. newWidth .. "x" .. newHeight)
image:save(scaled_fixed)
end
else
print("图片加载失败")
end
else
print("创建QImage对象失败")
end10. 旋转图像 (QImage.rotate)
说明: 旋转图像
函数: image:rotate(angle)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| angle | 浮点数 | 旋转角度,正数表示逆时针旋转,负数表示顺时针旋转 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型,true表示旋转成功,false表示失败 |
示例:
lua
local image = QImage.new()
local rotated_90 = getWorkPath() .. "/rotated_90.png"
local rotated_45 = getWorkPath() .. "/rotated_45.png"
if image ~= nil then
if image:snapShot() then
local result = image:rotate(90)
if result then
print("图像旋转成功")
image:save(rotated_90)
else
print("图像旋转失败")
end
image:snapShot()
if image:rotate(45) then
print("45度旋转成功")
image:save(rotated_45)
end
else
print("图片加载失败")
end
else
print("创建QImage对象失败")
end11. 裁剪图像区域 (QImage.crop)
说明: 裁剪图像区域
函数: image:crop(x, y, width, height)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| x | 整数 | 裁剪区域左上角X坐标 |
| y | 整数 | 裁剪区域左上角Y坐标 |
| width | 整数 | 裁剪区域宽度 |
| height | 整数 | 裁剪区域高度 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| userdata | 返回一个新的QImage对象,包含裁剪后的图像,如果裁剪失败返回nil |
示例:
lua
local image = QImage.new()
local cropped_image = getWorkPath() .. "/cropped_image.png"
local cropped_binary = getWorkPath() .. "/cropped_binary.png"
if image ~= nil then
if image:snapShot() then
local originalWidth = image:width()
local originalHeight = image:height()
print("原图尺寸:" .. originalWidth .. "x" .. originalHeight)
local cropX = 50
local cropY = 50
local cropWidth = 100
local cropHeight = 100
local croppedImage = image:crop(cropX, cropY, cropWidth, cropHeight)
if croppedImage ~= nil then
print("图像裁剪成功")
local newWidth = croppedImage:width()
local newHeight = croppedImage:height()
print("裁剪后尺寸:" .. newWidth .. "x" .. newHeight)
croppedImage:save(cropped_image)
if croppedImage:binarize(128, 255, 0) then
croppedImage:save(cropped_binary)
end
else
print("图像裁剪失败,请检查裁剪参数")
end
else
print("图片加载失败")
end
else
print("创建QImage对象失败")
end12. 绘制一条直线 (QImage.drawLine)
说明: 绘制一条直线
函数: image:drawLine(x0, y0, x1,y1,argb,thickness)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| x0 | 整数 | 起始点X坐标 |
| y0 | 整数 | 起始点Y坐标 |
| x1 | 整数 | 终点X坐标 |
| y1 | 整数 | 终点Y坐标 |
| argb | 整数 | 表示用什么颜色绘制例如0xffff0000 (红色) |
| thickness | 整数 | 线条的粗细(厚度) |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型 |
示例:
lua
local img = QImage.new()
local ret = img:snapShot()
local savepath = getWorkPath() .. "/save.png"
if ret then
img:drawLine(100,100,300,300,0xffff0000,30)
img:save(savepath)
print("绘制完成保存路径:",savepath)
end13. 绘制矩形框 (QImage.drawRect)
说明: 绘制矩形框
函数: image:drawRect(x0, y0, x1,y1,argb,thickness)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| x0 | 整数 | 起始点X坐标 |
| y0 | 整数 | 起始点Y坐标 |
| x1 | 整数 | 终点X坐标 |
| y1 | 整数 | 终点Y坐标 |
| argb | 整数 | 表示用什么颜色绘制例如0xffff0000 (红色) |
| thickness | 整数 | 线条的粗细(厚度),-1 表示填充 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型 |
示例:
lua
local img = QImage.new()
local ret = img:snapShot()
local savepath = getWorkPath() .. "/save.png"
if ret then
img:drawRect(100,100,300,300,0xffff0000,5)
img:save(savepath)
print("绘制完成保存路径:",savepath)
end14. 绘制圆形 (QImage.drawCircle)
说明: 绘制圆形
函数: image:drawCircle(x, y, radius,argb,thickness)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| x | 整数 | 圆心X坐标 |
| y | 整数 | 圆心Y坐标 |
| radius | 整数 | 圆的半径 |
| argb | 整数 | 表示用什么颜色绘制例如0xffff0000 (红色) |
| thickness | 整数 | 线条的粗细(厚度),-1 表示填充 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型 |
示例:
lua
local img = QImage.new()
local ret = img:create(500,500,0xffffffff)
local savepath = getWorkPath() .. "/save.png"
if ret then
img:drawCircle(250,250,100,0xffff0000,5)
img:save(savepath)
print("绘制完成保存路径:",savepath)
end15. 填充一个矩形区域 (QImage.fillRect)
说明: 填充一个矩形区域
函数: QImage.fillRect(x0, y0, x1,y1,argb)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| x0 | 整数 | 起始点X坐标 |
| y0 | 整数 | 起始点Y坐标 |
| x1 | 整数 | 终点X坐标 |
| y1 | 整数 | 终点Y坐标 |
| argb | 整数 | 表示用什么颜色绘制例如0xffff0000 (红色) |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型 |
示例:
lua
local img = QImage.new()
local ret = QImage.fillRect(500,500,0xffffffff)
local savepath = getWorkPath() .. "/save.png"
if ret then
QImage:fillRect(100,100,300,300,0xffff0000)
QImage:save(savepath)
print("创建白色画布完成,保存路径:",savepath)
end16. 绘制多边形 (QImage.drawPolygon)
说明: 绘制多边形
函数: image:drawPolygon(points, argb, thickness,isclosed)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| points | 数组 | 多边形顶点坐标数组,格式为{{x1,y1}, {x2,y2}, ...} |
| argb | 整数 | 表示用什么颜色绘制例如0xffff0000 (红色) |
| thickness | 整数 | 线条厚度,如果为-1则表示填充多边形 |
| isclosed | 布尔 | 这个多边形是否闭合 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回一个布尔类型,表示绘制是否成功 |
示例:
lua
local img = QImage.new()
local ret = img:create(500,500,0xffffffff)
local savepath = getWorkPath() .. "/polygon.png"
if ret then
local points = {
{250, 100},
{400, 200},
{350, 350},
{150, 350},
{100, 200}
}
img:drawPolygon(points, 0xffff0000, 3,true)
local trianglePoints = {
{250, 150},
{200, 250},
{300, 250}
}
img:drawPolygon(trianglePoints, 0xff0000ff, -1,true)
img:save(savepath)
print("绘制多边形完成,保存路径:",savepath)
end17. 区域多点找色 (findMultiColor)
说明: 区域多点找色
函数: findMultiColor([image],x1,y1,x2,y2,first_color,offset_color,dir,sim)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| x1 | 整数 | 查找区域左上X坐标 |
| y1 | 整数 | 查找区域左上Y坐标 |
| x2 | 整数 | 查找区域右下X坐标 |
| y2 | 整数 | 查找区域右下Y坐标 |
| first_color | 字符串 | 要对比的16进制颜色,多个颜色用" |
| offset_color | 字符串 | 偏移颜色 |
| dir | 整数 | 查找方向:0:表示从左上向右下查找;1:表示从中心往四周查找;2:表示从右下向左上查找;3:表示从左下向右上查找;4:表示从右上向左下查找 |
| sim | 浮点数 | 相似度,取值范围0-1 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 如果成功返回对应点的坐标x,y,否则返回-1,-1 |
示例:
lua
local firstColor = "757575-101010"
local offsetColor = "757575-101010" , "0|2|757575-101010|0|4|757575-101010|4|5|ffffff-101010|5|52|359bed-101010|-10|82|ffffff-101010|133|437|3e9eeb-101010|133|432|3e9eeb-101010|137|432|cfcfcf-101010"
local x , y = findMultiColor(0 , 0 , 0 , 0 , firstColor , offsetColor , 0 , 0.9)
if x~= nil and y ~= nil then
print(x , y)
end
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
x , y = findMultiColor(img , 0 , 0 , 0 , 0 , firstColor , offsetColor , 0 , 0.9)
if x~= nil and y ~= nil then
print(x , y)
end
end
end18. 区域多点找色 (findMultiColorT)
说明: 区域多点找色
函数: findMultiColorT([image],{x1,y1,x2,y2,first_color,offset_color,dir,sim})
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| tb | 表 | 表类型,其中每一项的参数如下:x1:整数型,查找区域左上X坐标;y1:整数型,查找区域左上Y坐标;x2:整数型,查找区域右下X坐标;y2:整数型,查找区域右下Y坐标;first_color:要对比的16进制颜色,多个颜色用" |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 如果成功返回对应点的坐标x,y,否则返回-1,-1 |
示例:
lua
local tb = {0,0,0,0,"359bed-101010","30|123|464646-101010|35|159|ffffff-101010|-43|306|cfcfcf-101010|-3|333|bbbbbb-101010|112|366|ffffff-101010|104|391|359bed-101010|-182|-33|ffffff-101010|-174|-15|359bed-101010",0,0.9}
local x , y = findMultiColorT(tb)
if x~= nil and y ~= nil then
print(x , y)
end
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
x , y = findMultiColorT(img,tb)
if x~= nil and y ~= nil then
print(x , y)
end
end
end19. 区域多点找色匹配所有目标 (findMultiColorAll)
说明: 区域多点找色匹配所有目标
函数: findMultiColorAll([image],x1,y1,x2,y2,first_color,offset_color,dir,sim)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| x1 | 整数 | 查找区域左上X坐标 |
| y1 | 整数 | 查找区域左上Y坐标 |
| x2 | 整数 | 查找区域右下X坐标 |
| y2 | 整数 | 查找区域右下Y坐标 |
| first_color | 字符串 | 要对比的16进制颜色,多个颜色用" |
| offset_color | 字符串 | 偏移颜色 |
| dir | 整数 | 查找方向:0:表示从左上向右下查找;1:表示从中心往四周查找;2:表示从右下向左上查找;3:表示从左下向右上查找;4:表示从右上向左下查找 |
| sim | 浮点数 | 相似度,取值范围0-1 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 表 | 如果成功返回所有满足要求的表格数组 |
示例:
lua
local firstColor = "757575-101010"
local offsetColor = "757575-101010" , "0|2|757575-101010|0|4|757575-101010|4|5|ffffff-101010|5|52|359bed-101010|-10|82|ffffff-101010|133|437|3e9eeb-101010|133|432|3e9eeb-101010|137|432|cfcfcf-101010"
local ret = findMultiColorAll(0 , 0 , 0 , 0 , firstColor , offsetColor , 0 , 0.9)
if ret ~= nil then
print(ret)
end
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
ret = findMultiColorAll(img , 0 , 0 , 0 , 0 , firstColor , offsetColor , 0 , 0.9)
if ret~= nil then
print(ret)
end
end
end20. 区域多点找色匹配所有目标 (findMultiColorAllT)
说明: 区域多点找色匹配所有目标
函数: findMultiColorAll([image],{x1,y1,x2,y2,first_color,offset_color,dir,sim})
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| tb | 表 | 表类型,其中每一项的参数如下:x1:整数型,查找区域左上X坐标;y1:整数型,查找区域左上Y坐标;x2:整数型,查找区域右下X坐标;y2:整数型,查找区域右下Y坐标;first_color:要对比的16进制颜色,多个颜色用" |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 表 | 如果成功返回所有满足要求的表格数组 |
示例:
lua
local tb = {0,0,0,0,"262626-101010","1|3|222222-101010|-2|61|ffffff-101010|-24|90|242424-101010|-23|92|222222-101010",0,0.9}
local ret = findMultiColorAllT(tb)
if ret ~= nil then
print(ret)
end
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
ret = findMultiColorAllT(img ,tb)
if ret~= nil then
print(ret)
end
end
end21. 区域单点找色 (findColor)
说明: 在指定区域内查找指定的颜色
函数: findColor([image],x1, y1, x2, y2,color,dir,sim)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| x1 | 整数 | 区域左上角x坐标 |
| y1 | 整数 | 区域左上角y坐标 |
| x2 | 整数 | 区域右下角x坐标 |
| y2 | 整数 | 区域右下角y坐标 |
| color | 字符串 | 要对比的16进制颜色,格式为"BBGGRR"多个颜色用" |
| dir | 整数 | 查找方向:0: 表示从左上向右下;1: 表示从中心往四周查找;2: 表示从右下向左上查找;3: 表示从左下向右上查找;4:表示从右上向左下查找 |
| sim | 浮点数 | 相似度,取值范围0-1 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | ret:如果有多个颜色用" |
示例:
lua
local index = 0-1
local x = 0-1
local y = 0-1
local color = "ffffff-101010|ffffff-101010|ffffff-101010|cfcfcf-101010"
index,x,y= findColor(0 , 0 , 0 , 0 , color , 0 , 0.9)
print(index , x , y)
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
index,x,y = findColor(img,0 , 0 , 0 , 0 , color , 0 , 0.9)
print(index , x , y)
end
end22. 区域单点找色 (findColorT)
说明: 在指定区域内查找指定的颜色
函数: findColorT([image],tb)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| tb | 表 | 表类型,其中每一项的参数如下:x1:整数型,区域左上角x坐标;y1:整数型,区域左上角y坐标;x2:整数型,区域右下角x坐标;y2:整数型,区域右下角y坐标;color 字符串:要对比的16进制颜色,格式为"BBGGRR"多个颜色用" |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | ret:如果有多个颜色用" |
示例:
lua
local index = nil
local x = nil
local y = nil
local tb = {0,0,0,0,"ffffff-101010|ffffff-101010|ffffff-101010|359bed-101010",0,0.9}
index,x,y= findColorT(tb)
print(index , x , y)
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
index,x,y = findColorT(tb)
print(index , x , y)
end
end23. 区域多点比色 (cmpColorEx)
说明: 同时比较指定的多个坐标点的颜色,支持多色、偏色、相似度比较
函数: cmpColorEx(mul_color,sim)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| mul_color | 字符串 | 需要对比的点xy坐标和16进制颜色,格式为(X坐标|Y坐标|16进制颜色),多个颜色用" |
| sim | 浮点数 | 相似度,取值范围0-1 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 整型,1为完全匹配,0为没有完全匹配到 |
示例:
lua
local color = "398|962|2b2b2b-101010,397|963|222222-101010,410|958|222222-101010"
local ret = cmpColorEx(color , 0.9)
if ret == 1 then
print("找到了")
else
print("没有找到")
end
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
local ret = cmpColorEx(img,color , 0.9)
if ret == 1 then
print("找到了")
else
print("没有找到")
end
end
end24. 区域多点比色 (cmpColorExT)
说明: 同时比较指定的多个坐标点的颜色,支持多色、偏色、相似度比较
函数: cmpColorExT([image],{mul_color,sim})
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| tb | 表 | 表类型,其中每一项的参数如下:mul_color:字符串,需要对比的点xy坐标和16进制颜色,格式为(X坐标|Y坐标|16进制颜色),多个颜色用" |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 整型,1为完全匹配,0为没有完全匹配到 |
示例:
lua
local tb = {"398|962|2b2b2b-101010,397|963|222222-101010,410|958|222222-101010",0.9}
local ret = cmpColorExT(tb)
if ret == 1 then
print("找到了")
else
print("没有找到")
end
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
local ret = cmpColorExT(img,tb)
if ret == 1 then
print("找到了")
else
print("没有找到")
end
end
end25. 指定坐标颜色比对 (cmpColor)
说明: 指定坐标颜色比对
函数: cmpColor([image],x,y,color,sim)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| x | 整数 | 需要对比颜色的X坐标 |
| y | 整数 | 需要对比颜色的Y坐标 |
| color | 字符串 | 待比较的16进制颜色,格式为"BBGGRR",多个颜色用" |
| sim | 浮点数 | 相似度,取值范围0-1 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 整型,1为匹配到,0为没有匹配到 |
示例:
lua
local color = "ffffff-101010|cfcfcf-101010|ffffff-101010"
local ret = cmpColor(198 , 104 , color , 0.9)
if ret == 1 then
print("找到了")
else
print("没有找到")
end
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
local ret = cmpColor(img , 198 , 104 , color , 0.9)
if ret == 1 then
print("找到了")
else
print("没有找到")
end
end
end26. 颜色值转化R,G,B三个分量 (colorToRGB)
说明: 颜色比对
函数: colorToRGB(color)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| color | 字符串/整数 | 16进制颜色,格式为"BBGGRR",或者10进制颜色 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | r:整数型,十进制颜色红分量 |
| 整数 | g:整数型,十进制颜色绿分量 |
| 整数 | b:整数型,十进制颜色蓝分量 |
示例:
lua
local r,g,b = colorToRGB(0x998899)
print(r,g,b)27. 颜色比对 (colorDiff)
说明: 颜色比对
函数: colorDiff(color1,color2)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| color1 | 字符串/整数 | 16进制颜色,格式为"BBGGRR",或者10进制颜色 |
| color2 | 字符串/整数 | 16进制颜色,格式为"BBGGRR",或者10进制颜色 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 整数型:两个颜色的红、绿、蓝三个分量的差值之总和 |
示例:
lua
local diff = colorDiff(0x998899,0x777777)
local diff1 = colorDiff("999999","888888")
print(diff)
print(diff1)28. 获取指定范围内的像素数组 (getScreenPixel)
说明: 获取指定范围内的像素数组
函数: getScreenPixel([image],x1,y1,x2,y2)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| x1 | 整数 | 区域左上角x坐标 |
| y1 | 整数 | 区域左上角y坐标 |
| x2 | 整数 | 区域右下角x坐标 |
| y2 | 整数 | 区域右下角y坐标 |
说明: 当x1,y1,x2,y2全部为0表示获取全屏像素值
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | w,h是获取范围的长宽 |
| 数组 | arr是返回的数组,如果w,h小于0则表明失败 |
示例:
lua
function printRGB(color)
local r , g , b = colorToRGB(color)
print("col:".."=>"..r..","..g..","..b)
end
function getScreenPixelTest(useimage)
local img = QImage.new()
if img ~= nil then
img:snapShot()
end
if useimage then
w , h , arr = getScreenPixel(img , 0 , 0 , 0 , 0)
else
w , h , arr = getScreenPixel(0 , 0 , 0 , 0)
end
print("image size:"..w..","..h)
local x = 50
local y = 50
local color = arr[y * w + (x + 1)]
local cc = getPixelColor(x , y)
printRGB(cc)
printRGB(color)
end
getScreenPixelTest(false)
getScreenPixelTest(true)29. 获取指定坐标的颜色值 (getPixelColor)
说明: 获取指定坐标处屏幕的颜色值
函数: getPixelColor([image],x,y,[type])
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| x | 整数 | 水平坐标 |
| y | 整数 | 垂直坐标 |
| type | 整数 | 值为1的时候返回一个整数,默认不填写type是返回颜色的16进制字符串 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 字符串/整数 | 返回指定坐标处的颜色值 |
示例:
lua
local pixel = getPixelColor(100,100,0)
print(pixel)
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
local ret = getPixelColor(img ,100,100,0)
print(ret)
end
end30. 截图并保存 (snapShot)
说明: 截图并保存
函数: snapShot(path,[left,top,right,bottom])
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| path | 字符串 | 保存路径 |
| left | 整数 | 截图区域左边界坐标(可选) |
| top | 整数 | 截图区域上边界坐标(可选) |
| right | 整数 | 截图区域右边界坐标(可选) |
| bottom | 整数 | 截图区域下边界坐标(可选) |
返回值:
| 返回值类型 | 说明 |
|---|---|
| boolean | 返回截图是否成功 |
示例:
lua
local savepath = getWorkPath() .. "/save.png"
local ret = snapShot(savepath,0,0,100,100)
if ret then
print("截图完成")
else
print("截图失败")
end31. 截图到内存 (keepCapture)
说明: 截取当前屏幕内容保留在内存,可以供后续图色查找
函数: keepCapture()
参数: 无
返回值: 无
示例:
lua
keepCapture()
for i = 1 , 10 , 1 do
local x = 0-1
local y = 0-1
x , y = findMultiColor(0 , 0 , 0 , 0 , "359bed-101010" , "45|76|ffffff-101010|93|114|ffffff-101010|33|146|ffffff-101010|-9|432|359bed-101010|58|447|359bed-101010" , 0 , 0.9)
if x ~= 0-1 and y ~= 0-1 then
print(x , y)
end
end
releaseCapture()32. 删除内存中的截图 (releaseCapture)
说明: 删除内存中的截图
函数: releaseCapture()
参数: 无
返回值: 无
示例:
lua
keepCapture()
for i = 1 , 10 , 1 do
local x = 0-1
local y = 0-1
x , y = findMultiColor(0 , 0 , 0 , 0 , "359bed-101010" , "45|76|ffffff-101010|93|114|ffffff-101010|33|146|ffffff-101010|-9|432|359bed-101010|58|447|359bed-101010" , 0 , 0.9)
if x ~= 0-1 and y ~= 0-1 then
print(x , y)
end
end
releaseCapture()33. opencv模板匹配找图 (findImage)
说明: opencv模板匹配找图
函数: findImage([image],x1, y1, x2, y2,pic_name,sim)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| x1 | 整数 | 查找区域左上X坐标 |
| y1 | 整数 | 查找区域左上Y坐标 |
| x2 | 整数 | 查找区域右下X坐标 |
| y2 | 整数 | 查找区域右下Y坐标 |
| pic_name | 字符串 | 要找的图片名字,多个图片用" |
| sim | 浮点数 | 相似度,取值范围0-1 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | ret:如果有多个颜色用" |
| 整数 | x,y 对应找到的坐标 |
示例:
lua
index , x , y = findImage(0 , 0 , 0 , 0 , "test2.png|test.png" , 0.9)
print(index , x , y)
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
index , x , y = findImage(img , 0 , 0 , 0 , 0 , "test2.png|test.png" , 0.9)
print(index , x , y)
end
end34. 高级区域找图 (findPicEx)
说明: 高级区域找图
函数: findPicEx([image],x1, y1, x2, y2,pic_name,sim)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| x1 | 整数 | 查找区域左上X坐标 |
| y1 | 整数 | 查找区域左上Y坐标 |
| x2 | 整数 | 查找区域右下X坐标 |
| y2 | 整数 | 查找区域右下Y坐标 |
| pic_name | 字符串 | 要找的图片名字,多个图片用" |
| sim | 浮点数 | 相似度,取值范围0-1 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | ret:如果有多个颜色用" |
| 整数 | x,y 对应找到的坐标 |
示例:
lua
idx , x , y = findPicEx(0 , 0 , 0 , 0 , "1.png|2.png" , 0.9)
print(idx , x , y)
local img = QImage.new()
if img ~= nil then
if img:snapShot() then
idx , x , y = findPicEx(img , 0 , 0 , 0 , 0 , "1.png|2.png" , 0.9)
print(idx , x , y)
end
end35. 快速区域找图 (findPicFast)
说明: 快速区域找图(暂时不支持透明图)
函数: findPicFast(x1, y1, x2, y2,pic_name,delta_color,dir,sim)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| x1 | 整数 | 查找区域左上X坐标 |
| y1 | 整数 | 查找区域左上Y坐标 |
| x2 | 整数 | 查找区域右下X坐标 |
| y2 | 整数 | 查找区域右下Y坐标 |
| pic_name | 字符串 | 要找的图片名字,多个图片用" |
| delta_color | 字符串 | 16进制字符串,偏色 |
| dir | 整数 | 查找方向:0:表示从左上向右下查找;1:表示从中心往四周查找;2:表示从右下向左上查找;3:表示从左下向右上查找;4:表示从右上向左下查找 |
| sim | 浮点数 | 相似度,取值范围0-1 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | index:如果有多个颜色用" |
| 整数 | x,y 对应找到的坐标 |
示例:
lua
index , x , y = findPicFast(0 , 0 , 0 , 0 , "1.png|2.png" , "101010" , 0 , 0.9)
print(index , x , y)36. 快速区域找图 (findPicFastT)
说明: 快速区域找图(暂时不支持透明图)
函数: findPicFastT([image],{x1, y1, x2, y2,pic_name,delta_color,dir,sim})
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| tb | 表 | 表类型,其中每一项的参数如下:x1:整数型,查找区域左上X坐标;y1:整数型,查找区域左上Y坐标;x2:整数型,查找区域右下X坐标;y2:整数型,查找区域右下Y坐标;pic_name:字符串,要找的图片名字,多个图片用" |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | index:如果有多个颜色用" |
| 整数 | x,y 对应找到的坐标 |
示例:
lua
local tb = {0,0,0,0,"1.png|2.png","101010",0,0.9}
index , x , y = findPicFastT(tb)
print(index , x , y)37. 快速区域找图匹配所有目标 (findPicFastAll)
说明: 快速区域找图匹配所有目标(暂时不支持透明图)
函数: findPicFastAll(x1, y1, x2, y2,pic_name,delta_color,dir,sim)
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| image | userdata | QImage对象类型,如果第一个参数填写了就表示从QImage取图,否则表示从系统截图 |
| x1 | 整数 | 查找区域左上X坐标 |
| y1 | 整数 | 查找区域左上Y坐标 |
| x2 | 整数 | 查找区域右下X坐标 |
| y2 | 整数 | 查找区域右下Y坐标 |
| pic_name | 字符串 | 要找的图片名字,多个图片用" |
| delta_color | 字符串 | 16进制字符串,偏色 |
| dir | 整数 | 查找方向:0:表示从左上向右下查找;1:表示从中心往四周查找;2:表示从右下向左上查找;3:表示从左下向右上查找;4:表示从右上向左下查找 |
| sim | 浮点数 | 相似度,取值范围0-1 |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 表 | 返回所有满足要求的表格数组 |
示例:
lua
local ret = findPicFastAll(0 , 0 , 0 , 0 , "1.png|2.png" , "101010" , 0 , 0.9)
if ret ~= nil then
print(ret)
end