我正在尝试在桌面屏幕截图中检测某些彩色图像,其中我有形状相同但颜色不同的模板(这些模板在使用正常的 matchTemplate 方法时并没有区别,因为它是用灰度图像完成的)这是主要的代码检测:
template = cv2.imread(template_path,1)
#template_hsv = cv2.cvtColor(template, cv2.COLOR_RGB2HSV)
#template_B, template_G, template_R = cv2.split(template)
#scr_B, scr_G, scr_R = cv2.split(screenshot)
scr_B = screenshot[:, :, 0]
scr_G = screenshot[:, :, 1]
scr_R = screenshot[:, :, 2]
template_B = template[:, :, 0]
template_G = template[:, :, 1]
template_R = template[:, :, 2]
#cv2.imwrite('./sc4.png', scr_R)
#cv2.imwrite('./template.png', template)
resB = cv2.matchTemplate(scr_B, template_B, cv2.TM_CCOEFF_NORMED)
resG = cv2.matchTemplate(scr_G, template_G, cv2.TM_CCOEFF_NORMED)
resR = cv2.matchTemplate(scr_R, template_R, cv2.TM_CCOEFF_NORMED)
res = resB + resG + resR
#res = cv2.matchTemplate(screenshot, template_G, cv2.TM_CCOEFF_NORMED)
matches = np.where(res >= 3*threshold)
print(matches)
return matches
如您所见,我尝试拆分 rgb 屏幕截图图像的通道,然后与同样拆分的模板图像进行比较。正如您在注释代码中看到的那样,我还尝试使用 HSV 通道执行此操作。然而这并没有奏效,尽管看到在单通道图像中存在颜色的视觉差异,但程序没有区分它们(我还尝试与模板和屏幕截图的每个单独通道进行比较)。
欢迎所有建议,甚至尝试使用其他任何方法来实现我的目标。先感谢您。