c语言,识别到黑色就自动开枪,4399单击游戏狙击战场,源码分享,豆包ai出品

不好用,识别速度慢,有时候识别不准确

cpp 复制代码
#include <windows.h>
#include <stdio.h>
#include <math.h> 
HDC hdcScreen;
void leftClick();
void RGBtoHSV(int r, int g, int b, int* h, int* s, int* v);
int fuzzyFindColor(int x1, int y1, int x2, int y2,
	int targetH, int targetS, int targetV,
	int hTol, int sTol, int vTol,
	int* outX, int* outY);
void leftClick() {
	// 模拟鼠标左键按下
	mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
	// 短暂延迟,模拟真实点击的持续时间
	Sleep(50);
	// 模拟鼠标左键释放
	mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
// 1. RGB转HSV(纯C工具函数,0<=H<=360,0<=S,V<=100)
void RGBtoHSV(int r, int g, int b, int* h, int* s, int* v) {
	/*
	作用:将输入的 0-255 整数(硬件设备的绝对亮度值)转换为 0-1 的相对比例(消除绝对值影响,统一计算标准)。
	细节:除以 255.0f(浮点型)而非 255(整数),确保结果为浮点数(避免整数除法导致的截断)。
	*/
	float R = r / 255.0f, G = g / 255.0f, B = b / 255.0f;
	float max = (R > G ? (R > B ? R : B) : (G > B ? G : B));
	float min = (R < G ? (R < B ? R : B) : (G < B ? G : B));
	float delta = max - min;


	// 计算色相H
	if (delta < 1e-6) *h = 0;
	else if (max == R) *h = (int)(60 * fmod(((G - B) / delta) + 6, 6));
	else if (max == G) *h = (int)(60 * (((B - R) / delta) + 2));
	else *h = (int)(60 * (((R - G) / delta) + 4));

	// 计算饱和度S
	*s = (max < 1e-6) ? 0 : (int)((delta / max) * 100);

	// 计算明度V
	*v = (int)(max * 100);
}

// 2. 区域模糊找色(返回第一个匹配点坐标)
// 参数:x1,y1/x2,y2=查找区域;targetH/S/V=目标颜色;h/s/vTol=模糊阈值;outX/outY=输出坐标
int fuzzyFindColor(int x1, int y1, int x2, int y2,
	int targetH, int targetS, int targetV,
	int hTol, int sTol, int vTol,
	int* outX, int* outY) {

	// 校验区域合法性
	if (x1 > x2 || y1 > y2) return 0;

	// 获取屏幕设备上下文(DC)
	
	//hdcScreen = CreateDCA("DISPLAY", NULL, NULL, NULL);

	hdcScreen=GetDC(NULL);//2
	if (!hdcScreen) return 0;

	int h, s, v;
	COLORREF pixel; // 存储RGB颜色(0x00BBGGRR)
	int r, g, b;

	// 遍历区域内所有像素
	for (int y = y1; y <= y2; y++) {
		for (int x = x1; x <= x2; x++) {
			// 读取当前像素的RGB值
			pixel = GetPixel(hdcScreen, x, y);
			r = GetRValue(pixel); // 提取红色分量
			g = GetGValue(pixel); // 提取绿色分量
			b = GetBValue(pixel); // 提取蓝色分量

			// RGB转HSV
			RGBtoHSV(r, g, b, &h, &s, &v);

			
		//	printf("RGB %d, %d,%d)\n", r, g, b);
		//	printf("hsv %d, %d,%d)\n", h, s, v);
			// 模糊匹配:判断HSV是否在阈值范围内
			if ((h >= targetH - hTol && h <= targetH + hTol) &&
				(s >= targetS - sTol && s <= targetS + sTol) &&
				(v >= targetV - vTol && v <= targetV + vTol)) {
				*outX = x;
				*outY = y;
				//DeleteDC(hdcScreen); // 释放资源
				ReleaseDC(NULL, hdcScreen);//2
				leftClick();
				return 1; // 找到匹配点
			}
		}
	}

	//DeleteDC(hdcScreen); // 释放资源
	ReleaseDC(NULL, hdcScreen);//2
	return 0; // 未找到
}



// 3. 测试主函数
int main() {

	while (1)
	{
		int matchX, matchY;
		// 需求:在(100,100)-(800,600)区域找HSV(30,80,90),阈值H±3、S±8、V±15
		int found = fuzzyFindColor(537	, 533, 543, 538,
			180, 50, 8,
			179, 49, 7,
			&matchX, &matchY);

		if (found) {
			printf("--------------------------------------------------------------找到匹配点!坐标:(%d, %d)\n", matchX, matchY);
			Sleep(1000);

		}
		else {
			printf("未找到匹配颜色未找到匹配颜色未找到匹配颜色\n");
		}

		//system("pause"); // 暂停查看结果
	}

	

	
	return 0;
}
相关推荐
在所不辞兄3 小时前
【零基础学智能仿真-53】多智能体协作——有限元自动化实战从工程需求到可审核的仿真报告
人工智能·深度学习·神经网络·机器学习·有限元
AI Coder 码农王5 小时前
AgentScope Java v2 第3课,从 ReActAgent 到 HarnessAgent
人工智能
格物致胜工业数智技术5 小时前
明治EOS系列传感器技术解析:精密制造高反光金属件在位检测
人工智能·制造
甲维斯5 小时前
DeepSeek 新版还是难评!
人工智能
晨间晓5 小时前
图片细节丰富不丰富?用华为云码道开发 OpenCV 纹理复杂度分析工具
人工智能·opencv·华为云
2603_965148116 小时前
电商选品调研:通过关键词搜索API获取市场热销商品,结合商品详情API分析竞品卖点与定价
开发语言·数据库·人工智能·python·信息可视化
镜象科技6 小时前
什么是好的校园AI心理健康解决方案服务商?从中外校园心理服务的一天说起
人工智能
Geeys6 小时前
拼多多单品推广完整方案(可直接落地)
大数据·人工智能
泡椒鸡脚6 小时前
AI室内设计风格融合:把布局、材质和光影参考分开写
人工智能·材质
袁俪6 小时前
世界模型与智能体
人工智能