鱼C论坛

 找回密码
 立即注册
查看: 1472|回复: 3

关于学习 使用opencv识别验证码缺口遇到的问题

[复制链接]
发表于 2023-9-3 15:08:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import cv2

  2. GAUSSIAN_BLUR_KERNAL_SIZE = (5, 5)
  3. GAUSSIAN_BLUR_SIGMA_X = 0
  4. GANNY_THERSHOLD1 = 200
  5. GANNY_THERSHOLD2 = 200
  6. import random


  7. def get_gussian_blur_image(image):
  8.     return cv2.GaussianBlur(image, GAUSSIAN_BLUR_KERNAL_SIZE, GAUSSIAN_BLUR_SIGMA_X)


  9. def get_canny_image(image):
  10.     return cv2.Canny(image, GANNY_THERSHOLD1, GANNY_THERSHOLD2)


  11. def get_contours(image):
  12.     contours, _ = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
  13.     return contours


  14. # 以下是显示图像的代码,不做修改

  15. import cv2
  16. import matplotlib.pyplot as plt

  17. # 读取图像
  18. image_raw = cv2.imread('2222.png')

  19. # 获取图像尺寸
  20. image_height, image_width, _ = image_raw.shape

  21. # 高斯模糊处理
  22. image_gaussian_blur = get_gussian_blur_image(image_raw)

  23. # Canny边缘检测
  24. image_canny = get_canny_image(image_gaussian_blur)

  25. # 轮廓查找
  26. contours = get_contours(image_canny)

  27. # 显示原始图像
  28. plt.subplot(2, 2, 1)
  29. plt.imshow(cv2.cvtColor(image_raw, cv2.COLOR_BGR2RGB))
  30. plt.title('Original Image')

  31. # 显示高斯模糊后的图像
  32. plt.subplot(2, 2, 2)
  33. plt.imshow(cv2.cvtColor(image_gaussian_blur, cv2.COLOR_BGR2RGB))
  34. plt.title('Gaussian Blur Image')


  35. def get_contour_area_threshold(image_width, image_height):
  36.     contour_area_min = image_width * image_height * 0.1 * 0.1 * 0.8
  37.     contour_area_max = image_width * image_height * 0.2 * 0.2 * 1.2
  38.     return contour_area_min, contour_area_max


  39. def get_arc_length_threshold(image_width, image_height):
  40.     arc_length_min = ((image_width * 0.1) + (image_height * 0.1)) * 2 * 0.8
  41.     arc_length_max = ((image_width * 0.2) + (image_height * 0.2)) * 2 * 1.2
  42.     return arc_length_min, arc_length_max


  43. def get_offset_threshold(image_width):
  44.     offset_min = 0.3 * image_width
  45.     offset_max = 0.8 * image_width
  46.     return offset_min, offset_max


  47. import random
  48. import matplotlib.pyplot as plt

  49. # 绘制轮廓
  50. for contour in contours:
  51.     color = (random.randint(0, 255) / 255, random.randint(0, 255) / 255, random.randint(0, 255) / 255)
  52.     cv2.drawContours(image_raw, [contour], -1, color, 2)

  53. # 显示Canny边缘检测后的图像和轮廓
  54. plt.subplot(2, 2, 3)
  55. plt.imshow(image_canny, cmap='gray')
  56. plt.title('Canny Image')

  57. plt.subplot(2, 2, 4)
  58. plt.imshow(cv2.cvtColor(image_raw, cv2.COLOR_BGR2RGB))
  59. for contour in contours:
  60.     color = (random.randint(0, 255) / 255, random.randint(0, 255) / 255, random.randint(0, 255) / 255)
  61.     plt.plot(contour[:, 0, 0], contour[:, 0, 1], color=color, linewidth=2)
  62. plt.title('Contours')

  63. # 调整子图布局
  64. plt.tight_layout()

  65. # 保存最后的图片
  66. plt.savefig('contours.png')

  67. # 显示图像
  68. plt.show()

  69. contour_area_min, contour_area_max = get_contour_area_threshold(image_width, image_height)
  70. arc_length_min, arc_length_max = get_arc_length_threshold(image_width, image_height)
  71. offset_min, offset_max = get_offset_threshold(image_width)
  72. offset = None
  73. print(len(contours))
  74. count = 1
  75. for contour in contours:
  76.     x, y, w, h = cv2.boundingRect(contour)
  77.     if offset_min < x < offset_max and arc_length_min < cv2.arcLength(contour,
  78.                                                                       True) < arc_length_max and contour_area_min < cv2.contourArea(
  79.             contour) < contour_area_max:
  80.         cv2.rectangle(image_raw, (x, y), (x + w, y + h), (0, 0, 255), 2)
  81.         offset = x
  82.         print(1)

  83. # 保存带有矩形框的图像
  84. cv2.imwrite('image_label.png', cv2.cvtColor(image_raw, cv2.COLOR_BGR2RGB))
  85. print('offset', offset)
