模板匹配
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)
result = cv2.matchTemplate(target_, template_, cv2.TM_CCOEFF_NORMED)
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'{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
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)
ret, thresh = cv2.threshold(imgray_gauss, start_gray, 255, 0)
contours, hierarchy = cv2.findContours(thresh, 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()
canny边缘检测后显示轮廓
def show_window_canny(image_path):
"""
canny边缘检测后,画出边缘图
"""
im = cv2.imread(image_path)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)