我有一个车轮的图像,我想用OpenCV(用于估计车辆/车轮的姿态)检测映射到该车轮(或车轮的内圆)的椭圆。
[![在此处输入图片描述] [1]] [1]
有谁知道如何做到这一点?
[![在此处输入图片描述] [2]] [2]
生成的图像应该看起来像这样(或有点像这样-我需要检索经过椭圆形的一些点)。请注意,结果应该是椭圆形,而不是圆形,因为如果从侧面看,则轮子可能处于透视/扭曲状态。
到目前为止,我已经对Contour(并确定了最大的轮廓区域),canny和颜色阈值进行了实验,我能够在大多数测试图像上识别出轮的内椭圆(成功!)。这个“内部椭圆”是最大轮廓的一部分,但是它也包含我不需要的其他点。
见图片:
[![在此处输入图片描述] [3]] [3]
最后一个问题是,如何得出这个内圆/椭圆的几个点(并摆脱其他垃圾),以便我可以计算椭圆参数?
任何帮助,将不胜感激。
非常感谢!
编辑:
到目前为止,这是我的代码:
import sys
import numpy as np
import skimage.io
import skimage.filters
import matplotlib.pyplot as plt
import cv2
from skimage import data, color, img_as_ubyte
from skimage.feature import canny
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter
# get filename and sigma value from command line
filename0 = 'back_wheel.png'
filename1 = "backwheel1.png"
filename2 = "backwheel2.png"
filename3 = "backwheel3.png"
filename4 = "frontwheel1.png"
filename5 = "frontwheel2.png"
def getWheel0(filename):
image_rgb_orig = cv2.imread(filename)
image_rgb = image_rgb_orig.copy()
print('Original Dimensions : ',image_rgb.shape)
width = 350
height = (int)(width * image_rgb.shape[0]/image_rgb.shape[1])
dim = (width, height)
image_rgb = cv2.resize(image_rgb, dim, interpolation=cv2.INTER_AREA)
image_gray = cv2.cvtColor(image_rgb, cv2.COLOR_BGR2GRAY)
equ = cv2.equalizeHist(image_gray)
#edges = cv2.Canny(image_gray, 150, 200)
norm_image = cv2.normalize(equ, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
#--- First obtain the threshold using the greyscale image ---
ret,th = cv2.threshold(norm_image, 50, 100, cv2.THRESH_TOZERO_INV)
#--- Find all the contours in the binary image ---
contours,hierarchy = cv2.findContours(th,2,1)
#contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
cnt = contours
big_contour = []
max = 0
for i in cnt:
area = cv2.contourArea(i) #--- find the contour having biggest area ---
if(area > max):
max = area
big_contour = i
result = cv2.drawContours(image_rgb, big_contour, -1, (0,255,0), 3)
fig2, (ax1, ax2, ax3) = plt.subplots(ncols=3, nrows=1, figsize=(8, 4), sharex=True,
sharey=True)
ax1.set_title('Original picture')
ax1.imshow(image_rgb_orig)
ax2.set_title('Threshhold')
ax2.imshow(th)
ax3.set_title('Contour')
ax3.imshow(result)
plt.show()
getWheel0(filename0)
getWheel0(filename1)
getWheel0(filename2)
getWheel0(filename3)
getWheel0(filename4)
getWheel0(filename5)
用它运行
“python tire.py”
最佳答案
在图像上定位圆的问题是众所周知的,并且已经解决。
使用霍夫圆变换。
这是OpenCV lib的基本功能:docs。
广义霍夫变换可用于检测椭圆。但是您需要自己编写代码,因为this question的答案中提供了一些合理的想法。您需要具有更大尺寸(5)的累加器,以进行线(2)或圆(3)检测,但这应该不是问题。
看起来Canny可以在图像上提供良好的边缘。您可以尝试以下操作:
这两种解决方案都可能不适用于具有非对称轴的椭圆形和椭圆形轮廓。
关于opencv - 如何使用OpenCV检测车轮中的椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59107943/