模板匹配
import os
import matplotlib.pyplot as plt
import cv2
def opencv_compare(dst_target_dir, target, dst_template_dir, template):
"""
获取 opencv操作后得到的 分值; 这里1为最大值;0.95为阈值,>0.95的为OK
:param dst_target_dir: 须进入到目标目录使用; 这样可以防止中文路径
:param dst_template_dir: 须进入到目标目录使用; 这样可以防止中文路径
:param target:
:param template:
:return:
"""
os.chdir(dst_target_dir)
target_ = cv2.imread(target)
# 读取模板图片
os.chdir(dst_template_dir)
template_ = cv2.imread(template)
# 执行模板匹配,采用的匹配方式cv2.TM_CCOEFF_NORMED
result = cv2.matchTemplate(target_, template_, cv2.TM_CCOEFF_NORMED)
# 寻找矩阵(一维数组当做向量,用Mat定义)中的最大值和最小值的匹配结果及其位置
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# 打印出当前的值
return max_val, max_loc
坐标值获取
def get_gray_xy(image_path) -> dict:
"""
通过左键点击灰度图片,获取点击点的坐标
可以在图上标出坐标值
:param image_path:
:return:
"""
dic_xy = {}
# 读取图片
image = cv2.imread(image_path)
# 图片转为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
dic_xy['pic_1x'] = 0
dic_xy['pic_1y'] = 0
def on_mouse(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
xy = "%d,%d" % (x, y)
dic_xy['pic_1x'] = x
dic_xy['pic_1y'] = y
cv2.circle(image, (x, y), 2, (255, 0, 0), thickness=-1)
cv2.putText(image, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
4.0, (0, 0, 255), thickness=3)
cv2.imshow("image", image)
print(type(x))
print(dic_xy)
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
cv2.setMouseCallback("image", on_mouse)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
return dic_xy
灰度值获取
def get_gray_value(image_path) -> int:
"""
通过左键点击灰度图片,获取点击点的灰度值
可以在图上标出灰度值
:param image_path:
:return:
"""
# 读取图片
image = cv2.imread(image_path)
# 图片转为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_value = 0
def on_mouse(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
gray_value = int(gray[y, x])
# gray_value_str = f'({x},{y}):{str(gray_value)}' # 带坐标
gray_value_str = f'{str(gray_value)}'
cv2.circle(image, (x, y), 2, (255, 0, 0), thickness=-1)
cv2.putText(image, gray_value_str, (x, y), cv2.FONT_HERSHEY_PLAIN,
3.0, (0, 0, 255), thickness=3)
cv2.imshow("image", image)
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
cv2.setMouseCallback("image", on_mouse)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
return gray_value
特定像素点个数
def get_gray_white_num(image_path, start_gray, end_gray):
"""
获取需要统计的像素数、总的像素数、比率
:param end_gray: 开始的灰度值 如248
:param start_gray: 结束的灰度值 如252
:param image_path:
:return:需要统计的像素数、总的像素数、比率
"""
# 读取图片
image = cv2.imread(image_path)
# 图片转为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 总的像素值
gray_size = gray.size
# [gray]:图像;[0]:灰度;None:mask掩模图像;[256]:分为256份,一份1个的求和;[0,256]:像素范围
hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
num = 0
for i in range(start_gray, end_gray + 1):
num = num + hist[i]
return int(num), gray_size, round(int(num) / gray_size, 5)
阈值化后显示轮廓
def show_window_threshold(image_path, start_gray):
"""
显示轮廓
"""
im = cv2.imread(image_path)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
imgray_gauss = cv2.GaussianBlur(imgray, (3, 3), 0) # 高斯滤波
# 返回值: thresh:阈值化后的图像
ret, thresh = cv2.threshold(imgray_gauss, start_gray, 255, 0)
# 返回值:轮廓,轮廓层析结构
# 轮廓是一个 Python列表,其中存储这图像中的所有轮廓。
# 每一个轮廓都是一个 Numpy 数组,包含对象边界点( x, y)的坐标
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# 画图 参数1:原始图件;参数2:轮廓;参数3:轮廓索引;参数45:颜色与厚度
im = cv2.drawContours(im, contours, -1, (0, 255, 0), 3)
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
cv2.imshow("image", im)
cv2.waitKey()
cv2.destroyAllWindows()
canny边缘检测后显示轮廓
def show_window_canny(image_path):
"""
canny边缘检测后,画出边缘图
"""
im = cv2.imread(image_path)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
# 高斯滤波,此处必须有(7,7)的核,不然会有噪点,影响最值点的判断
imgray_gauss = cv2.GaussianBlur(imgray, (7, 7), 0)
canny = cv2.Canny(imgray_gauss, 50, 100)
contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
im = cv2.drawContours(im, contours, -1, (0, 255, 0), 3) # 画图
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
cv2.imshow("image", im)
cv2.waitKey()
cv2.destroyAllWindows()
只显示图片的G通道
def show_change(image_path):
rgb_img = cv2.imread(image_path) # 加载BGR彩色图像
rgb_img[:, :, 0] = 0
rgb_img[:, :, 2] = 0
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', rgb_img)
cv2.waitKey(0)
cv2.destroyAllWindows()