python行业缺口_opencv-python 缺口识别

本文介绍了OpenCV中的几个关键函数,如imread、imwrite、cvtColor和matchTemplate,用于读取、保存图片、颜色转换以及模板匹配。通过示例代码展示了如何将RGB图片转换为灰度,进行模板匹配并找到匹配区域,最后在模板上画出匹配框。
摘要由CSDN通过智能技术生成

一、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()

```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值