一、cv函数
1、imread:读取图片
imread(image_path, flag):
images_path:图片路径,找不到不报错
flag:
1/cv2.IMREAD_COLOR:彩色图片,图片透明性会被忽略,默认参数
0/cv2.IMREAD_GRAYSCALE:灰色图片
-1/cv2.IMREAD_UNCHANGED:包括其alpha通道
2、imwrite
imwrite(img_path_name,img)
img_path_name:保存的文件名
img:文件对象
3、cvtColor
cvtColor(img,code)
img: 图像对象
code:
cv2.COLOR_RGB2GRAY: RGB转换到灰度模式
cv2.COLOR_RGB2HSV: RGB转换到HSV模式(hue,saturation,Value)
4、matchTemplate
matchTemplate(img_path, bg_path, cv2.TM_CCOEFF_NORMED)
img_path:对比图片
bg_path:背景图片
cv2.TM_CCOEFF_NORMED
```
# encoding=utf8
import cv2
import numpyas np
def show(name):
cv2.imshow('Show', name)
cv2.waitKey(0)
cv2.destroyAllWindows()
def main():
otemp ='./images/tb.png'
oblk ='./images/bg.jpg'
target = cv2.imread(otemp, 0)
template = cv2.imread(oblk, 0)# 读取到两个图片,进行灰值化处理
w, h = target.shape[::-1]
aa = target.shape
print(aa)
print(w, h)
temp ='./images/temp.jpg'
targ ='./images/targ.jpg'
cv2.imwrite(temp, template)
cv2.imwrite(targ, target)# 处理后进行保存
target = cv2.imread(targ)
target = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)# 转化到灰度
target =abs(255 - target)# 返回绝对值
cv2.imwrite(targ, target)# 重新写入
target = cv2.imread(targ)
template = cv2.imread(temp)
result = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED)# 进行匹配
x, y = np.unravel_index(result.argmax(), result.shape)# 通过np转化为数值,就是坐标
print(y, x)
# 展示圈出来的区域
cv2.rectangle(template, (y, x), (y + w, x + h), (7, 249, 151), 2)
show(template)
return y, x
if __name__ =='__main__':
a, b = main()
```