#NoEnv ;不检查空变量是否为环境变量(建议所有新脚本使用)。
SendMode Input
;SendInput 和 SendPlay 与 Send 使用相同的语法, 但通常更快更可靠. 此外, 它们缓存了发送期间任何物理的键盘或鼠标活动, 这样避免了在发送时夹杂用户的键击。
SetWorkingDir %A_ScriptDir% ;让脚本无条件使用它所在的文件夹作为工作目录。
hOpencv := DllCall("LoadLibrary", "str", "opencv_world455.dll", "ptr")
;预加载opencv_world455.dll,避免了 DllCall 内部每次调用 LoadLibrary 和 FreeLibrary 的需要, 预先明确装载它可以显著提高执行效率。
hOpencvCom := DllCall("LoadLibrary", "str", "autoit_opencv_com455.dll", "ptr") ;预加载autoit_opencv_com455.dll。
DllCall("autoit_opencv_com455.dll\DllInstall", "int", 1, "wstr", A_IsAdmin = 0 ? "user" : "", "cdecl")
cv := ComObjCreate("OpenCV.cv") ;创建 COM 对象。
cap := ComObjCreate("OpenCV.cv.VideoCapture") ;创建 COM 对象,后面要用到open()方法来打开摄像头
/*
在opencv中关于视频的读操作是通过VideoCapture类来完成的;关于视频的写操作是通过VideoWriter类来实现的。
VideoCapture既支持从视频文件(.avi , .mpg格式)读取,也支持直接从摄像机(比如电脑自带摄像头)中读取。
*/
frame := ComObjCreate("OpenCV.cv.MAT") ;创建 COM 对象,后面将视频帧读取到Mat矩阵中
/*
默认构造函数,生成一个矩阵并由OpenCV提供的函数(一般是Mat::create() 和 cv::imread() )来分配储存空间。
Mat类可以分为两个部分:矩阵头和指向像素数据的矩阵指针
矩阵头 包括数字图像的矩阵尺寸、存储方法、存储地址和引用次数等,矩阵头的大小是一个常数,不会随着图像的大小而改变,
但是保存图像像素数据的矩阵则会随着图像的大小而改变,通常数据量会很大,比矩阵头大几个数量级。这样,在图像复制和传递过程中,
主要的开销是由存放图像像素的矩阵而引起的。因此,OpenCV使用了引用次数,当进行图像复制和传递时,不再复制整个Mat数据,而只是复制矩阵头和指向像素矩阵的指针
*/
cap.open(0)
/*
bool VideoCapture::open(const string& filename);
bool VideoCapture::open(int device);
功能:打开一个视频文件或者打开一个捕获视频的设备(也就是摄像头)
参数:
filename – 打开的视频文件名。
device – 打开的视频捕获设备id ,如果只有一个摄像头可以填0,表示打开默认的摄像头。
*/
;result := cap.isOpened() ;为了程序的健壮性,我们一般在获取摄像头初始化之后,检验摄像头是否初始化成功。如果打开成功则返回布尔值true,反之为false
While True{ ;建立一个死循环不断的执行下面的操作
ret := cap.read(frame) ;将视频帧读取到Mat矩阵中
/*
cap.read()按帧读取视频,ret是cap.read()方法的返回值。
ret就是每一帧的图像,是个三维矩阵。
*/
img_grey := cv.cvtColor(frame, CV_COLOR_BGR2GRAY := 6) ;这里的参数为一和三,输出为img_grey
/*
第一个参数SRC:输入图像:8位无符号,16位无符号(CV_16UC ...)或单精度浮点。
第二个参数DST:输出与src相同大小和深度的图像。
第三个参数code:颜色空间转换的标识符,opencv cvtcolor支持的空间转发code放在下面文中
第四个参数dstCn:目标图像的通道数,若该参数是0,表示目标图像取源图像的通道数
*/
cv.imshow("Image", img_grey)
/*
cv.imshow(winname, mat)
cv.imshow()函数可以在窗口中显示图像。该窗口和图像的原始大小自适应(自动调整到原始尺寸)
winname: 一个窗口名称(也就是我们对话框的名称),它是一个字符串类型。第二个参数是我们的图像。您可以创建任意数量的窗口,但必须使用不同的窗口名称。
mat: 需要显示的图像
*/
}
cv::COLOR_BGR2BGRA = 0,
cv::COLOR_RGB2RGBA = COLOR_BGR2BGRA,
cv::COLOR_BGRA2BGR = 1,
cv::COLOR_RGBA2RGB = COLOR_BGRA2BGR,
cv::COLOR_BGR2RGBA = 2,
cv::COLOR_RGB2BGRA = COLOR_BGR2RGBA,
cv::COLOR_RGBA2BGR = 3,
cv::COLOR_BGRA2RGB = COLOR_RGBA2BGR,
cv::COLOR_BGR2RGB = 4,
cv::COLOR_RGB2BGR = COLOR_BGR2RGB,
cv::COLOR_BGRA2RGBA = 5,
cv::COLOR_RGBA2BGRA = COLOR_BGRA2RGBA,
cv::COLOR_BGR2GRAY = 6,
cv::COLOR_RGB2GRAY = 7,
cv::COLOR_GRAY2BGR = 8,
cv::COLOR_GRAY2RGB = COLOR_GRAY2BGR,
cv::COLOR_GRAY2BGRA = 9,
cv::COLOR_GRAY2RGBA = COLOR_GRAY2BGRA,
cv::COLOR_BGRA2GRAY = 10,
cv::COLOR_RGBA2GRAY = 11,
cv::COLOR_BGR2BGR565 = 12,
cv::COLOR_RGB2BGR565 = 13,
cv::COLOR_BGR5652BGR = 14,
cv::COLOR_BGR5652RGB = 15,
cv::COLOR_BGRA2BGR565 = 16,
cv::COLOR_RGBA2BGR565 = 17,
cv::COLOR_BGR5652BGRA = 18,
cv::COLOR_BGR5652RGBA = 19,
cv::COLOR_GRAY2BGR565 = 20,
cv::COLOR_BGR5652GRAY = 21,
cv::COLOR_BGR2BGR555 = 22,
cv::COLOR_RGB2BGR555 = 23,
cv::COLOR_BGR5552BGR = 24,
cv::COLOR_BGR5552RGB = 25,
cv::COLOR_BGRA2BGR555 = 26,
cv::COLOR_RGBA2BGR555 = 27,
cv::COLOR_BGR5552BGRA = 28,
cv::COLOR_BGR5552RGBA = 29,
cv::COLOR_GRAY2BGR555 = 30,
cv::COLOR_BGR5552GRAY = 31,
cv::COLOR_BGR2XYZ = 32,
cv::COLOR_RGB2XYZ = 33,
cv::COLOR_XYZ2BGR = 34,
cv::COLOR_XYZ2RGB = 35,
cv::COLOR_BGR2YCrCb = 36,
cv::COLOR_RGB2YCrCb = 37,
cv::COLOR_YCrCb2BGR = 38,
cv::COLOR_YCrCb2RGB = 39,
cv::COLOR_BGR2HSV = 40,
cv::COLOR_RGB2HSV = 41,
cv::COLOR_BGR2Lab = 44,
cv::COLOR_RGB2Lab = 45,
cv::COLOR_BGR2Luv = 50,
cv::COLOR_RGB2Luv = 51,
cv::COLOR_BGR2HLS = 52,
cv::COLOR_RGB2HLS = 53,
cv::COLOR_HSV2BGR = 54,
cv::COLOR_HSV2RGB = 55,
cv::COLOR_Lab2BGR = 56,
cv::COLOR_Lab2RGB = 57,
cv::COLOR_Luv2BGR = 58,
cv::COLOR_Luv2RGB = 59,
cv::COLOR_HLS2BGR = 60,
cv::COLOR_HLS2RGB = 61,
cv::COLOR_BGR2HSV_FULL = 66,
cv::COLOR_RGB2HSV_FULL = 67,
cv::COLOR_BGR2HLS_FULL = 68,
cv::COLOR_RGB2HLS_FULL = 69,
cv::COLOR_HSV2BGR_FULL = 70,
cv::COLOR_HSV2RGB_FULL = 71,
cv::COLOR_HLS2BGR_FULL = 72,
cv::COLOR_HLS2RGB_FULL = 73,
cv::COLOR_LBGR2Lab = 74,
cv::COLOR_LRGB2Lab = 75,
cv::COLOR_LBGR2Luv = 76,
cv::COLOR_LRGB2Luv = 77,
cv::COLOR_Lab2LBGR = 78,
cv::COLOR_Lab2LRGB = 79,
cv::COLOR_Luv2LBGR = 80,
cv::COLOR_Luv2LRGB = 81,
cv::COLOR_BGR2YUV = 82,
cv::COLOR_RGB2YUV = 83,
cv::COLOR_YUV2BGR = 84,
cv::COLOR_YUV2RGB = 85,
cv::COLOR_YUV2RGB_NV12 = 90,
cv::COLOR_YUV2BGR_NV12 = 91,
cv::COLOR_YUV2RGB_NV21 = 92,
cv::COLOR_YUV2BGR_NV21 = 93,
cv::COLOR_YUV420sp2RGB = COLOR_YUV2RGB_NV21,
cv::COLOR_YUV420sp2BGR = COLOR_YUV2BGR_NV21,
cv::COLOR_YUV2RGBA_NV12 = 94,
cv::COLOR_YUV2BGRA_NV12 = 95,
cv::COLOR_YUV2RGBA_NV21 = 96,
cv::COLOR_YUV2BGRA_NV21 = 97,
cv::COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21,
cv::COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21,
cv::COLOR_YUV2RGB_YV12 = 98,
cv::COLOR_YUV2BGR_YV12 = 99,
cv::COLOR_YUV2RGB_IYUV = 100,
cv::COLOR_YUV2BGR_IYUV = 101,
cv::COLOR_YUV2RGB_I420 = COLOR_YUV2RGB_IYUV,
cv::COLOR_YUV2BGR_I420 = COLOR_YUV2BGR_IYUV,
cv::COLOR_YUV420p2RGB = COLOR_YUV2RGB_YV12,
cv::COLOR_YUV420p2BGR = COLOR_YUV2BGR_YV12,
cv::COLOR_YUV2RGBA_YV12 = 102,
cv::COLOR_YUV2BGRA_YV12 = 103,
cv::COLOR_YUV2RGBA_IYUV = 104,
cv::COLOR_YUV2BGRA_IYUV = 105,
cv::COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV,
cv::COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV,
cv::COLOR_YUV420p2RGBA = COLOR_YUV2RGBA_YV12,
cv::COLOR_YUV420p2BGRA = COLOR_YUV2BGRA_YV12,
cv::COLOR_YUV2GRAY_420 = 106,
cv::COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420,
cv::COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420,
cv::COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420,
cv::COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420,
cv::COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420,
cv::COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420,
cv::COLOR_YUV420p2GRAY = COLOR_YUV2GRAY_420,
cv::COLOR_YUV2RGB_UYVY = 107,
cv::COLOR_YUV2BGR_UYVY = 108,
cv::COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY,
cv::COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY,
cv::COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY,
cv::COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY,
cv::COLOR_YUV2RGBA_UYVY = 111,
cv::COLOR_YUV2BGRA_UYVY = 112,
cv::COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY,
cv::COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY,
cv::COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY,
cv::COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY,
cv::COLOR_YUV2RGB_YUY2 = 115,
cv::COLOR_YUV2BGR_YUY2 = 116,
cv::COLOR_YUV2RGB_YVYU = 117,
cv::COLOR_YUV2BGR_YVYU = 118,
cv::COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2,
cv::COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2,
cv::COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2,
cv::COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2,
cv::COLOR_YUV2RGBA_YUY2 = 119,
cv::COLOR_YUV2BGRA_YUY2 = 120,
cv::COLOR_YUV2RGBA_YVYU = 121,
cv::COLOR_YUV2BGRA_YVYU = 122,
cv::COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2,
cv::COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2,
cv::COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2,
cv::COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2,
cv::COLOR_YUV2GRAY_UYVY = 123,
cv::COLOR_YUV2GRAY_YUY2 = 124,
cv::COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY,
cv::COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY,
cv::COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2,
cv::COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2,
cv::COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2,
cv::COLOR_RGBA2mRGBA = 125,
cv::COLOR_mRGBA2RGBA = 126,
cv::COLOR_RGB2YUV_I420 = 127,
cv::COLOR_BGR2YUV_I420 = 128,
cv::COLOR_RGB2YUV_IYUV = COLOR_RGB2YUV_I420,
cv::COLOR_BGR2YUV_IYUV = COLOR_BGR2YUV_I420,
cv::COLOR_RGBA2YUV_I420 = 129,
cv::COLOR_BGRA2YUV_I420 = 130,
cv::COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420,
cv::COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420,
cv::COLOR_RGB2YUV_YV12 = 131,
cv::COLOR_BGR2YUV_YV12 = 132,
cv::COLOR_RGBA2YUV_YV12 = 133,
cv::COLOR_BGRA2YUV_YV12 = 134,
cv::COLOR_BayerBG2BGR = 46,
cv::COLOR_BayerGB2BGR = 47,
cv::COLOR_BayerRG2BGR = 48,
cv::COLOR_BayerGR2BGR = 49,
cv::COLOR_BayerRGGB2BGR = COLOR_BayerBG2BGR,
cv::COLOR_BayerGRBG2BGR = COLOR_BayerGB2BGR,
cv::COLOR_BayerBGGR2BGR = COLOR_BayerRG2BGR,
cv::COLOR_BayerGBRG2BGR = COLOR_BayerGR2BGR,
cv::COLOR_BayerRGGB2RGB = COLOR_BayerBGGR2BGR,
cv::COLOR_BayerGRBG2RGB = COLOR_BayerGBRG2BGR,
cv::COLOR_BayerBGGR2RGB = COLOR_BayerRGGB2BGR,
cv::COLOR_BayerGBRG2RGB = COLOR_BayerGRBG2BGR,
cv::COLOR_BayerBG2RGB = COLOR_BayerRG2BGR,
cv::COLOR_BayerGB2RGB = COLOR_BayerGR2BGR,
cv::COLOR_BayerRG2RGB = COLOR_BayerBG2BGR,
cv::COLOR_BayerGR2RGB = COLOR_BayerGB2BGR,
cv::COLOR_BayerBG2GRAY = 86,
cv::COLOR_BayerGB2GRAY = 87,
cv::COLOR_BayerRG2GRAY = 88,
cv::COLOR_BayerGR2GRAY = 89,
cv::COLOR_BayerRGGB2GRAY = COLOR_BayerBG2GRAY,
cv::COLOR_BayerGRBG2GRAY = COLOR_BayerGB2GRAY,
cv::COLOR_BayerBGGR2GRAY = COLOR_BayerRG2GRAY,
cv::COLOR_BayerGBRG2GRAY = COLOR_BayerGR2GRAY,
cv::COLOR_BayerBG2BGR_VNG = 62,
cv::COLOR_BayerGB2BGR_VNG = 63,
cv::COLOR_BayerRG2BGR_VNG = 64,
cv::COLOR_BayerGR2BGR_VNG = 65,
cv::COLOR_BayerRGGB2BGR_VNG = COLOR_BayerBG2BGR_VNG,
cv::COLOR_BayerGRBG2BGR_VNG = COLOR_BayerGB2BGR_VNG,
cv::COLOR_BayerBGGR2BGR_VNG = COLOR_BayerRG2BGR_VNG,
cv::COLOR_BayerGBRG2BGR_VNG = COLOR_BayerGR2BGR_VNG,
cv::COLOR_BayerRGGB2RGB_VNG = COLOR_BayerBGGR2BGR_VNG,
cv::COLOR_BayerGRBG2RGB_VNG = COLOR_BayerGBRG2BGR_VNG,
cv::COLOR_BayerBGGR2RGB_VNG = COLOR_BayerRGGB2BGR_VNG,
cv::COLOR_BayerGBRG2RGB_VNG = COLOR_BayerGRBG2BGR_VNG,
cv::COLOR_BayerBG2RGB_VNG = COLOR_BayerRG2BGR_VNG,
cv::COLOR_BayerGB2RGB_VNG = COLOR_BayerGR2BGR_VNG,
cv::COLOR_BayerRG2RGB_VNG = COLOR_BayerBG2BGR_VNG,
cv::COLOR_BayerGR2RGB_VNG = COLOR_BayerGB2BGR_VNG,
cv::COLOR_BayerBG2BGR_EA = 135,
cv::COLOR_BayerGB2BGR_EA = 136,
cv::COLOR_BayerRG2BGR_EA = 137,
cv::COLOR_BayerGR2BGR_EA = 138,
cv::COLOR_BayerRGGB2BGR_EA = COLOR_BayerBG2BGR_EA,
cv::COLOR_BayerGRBG2BGR_EA = COLOR_BayerGB2BGR_EA,
cv::COLOR_BayerBGGR2BGR_EA = COLOR_BayerRG2BGR_EA,
cv::COLOR_BayerGBRG2BGR_EA = COLOR_BayerGR2BGR_EA,
cv::COLOR_BayerRGGB2RGB_EA = COLOR_BayerBGGR2BGR_EA,
cv::COLOR_BayerGRBG2RGB_EA = COLOR_BayerGBRG2BGR_EA,
cv::COLOR_BayerBGGR2RGB_EA = COLOR_BayerRGGB2BGR_EA,
cv::COLOR_BayerGBRG2RGB_EA = COLOR_BayerGRBG2BGR_EA,
cv::COLOR_BayerBG2RGB_EA = COLOR_BayerRG2BGR_EA,
cv::COLOR_BayerGB2RGB_EA = COLOR_BayerGR2BGR_EA,
cv::COLOR_BayerRG2RGB_EA = COLOR_BayerBG2BGR_EA,
cv::COLOR_BayerGR2RGB_EA = COLOR_BayerGB2BGR_EA,
cv::COLOR_BayerBG2BGRA = 139,
cv::COLOR_BayerGB2BGRA = 140,
cv::COLOR_BayerRG2BGRA = 141,
cv::COLOR_BayerGR2BGRA = 142,
cv::COLOR_BayerRGGB2BGRA = COLOR_BayerBG2BGRA,
cv::COLOR_BayerGRBG2BGRA = COLOR_BayerGB2BGRA,
cv::COLOR_BayerBGGR2BGRA = COLOR_BayerRG2BGRA,
cv::COLOR_BayerGBRG2BGRA = COLOR_BayerGR2BGRA,
cv::COLOR_BayerRGGB2RGBA = COLOR_BayerBGGR2BGRA,
cv::COLOR_BayerGRBG2RGBA = COLOR_BayerGBRG2BGRA,
cv::COLOR_BayerBGGR2RGBA = COLOR_BayerRGGB2BGRA,
cv::COLOR_BayerGBRG2RGBA = COLOR_BayerGRBG2BGRA,
cv::COLOR_BayerBG2RGBA = COLOR_BayerRG2BGRA,
cv::COLOR_BayerGB2RGBA = COLOR_BayerGR2BGRA,
cv::COLOR_BayerRG2RGBA = COLOR_BayerBG2BGRA,
cv::COLOR_BayerGR2RGBA = COLOR_BayerGB2BGRA,
cv::COLOR_COLORCVT_MAX = 143
cvtColor 函数颜色空间(color space)说明
色彩空间转换都用到以下约定:8位图像范围是0~255,16位图像范围是0~65536,浮点数的范围是0.0~1.0。黑白图像转换为彩色图像时,最终图像的所有通道都是相同的;但是逆变换(例如RGB或BGR到灰度),灰度值的计算使用加权公式:
Y=(0.299)R+(0.587)G+(0.114)B
就HSV色彩模式或者HLS色彩模式来说,色调通常是在0~360之间。 在8位图中,这可能出现问题,因此,转换到HSV色彩模式,并以8位图的形式输出时,色调应该除以2。
关于cvtColor 函数颜色空间(color space)说明,转载自cvtColor 函数颜色空间(color space)说明
上面用到的dll和图片蓝奏云链接
天黑版opencv_ahk.dll使用(改变了调用方式,优化速度…)
相关文件:https://wwz.lanzouw.com/iAkK803eaaud
cv2.ahk和log.ahk来自社区群友zzZ…
可以用文件中的天黑版的v2h版ahk运行。
示例:从相机捕捉视频
#DllLoad opencv_ahk.dll
#include <cv2>
#include <log>
SetWorkingDir A_ScriptDir
;初始化opencv模块
cv := ObjFromPtr(DllCall('opencv_ahk.dll\opencv_init', 'ptr', DllCall(A_AhkPath '\ahkGetApi', 'ptr'), 'cdecl ptr'))
cap := cv.VideoCapture()
frame := cv.Mat()
cap.open(0)
result := cap.isOpened()
while result{
ret := cap.read(frame)
cv.imshow("image", frame)
}
cv.waitKey()
cv.destroyAllWindows()
有错误请联系我改正!
本系列所有贡献者(AutoHotKey中文社区群友)不分先后:天黑请闭眼,zzZ…,演好自己,僵尸,城西,Tebayaki。
图片正文中也放一下,最上面是封面的图片
多谢老猪ssssssssssssss