0


对圆和椭圆进行边缘检测

霍夫梯度:检测的圆与原始图像具有相同的大小

检测到的相邻圆的中心的最小距离(如果参数太小,除了一个真实的圆外,还可能会错误地检测到多个相邻圆。如果太大,可能会漏掉一些圆。)

在#HOUGH梯度的情况下,它是较高的. 两个阈值传递到Canny边缘检测器(较低的一个小两倍)。

在#HOUGH梯度的情况下,它是检测阶段圆心的累加器阈值。它越小,就越可能检测到假圆;minRadius:最小圆半径maxRadius:最大圆半径,如果<=0,则使用最大图像尺寸。如果<0,则返回没有找到半径的中心。

PS:在opencv中经常使用cv2.findContours()函数来查找检测物体的轮廓

  1. """
  2. -*- coding: utf-8 -*-
  3. author: Hao Hu
  4. @date 2021/12/3 8:00 AM
  5. """
  6. import math
  7. import cv2
  8. import numpy as np
  9. import matplotlib.pyplot as plt
  10. def circular_detect():
  11. """霍夫变换圆检测"""
  12. import cv2
  13. # 载入并显示图片
  14. img = cv2.imread('./circular.png')
  15. # 灰度化
  16. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  17. # 输出图像大小,方便根据图像大小调节minRadius和maxRadius
  18. print(img.shape)
  19. ret, thresh1 = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
  20. cv2.imshow('thresh1', thresh1)
  21. canny = cv2.Canny(thresh1, 40, 80)
  22. cv2.imshow('Canny', canny)
  23. canny = cv2.blur(canny, (3, 3))
  24. cv2.imshow('blur', canny)
  25. # 霍夫变换圆检测
  26. circles = cv2.HoughCircles(canny, cv2.HOUGH_GRADIENT, 1, 100, param1=50, param2=30, minRadius=30, maxRadius=150)
  27. # 输出返回值,方便查看类型
  28. print('定义了一个三维数组(x,y,r)',circles)
  29. # 输出检测到圆的个数
  30. print(len(circles[0]))
  31. # 根据检测到圆的信息,画出每一个圆
  32. for circle in circles[0]:
  33. if (circle[2] >= 100):
  34. continue
  35. # 圆的基本信息
  36. print('半径为',circle[2])
  37. # 坐标行列
  38. x = int(circle[0])
  39. y = int(circle[1])
  40. # 半径
  41. r = int(circle[2])
  42. # 在原图用指定颜色标记出圆的位置
  43. img = cv2.circle(img, (x, y), r, (0, 0, 255), -1)
  44. # 显示新图像
  45. cv2.imshow('circular_detection', img)
  46. def Ellipse_feature_extraction2():
  47. img = cv2.imread("ellipse.png", 3)
  48. imgray = cv2.Canny(img, 600, 100, 3) # Canny边缘检测,参数可更改
  49. ret, thresh = cv2.threshold(imgray, 127, 255, cv2.THRESH_BINARY)
  50. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # contours为轮廓集,可以计算轮廓的长度、面积等
  51. for cnt in contours:
  52. if len(cnt) > 50:
  53. S1 = cv2.contourArea(cnt)
  54. ell = cv2.fitEllipse(cnt)
  55. S2 = math.pi * ell[1][0] * ell[1][1]
  56. if (S1 / S2) > 0.2: # 面积比例,可以更改,根据数据集。。。
  57. img = cv2.ellipse(img, ell, (0, 255, 0), 2)
  58. #print(str(S1) + " " + str(S2) + " " + str(ell[0][0]) + " " + str(ell[0][1]))
  59. print(contours[0][0][0],contours[-1][-1][-1])
  60. # 这是椭圆相隔最远的点
  61. x = (contours[0][0][0][0] + contours[-1][-1][-1][0])/2
  62. y = (contours[0][0][0][1] + contours[-1][-1][-1][1])/2
  63. print('椭圆圆心',x,y)
  64. r = math.sqrt((x-contours[0][0][0][0])*(x-contours[0][0][0][0])+(y-contours[0][0][0][1])*(y-contours[0][0][0][1]))
  65. print('椭圆半径',r)
  66. cv2.imshow("0", img)
  67. if __name__ == "__main__":
  68. # line_detect_possible_demo()
  69. # circular_detect()
  70. Ellipse_feature_extraction2()
  71. cv2.waitKey(0)
  72. cv2.destroyAllWindows()

本文转载自: https://blog.csdn.net/weixin_48262500/article/details/122139292
版权归原作者 这个利弗莫尔不太冷 所有, 如有侵权,请联系我们删除。

“对圆和椭圆进行边缘检测”的评论:

还没有评论