关于地板的木纹和图库中的匹配的。
手机拍照的照片,和图片库中的图片,比对,找到图库中相似的图片。
这个提问只是要联系到可以接项目,或者付费技术支持的人。
我在网上找技术方案,效果都不好。
下面是我的实现的代码:效果不好。
import cv2
import numpy as np
import os
import glob
def split_image_into_nine(img):
h, w = img.shape[:2]
h_step, w_step = h // 3, w // 3
parts = []
for i in range(3):
for j in range(3):
part = img[i * h_step:(i + 1) * h_step, j * w_step:(j + 1) * w_step]
parts.append(part)
return parts
def template_matching(template, search_image):
# 转为灰度图
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
search_gray = cv2.cvtColor(search_image, cv2.COLOR_BGR2GRAY)
res = cv2.matchTemplate(search_gray, template_gray, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
return max_val # 返回匹配的最高分数
def process_images(dirA, dirB, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
imagesA = glob.glob(os.path.join(dirA, '*.*'))
imagesB = glob.glob(os.path.join(dirB, '*.*'))
for imgA_path in imagesA:
imgA = cv2.imread(imgA_path)
if imgA is None:
print(f"无法读取图片: {imgA_path}")
continue
parts = split_image_into_nine(imgA)
max_values = []
for idx, part in enumerate(parts):
current_max = -1 # 初始化为一个极小值
for imgB_path in imagesB:
imgB = cv2.imread(imgB_path)
if imgB is None:
print(f"无法读取图片: {imgB_path}")
continue
# 需要确保模板不大于搜索图片
if part.shape[0] > imgB.shape[0] or part.shape[1] > imgB.shape[1]:
continue
match_val = template_matching(part, imgB)
if match_val > current_max:
current_max = match_val
max_values.append(current_max)
print(f"图片 {os.path.basename(imgA_path)} 第 {idx + 1} 部分的最大匹配值: {current_max}")
# 根据需求,您可以对max_values进行进一步处理,如保存为文件
# 例如,将结果保存为txt文件
result_path = os.path.join(output_dir, f"{os.path.splitext(os.path.basename(imgA_path))[0]}_result.txt")
with open(result_path, 'w') as f:
for idx, val in enumerate(max_values):
f.write(f"部分 {idx + 1}: {val}\n")
print(f"处理完成,结果保存在: {result_path}")
if __name__ == "__main__":
dirA = r'D:\pic\A' # 替换为实际路径
dirB = r'D:\pic\B' # 替换为实际路径
output_dir = r'D:\pic\AB' # 替换为实际路径
process_images(dirA, dirB, output_dir)