当前位置:   article > 正文

滑块缺口识别_c# 图片滑块识别

c# 图片滑块识别

带缺口的图片长这样:

缺口图片:

 识别过程:

  1. # -*- coding: utf-8 -*-
  2. import cv2
  3. from matplotlib import pyplot as plt
  4. # 带有缺口的图片
  5. path1 = r"jietu_1.png"
  6. # 模板图片
  7. path2 = r"jietu_2.png"
  8. def pic2gray(pic_path, new_path=''):
  9. # 转灰度图
  10. pic_path_rgb = cv2.imread(pic_path)
  11. pic_path_gray = cv2.cvtColor(pic_path_rgb, cv2.COLOR_BGR2GRAY)
  12. if new_path:
  13. cv2.imwrite(new_path, pic_path_gray)
  14. return pic_path_gray
  15. def canny_edge(image_array, new_path=''):
  16. # 灰度图锐化
  17. can = cv2.Canny(image_array, threshold1=200, threshold2=300)
  18. if new_path:
  19. cv2.imwrite(new_path, can)
  20. return can
  21. # 获取灰度图
  22. gray_path = r"jietu_1_gray.png"
  23. image_gray1 = pic2gray(path1, gray_path)
  24. gray_path = r"jietu_2_gray.png"
  25. image_gray2 = pic2gray(path2, gray_path)
  26. # 获取锐化后的图片,当实际图片内容复杂时,背景图片和缺口图片的锐化参数应该是不同的
  27. can_path1 = r"jietu_1_can.png"
  28. canny_edge(image_gray1, can_path1)
  29. can_path2 = r"jietu_2_can.png"
  30. canny_edge(image_gray2, can_path2)
  31. img = cv2.imread(can_path1)
  32. template = cv2.imread(can_path2)
  33. # 实行缺口匹配算法
  34. res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF)
  35. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
  36. print(min_val, max_val, min_loc, max_loc)
  37. h, w = template.shape[:2]
  38. # h1, w1 = img.shape[:2]
  39. # print(h1, w1)
  40. left_top = max_loc # 左上角
  41. right_bottom = (left_top[0] + w, left_top[1] + h) # 右下角
  42. cv2.rectangle(img, left_top, right_bottom, 255, 2) # 画出矩形位置
  43. plt.subplot(121), plt.imshow(res, cmap='gray')
  44. plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
  45. plt.subplot(122), plt.imshow(img, cmap='gray')
  46. plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
  47. plt.show()

 运行结果:

可以知道确实找到缺口位置了,done。

参考:

  1. https://zhuanlan.zhihu.com/p/115317146
  2. https://www.cnblogs.com/gezhuangzhuang/p/10724769.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/287158
推荐阅读
相关标签
  

闽ICP备14008679号