复制代码


原理我大概懂了,实操起来到处是困难,运行后每一个轮廓满足条件,我觉得应该是3个判断阈值函数设置有问题,可是我就是不知道哪里错了,希望论坛的大佬能指点我,万分感谢。代码如上,图片使用的是这个:https{删除}://smms.app/image/N6qywYfZU3Senlz
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-3 15:08:48 | 显示全部楼层
根据您提供的代码,有一些可能导致问题的地方:

1. 高斯模糊参数:
   GAUSSIAN_BLUR_KERNAL_SIZE = (5, 5) 设置了高斯模糊的内核大小。您可以尝试调整这个参数来适应您的验证码图片,如果图片较小,可以减小内核大小。

2. Canny 边缘检测参数:
   GANNY_THERSHOLD1 = 200GANNY_THERSHOLD2 = 200 是用于设置 Canny 边缘检测的阈值参数。您可以尝试调整这两个阈值来获取更好的边缘检测效果。

3. 轮廓查找参数:
   contours, _ = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) 中的 cv2.RETR_CCOMPcv2.CHAIN_APPROX_SIMPLE 分别是轮廓检测的两个参数。您可以尝试使用其他参数组合来获取更准确的轮廓。

此外,您也可以尝试调整其他阈值函数中的参数和阈值范围,以便更好地适应您的验证码图片。希望这些提示对您有帮助!
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-3 15:08:51 | 显示全部楼层
你的代码中有几个问题导致判断条件无法生效:

1.  get_contour_area_threshold 函数中计算面积阈值的公式错误。应该将0.1和0.2乘以0.1和0.2,而不是乘以0.8和1.2。正确的代码如下:

  1. def get_contour_area_threshold(image_width, image_height):
  2.     contour_area_min = image_width * image_height * 0.1 * 0.1 * 0.1
  3.     contour_area_max = image_width * image_height * 0.2 * 0.2 * 0.2
  4.     return contour_area_min, contour_area_max
复制代码


2.  get_arc_length_threshold 函数中计算轮廓周长阈值的公式错误。应该将0.1和0.2分别乘以2,而不是乘以0.8和1.2。正确的代码如下:

  1. def get_arc_length_threshold(image_width, image_height):
  2.     arc_length_min = ((image_width * 0.1) + (image_height * 0.1)) * 2
  3.     arc_length_max = ((image_width * 0.2) + (image_height * 0.2)) * 2
  4.     return arc_length_min, arc_length_max
复制代码


3.  get_offset_threshold 函数中计算偏移阈值的公式错误。应该将0.3和0.8分别乘以 image_width ,而不是乘以1。正确的代码如下:

  1. def get_offset_threshold(image_width):
  2.     offset_min = 0.3 * image_width
  3.     offset_max = 0.8 * image_width
  4.     return offset_min, offset_max
复制代码


修改以上问题后,再次运行代码,判断条件应该可以正确生效了。希望对你有帮助!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-3 18:47:42 | 显示全部楼层
很抱歉,作为AI机器人,我无法查看您的代码或使用图片链接。但是,我可以向您提供一些检查图像轮廓的方法,希望对您有所帮助。

1. 首先,您需要获取图像的轮廓。使用OpenCV函数findContours()可以找到轮廓的起始点和结束点。请确保在调用该函数前将图像转换为二进制形式(可以使用阈值处理等方法)。

2. 检查每个轮廓的面积,可以使用OpenCV函数contourArea()计算得出。如果一个轮廓的面积太小,可能是不需要的噪声或错误的对象。

3. 检查每个轮廓的周长,可以使用OpenCV函数arcLength()计算得出。如果一个轮廓的周长太小,也可能是不需要的噪声或错误的对象。

4. 检查每个轮廓的凸包,在OpenCV中使用convexHull()函数实现。如果一个轮廓不是凸多边形,则可能是需要进一步处理或排除的对象。

5. 检查每个轮廓的形状。可以使用OpenCV函数approxPolyDP()来近似估算轮廓的形状。如果一个轮廓的形状不符合预期,可能需要进一步处理或排除。

6. 如果以上方法无法解决问题,您可以尝试手动打印或可视化每个轮廓的数据,以确定哪个阈值设置不正确或哪个轮廓出现了问题。

希望这些方法可以对您有所帮助。如果您有任何其他问题,请随时提出。
此内容为ChatGPT回答,如果胡说八道,请不要理会
如果对你有帮助,请设置一个最佳答案!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 19:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表