立方体边缘检测

huangapple go评论127阅读模式
英文:

Cube Edge detection

问题

我试图使用Scikit-Image库中的Hough变换获取下面图像中立方体的边缘。

更新
这是我正在使用的代码:

  1. smoothed_image = filters.frangi(gray_image)
  2. # 使用Canny边缘检测器进行边缘检测
  3. 低阈值 = 0.1
  4. 高阈值 = 3
  5. 低阈值 = * smoothed_image.max()
  6. 高阈值 = * 低阈值
  7. edges = canny(smoothed_image, sigma, low_threshold=低阈值,
  8. high_threshold=高阈值)
  9. # 使用Hough变换检测丝状物轮廓
  10. hspace, theta, dist = hough_line(edges)
  11. # 找到垂直线
  12. vertical_peaks = hough_line_peaks(hspace, theta, dist, num_peaks=2)
  13. vertical_lines = []
  14. for _, angle, dist in zip(*vertical_peaks):
  15. x = dist * np.cos(angle)
  16. y = dist * np.sin(angle)
  17. vertical_lines.append((x, y))
  18. # 可视化结果
  19. fig, axes = plt.subplots(1, 3, figsize=(15, 5))
  20. axes[0].imshow(smoothed_image)
  21. axes[0].set_title('平滑后的图像')
  22. #axes[0].axis('off')
  23. axes[1].imshow(edges, cmap='gray')
  24. axes[1].set_title('Canny边缘检测')
  25. axes[1].axis('off')
  26. axes[2].imshow(edges, cmap='gray')
  27. for x, y in vertical_lines:
  28. axes[2].axvline(x=x, color='red')
  29. axes[2].set_xlim((0, image.shape[1]))
  30. axes[2].set_ylim((image.shape[0], 0))
  31. axes[2].set_title('检测到的线条')
  32. axes[2].axis('off')
  33. plt.tight_layout()
  34. plt.show()

这是生成的输出:
立方体边缘检测

有关如何使Hough变换检测到另一条边的任何想法吗?

英文:

I am trying to get the edges of a cube in the image below using the Hough transform in the Scikit-Image library 立方体边缘检测.

Update
Here is the code I am working with:

  1. smoothed_image = filters.frangi(gray_image)
  2. # Perform edge detection using the Canny edge detector
  3. low_threshold = 0.1
  4. high_threshold = 3
  5. low_threshold = low * smoothed_image.max()
  6. high_threshold = high * low_threshold
  7. edges = canny(smoothed_image, sigma, low_threshold=low_threshold,
  8. high_threshold=high_threshold)
  9. # Hough transform to detect the filament profile
  10. hspace, theta, dist = hough_line(edges)
  11. # Find the vertical lines
  12. vertical_peaks = hough_line_peaks(hspace, theta, dist, num_peaks=2)
  13. vertical_lines = []
  14. for _, angle, dist in zip(*vertical_peaks):
  15. x = dist * np.cos(angle)
  16. y = dist * np.sin(angle)
  17. vertical_lines.append((x, y))
  18. # Visualize the results
  19. fig, axes = plt.subplots(1, 3, figsize=(15, 5))
  20. axes[0].imshow(smoothed_image)
  21. axes[0].set_title('Smoothed Image')
  22. #axes[0].axis('off')
  23. axes[1].imshow(edges, cmap='gray')
  24. axes[1].set_title('Canny Edge Detection')
  25. axes[1].axis('off')
  26. axes[2].imshow(edges, cmap='gray')
  27. for x, y in vertical_lines:
  28. axes[2].axvline(x=x, color='red')
  29. axes[2].set_xlim((0, image.shape[1]))
  30. axes[2].set_ylim((image.shape[0], 0))
  31. axes[2].set_title('Detected Lines')
  32. axes[2].axis('off')
  33. plt.tight_layout()
  34. plt.show()

And this is the resulting output
立方体边缘检测

Any ideas on how to make the Hough transform detect the other edge?

答案1

得分: 0

如@Pete建议的,"遮住管子",因为它挡住了立方体,并调整以下参数:

  1. Canny 参数
  2. 高斯滤波器 参数
  3. 霍夫直线 检测器参数

这样你就可以检测到另一条边。

