# coding: utf-8
# Author:WangTianRui
# Date :2020/12/14 9:20
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import os, cv2, time
from mpl_toolkits.mplot3d import Axes3D
def load_img(path):
if os.path.exists(path):
return np.array(mpimg.imread(path), dtype="float32").astype(np.uint8)
else:
print("图片路径错误")
return
def plot_3d(x, y, z, title):
fig = plt.figure()
ax = Axes3D(fig)
x, y = np.meshgrid(x, y)
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
# zdir 表示向那个轴投影
# ax.contourf(x, y, img, zdir='z', offset=np.max(z) + 0.5, cmap='gray')
ax.set_title(title)
plt.show()
def show_gray_img(img, title=""):
plt.imshow(img)
plt.title(title)
plt.show()
def corr(frame1, frame2):
return np.sum(frame1 * frame2) / np.sqrt(np.sum(frame1 ** 2) * np.sum(frame2 ** 2))
def argmax_2d(array):
h, w = array.shape
max_idx = np.argmax(array)
return max_idx // w, max_idx % w
def argmin_2d(array):
h, w = array.shape
min_dix = np.argmin(array.flatten())
return min_dix // w, min_dix % w
def ncc(template, scene):
# 中心化
template = template - np.mean(template)
scene = scene - np.mean(scene)
template_h, template_w = template.shape
scene_h, scene_w = scene.shape
print(template_h, template_w, scene_h, scene_w)
# 分窗
strides = scene.itemsize * np.array([scene_w, 1, scene_w, 1])
# print(strides)
new_h = scene_h - (template_h - 1)
new_w = scene_w - (template_w - 1)
win_wrap_img = np.lib.stride_tricks.as_strided(scene, shape=(new_h, new_w, template_h, template_w),
strides=strides)
print(win_wrap_img.shape)
corr_result = np.zeros((new_h, new_w))
for h in range(new_h):
for w in range(new_w):
corr_result[h][w] += corr(template, win_wrap_img[h][w])
# show_gray_img(corr_result)
plot_3d(np.arange(new_w), np.arange(new_h), corr_result, "ncc corr result")
max_local = argmax_2d(corr_result)
return [*max_local, *template.shape]
def hausdorff_dis(frame1, frame2):
frame1_2 = np.min(
(
abs(frame1.reshape([-1, 1]).repeat(frame2.shape[0], 1) - frame2)
).reshape([len(frame1.flatten()), -1]), axis=-1
)
frame2_1 = np.min(
(
abs(frame2.reshape([-1, 1]).repeat(frame1.shape[0], 1) - frame1)
).reshape([len(frame2.flatten()), -1]), axis=-1
)
return np.max((np.max(frame1_2), np.max(frame2_1)))
def hausdorff(template, scene):
template = cv2.Canny(template, 150, 250)
scene = cv2.Canny(scene, 150, 250)
show_gray_img(scene)
show_gray_img(template)
stride = 3
template_h, template_w = template.shape
scene_h, scene_w = scene.shape
strides = scene.itemsize * np.array([scene_w * stride, 1 * stride, scene_w, 1])
new_h = (scene_h - template_h) // stride + 1
new_w = (scene_w - template_w) // stride + 1
win_wrap_img = np.lib.stride_tricks.as_strided(scene, shape=(new_h, new_w, template_h, template_w),
strides=strides)
temp_x, temp_y = np.where(template > 0)
dis_result = np.zeros((new_h, new_w))
print(win_wrap_img.shape)
for h in range(new_h):
for w in range(new_w):
win_x, win_y = np.where(win_wrap_img[h][w] > 0)
dis_result[h][w] += np.max([hausdorff_dis(temp_x, win_x), hausdorff_dis(temp_y, win_y)])
plot_3d(np.arange(new_w), np.arange(new_h), dis_result, "hausdorff dis result")
min_local = argmin_2d(dis_result)
print(min_local)
return [(min_local[0] - 1) * stride, (min_local[1] - 1) * stride, *template.shape]
def hausdorff_distance_trans(bk_edge):
row, col = bk_edge.shape
min_dis = (row + col) * np.ones((row, col))
a, b = np.where(bk_edge == 255)
for i in range(row):
for j in range(col):
min_dis[i, j] = np.min(abs(a - i) + abs(b - j))
return min_dis
def distance_transform(template, scene):
template = cv2.Canny(template, 150, 250)
scene = cv2.Canny(scene, 150, 250)
show_gray_img(scene)
print(np.max(scene))
scene = hausdorff_distance_trans(scene)
show_gray_img(scene)
stride = 1
template_h, template_w = template.shape
scene_h, scene_w = scene.shape
strides = scene.itemsize * np.array([scene_w * stride, 1 * stride, scene_w, 1])
new_h = (scene_h - template_h) // stride + 1
new_w = (scene_w - template_w) // stride + 1
win_wrap_img = np.lib.stride_tricks.as_strided(scene, shape=(new_h, new_w, template_h, template_w),
strides=strides)
dis_result = np.zeros((new_h, new_w))
temp_x, temp_y = np.where(template > 0)
print(win_wrap_img.shape)
for h in range(new_h):
for w in range(new_w):
# dis_result[h][w] += corr(template, win_wrap_img[h][w])
# win_x, win_y = np.where(win_wrap_img[h][w] > 0)
# dis_result[h][w] += np.max([hausdorff_dis(temp_x, win_x), hausdorff_dis(temp_y, win_y)])
dis_result[h][w] += np.mean(template * win_wrap_img[h][w])
plot_3d(np.arange(new_w), np.arange(new_h), dis_result, "distance_transform corr result")
min_local = argmin_2d(dis_result)
# print(min_local)
return [(min_local[0] - 1) * stride, (min_local[1] - 1) * stride, *template.shape]
def draw_rectangle(locals, scene, title=""):
plt.imshow(scene, cmap='gray')
plt.title(title)
ax = plt.gca()
# 默认框的颜色是黑色,第一个参数是左上角的点坐标
# 第二个参数是宽,第三个参数是长
names = ["Template_1.jpg", "Template_2.jpg"]
for indx, local in enumerate(locals):
ax.add_patch(plt.Rectangle((local[1], local[0]), local[2], local[3], color="blue", fill=False, linewidth=1))
ax.text(local[1], local[0], names[indx], bbox={'facecolor': 'blue', 'alpha': 0.5})
plt.title(title)
plt.show()
def find_position(templates, scene, function, title):
scene_r = scene.shape[0]
locals = []
for template in templates:
locals.append(function(template, scene))
print(locals)
print("左下角为(0,0)情况下,模版1和2目标框的左上角坐标分别为:\n", [[scene_r - item[0], item[1]] for item in locals])
draw_rectangle(locals, scene, title=title)
if __name__ == '__main__':
img_home = r"./data/template_matching/"
template_test_1 = load_img(os.path.join(img_home, "Template_1.jpg"))
template_test_2 = load_img(os.path.join(img_home, "Template_2.jpg"))
scene_test = load_img(os.path.join(img_home, "Scene.jpg"))
start = time.time()
find_position([template_test_1, template_test_2], scene_test, ncc, "ncc")
print("ncc耗时:", time.time() - start)
start = time.time()
find_position([template_test_1, template_test_2], scene_test, hausdorff, "hausdorff")
print("hausdorff耗时:", time.time() - start)
start = time.time()
find_position([template_test_1, template_test_2], scene_test, distance_transform, "distance_transform")
print("distance_transform+hausdorff耗时:", time.time() - start)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
计算机视觉作业基于直方图的自适应阈值分割、聚类实现纹理图像分割、模板匹配、目标跟踪、背景建模、目标检测python源码.zip 计算机视觉作业 基于直方图的自适应阈值分割、利用聚类技术实现纹理图像分割、模板匹配技术、目标跟踪、背景建模、目标检测 计算机视觉作业 基于直方图的自适应阈值分割、利用聚类技术实现纹理图像分割、模板匹配技术、目标跟踪、背景建模、目标检测 计算机视觉作业 基于直方图的自适应阈值分割、利用聚类技术实现纹理图像分割、模板匹配技术、目标跟踪、背景建模、目标检测
资源推荐
资源详情
资源评论
收起资源包目录
计算机视觉作业基于直方图的自适应阈值分割、聚类实现纹理图像分割、模板匹配、目标跟踪、背景建模、目标检测python源码.zip (2000个子文件)
81.bmp 186KB
126.bmp 165KB
106.bmp 158KB
38.bmp 155KB
35.bmp 149KB
82.bmp 140KB
150.bmp 133KB
57.bmp 130KB
79.bmp 129KB
156.bmp 128KB
7.bmp 123KB
21.bmp 122KB
34.bmp 119KB
112.bmp 118KB
14.bmp 115KB
151.bmp 115KB
11.bmp 114KB
90.bmp 113KB
134.bmp 113KB
36.bmp 112KB
1.bmp 111KB
74.bmp 110KB
8.bmp 109KB
161.bmp 106KB
6.bmp 102KB
158.bmp 101KB
168.bmp 99KB
136.bmp 98KB
143.bmp 95KB
102.bmp 94KB
138.bmp 94KB
86.bmp 93KB
101.bmp 92KB
17.bmp 90KB
13.bmp 88KB
127.bmp 88KB
2.bmp 88KB
19.bmp 88KB
58.bmp 87KB
169.bmp 86KB
37.bmp 85KB
100.bmp 84KB
132.bmp 84KB
car046.bmp 84KB
car002.bmp 84KB
car099.bmp 84KB
car069.bmp 84KB
car062.bmp 84KB
car001.bmp 84KB
car057.bmp 84KB
car070.bmp 84KB
car072.bmp 84KB
car077.bmp 84KB
car076.bmp 84KB
car029.bmp 84KB
car011.bmp 84KB
car091.bmp 84KB
car035.bmp 84KB
car100.bmp 84KB
car003.bmp 84KB
car075.bmp 84KB
car036.bmp 84KB
car064.bmp 84KB
car065.bmp 84KB
car044.bmp 84KB
car090.bmp 84KB
car031.bmp 84KB
car087.bmp 84KB
car009.bmp 84KB
car051.bmp 84KB
car073.bmp 84KB
car086.bmp 84KB
car066.bmp 84KB
car068.bmp 84KB
car038.bmp 84KB
car084.bmp 84KB
car010.bmp 84KB
car094.bmp 84KB
car018.bmp 84KB
car033.bmp 84KB
car007.bmp 84KB
car083.bmp 84KB
car095.bmp 84KB
car059.bmp 84KB
car089.bmp 84KB
car056.bmp 84KB
car050.bmp 84KB
car032.bmp 84KB
car042.bmp 84KB
car039.bmp 84KB
car023.bmp 84KB
car079.bmp 84KB
car088.bmp 84KB
car006.bmp 84KB
car034.bmp 84KB
car024.bmp 84KB
car043.bmp 84KB
car026.bmp 84KB
car012.bmp 84KB
car092.bmp 84KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- 一帆风顺7122023-12-08发现一个超赞的资源,赶紧学习起来,大家一起进步,支持!z同学的编程之路2023-12-11不客气,您对资源的认可、支持与好评,将是我后续持续创作及分享的动力源泉,互相学习进步。
- m0_741663112023-11-17发现一个宝藏资源,资源有很高的参考价值,赶紧学起来~z同学的编程之路2023-12-11加油啊啊
- 2301_772263552023-10-18资源很实用,内容详细,值得借鉴的内容很多,感谢分享。z同学的编程之路2023-12-11感谢支持和好评
z同学的编程之路
- 粉丝: 1805
- 资源: 2129
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功