P.S:我使用了 OpenCV,但你也可以使用 Scikit Image 重复这些结果。

  1. #!/usr/bin/env python3
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. im_path = "cube.png"
  6. img = cv2.imread(im_path)
  7. # 将图像转换为灰度
  8. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  9. # 高斯滤波
  10. k = 5
  11. sigma = 0
  12. gauss = cv2.GaussianBlur(gray, (k, k), sigma)
  13. # 在图像上应用边缘检测方法
  14. edges = cv2.Canny(gauss, 50, 150, apertureSize=3)
  15. # 移除不需要的管子
  16. # https://stackoverflow.com/a/64175996/8618242
  17. sum_ = np.sum(edges, axis=1)
  18. mean_ = np.mean(sum_)
  19. edges[sum_ > mean_, :] = 0
  20. # 这将返回一个r和theta值的数组
  21. lines = cv2.HoughLines(edges, 1, np.pi/180, 150)
  22. # 以下for循环在r和theta值在2D数组的范围内运行
  23. for r_theta in lines:
  24. arr = np.array(r_theta[0], dtype=np.float64)
  25. r, theta = arr
  26. # 在a中存储cos(theta)的值
  27. a = np.cos(theta)
  28. # 在b中存储sin(theta)的值
  29. b = np.sin(theta)
  30. # x0存储rcos(theta)的值
  31. x0 = a*r
  32. # y0存储rsin(theta)的值
  33. y0 = b*r
  34. # x1存储(rcos(theta)-1000sin(theta))的四舍五入值
  35. x1 = int(x0 + 1000*(-b))
  36. # y1存储(rsin(theta)+1000cos(theta))的四舍五入值
  37. y1 = int(y0 + 1000*(a))
  38. # x2存储(rcos(theta)+1000sin(theta))的四舍五入值
  39. x2 = int(x0 - 1000*(-b))
  40. # y2存储(rsin(theta)-1000cos(theta))的四舍五入值
  41. y2 = int(y0 - 1000*(a))
  42. # cv2.line在图像中从点(x1, y1)到(x2, y2)绘制一条线。
  43. # (0,0,255)表示要绘制的线的颜色。在这种情况下,是红色。
  44. cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
  45. cv2.namedWindow("output", cv2.WINDOW_NORMAL)
  46. cv2.imshow("output", img)
  47. cv2.waitKey(0)

立方体边缘检测

英文:

As suggested by @Pete "Mask out the tube" as it is occluding the cube, and adjust the parameters:

  1. Canny parameters
  2. Gaussian Filter parameters
  3. Hough Line detector parameters

so you can detect the other edge.

> P.S: I used OpenCV, but you might repeat the results using Scikit Image

  1. #!/usr/bin/env python3
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. im_path = "cube.png"
  6. img = cv2.imread(im_path)
  7. # Convert the img to grayscale
  8. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  9. # Gaussian Filter
  10. k = 5
  11. sigma = 0
  12. gauss = cv2.GaussianBlur(gray,(k,k),sigma)
  13. # Apply edge detection method on the image
  14. edges = cv2.Canny(gauss, 50, 150, apertureSize=3)
  15. # Remove unwanted tube
  16. # https://stackoverflow.com/a/64175996/8618242
  17. sum_ = np.sum(edges, axis=1)
  18. mean_ = np.mean(sum_)
  19. edges[sum_> mean_, :] = 0
  20. # This returns an array of r and theta values
  21. lines = cv2.HoughLines(edges, 1, np.pi/180, 150)
  22. # The below for loop runs till r and theta values
  23. # are in the range of the 2d array
  24. for r_theta in lines:
  25. arr = np.array(r_theta[0], dtype=np.float64)
  26. r, theta = arr
  27. # Stores the value of cos(theta) in a
  28. a = np.cos(theta)
  29. # Stores the value of sin(theta) in b
  30. b = np.sin(theta)
  31. # x0 stores the value rcos(theta)
  32. x0 = a*r
  33. # y0 stores the value rsin(theta)
  34. y0 = b*r
  35. # x1 stores the rounded off value of (rcos(theta)-1000sin(theta))
  36. x1 = int(x0 + 1000*(-b))
  37. # y1 stores the rounded off value of (rsin(theta)+1000cos(theta))
  38. y1 = int(y0 + 1000*(a))
  39. # x2 stores the rounded off value of (rcos(theta)+1000sin(theta))
  40. x2 = int(x0 - 1000*(-b))
  41. # y2 stores the rounded off value of (rsin(theta)-1000cos(theta))
  42. y2 = int(y0 - 1000*(a))
  43. # cv2.line draws a line in img from the point(x1,y1) to (x2,y2).
  44. # (0,0,255) denotes the colour of the line to be
  45. # drawn. In this case, it is red.
  46. cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
  47. cv2.namedWindow("output", cv2.WINDOW_NORMAL)
  48. cv2.imshow("output", img)
  49. cv2.waitKey(0)

立方体边缘检测

huangapple
  • 本文由 发表于 2023年7月17日 23:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定