如何使用OpenCV Python检测椭圆并去除图像中的异常值

3

我想要提取构成椭圆的点,然后画出椭圆。但由于一些可以被认为是离群值的点,我得到了一个无效的椭圆掩码,就像这样:

我要从这个图片中提取椭圆 当我尝试连接这些点绘制椭圆时,我得到了如下结果 这是最终的掩码,形式无效

这是我正在执行的代码,但它总是选择异常值

`cv2.rectangle(cleanedpartiallyimage, (0, 0), (1200, 10), (0, 0, 0), -1)

cv2.rectangle(cleanedpartiallyimage, (0, 0), (47, 1200), (0, 0, 0), -1)

image = cv2.cvtColor(cleanedpartiallyimage, cv2.COLOR_BGR2HSV) lower = np.array([85, 0, 20], dtype="uint8")
 upper = np.array([95, 255, 255], dtype="uint8") mygray = cv2.inRange(image, lower, upper)

#--- Gaussian and Canny filters to make it easy to get the contours

blurred = cv2.GaussianBlur(mygray, (5, 5), 0) imageCanny = cv2.Canny(blurred, 0, 100, 0)

ret,th = cv2.threshold(imageCanny,127,255, 0)

#--- Find all the contours in the binary image --- 
contours,hierarchy = cv2.findContours(th,3,1) 
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

final = cv2.drawContours(imageCanny, big_contour, -1, (0,255,0), 3)

actualcontours, hierarchy = cv2.findContours(final, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

#---Removing side contour points

actualcontours = getactualcontours(actualcontours, 60)

empty = np.zeros((image.shape[0], image.shape[1], 3), np.uint8)

#---Removes linear contour points

ConvexHullPoints = contoursConvexHull(actualcontours)

#---Converts the points to Ellipse using fitEllipse

test41 = cv2.polylines(empty, [ConvexHullPoints], True, (255, 255, 255), 3) 
imageCannyee = cv2.Canny(test41, 0, 100, 0) 
contours, hierarchy = cv2.findContours(imageCannyee, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) 
for cont in contours: 
   if len(cont) < 20: 
    break 
   elps = cv2.fitEllipse(cont) 
anotherempty = np.zeros((image.shape[0], image.shape[1], 3), np.uint8)
#---Drawing the ellipse into the empty mask
cv2.ellipse(anotherempty, elps, (255, 255, 255), 2) plt.imshow(anotherempty)

请查看此链接中有关OpenCV Python中椭圆检测的答案。 - Eumel
@Eumel很遗憾,它没有起作用,图像中仍然存在异常值。 - ELOUTMADI
1个回答

1
这里是一个简单的方法:
  1. 获取二进制图像。 我们 加载图像,转换为灰度高斯模糊,然后使用Otsu阈值来获取二进制图像。

  2. 膨胀形成单个轮廓。 接下来,我们使用cv2.getStructuringElementcv2.MORPH_ELLIPSE参数创建椭圆形内核,并膨胀以将小的单个轮廓合并成一个大轮廓。

  3. 识别椭圆。 接下来,我们查找轮廓,使用轮廓面积进行过滤,然后使用cv2.fitEllipse()检测椭圆。


enter image description here

import cv2

# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Dilate with elliptical shaped kernel
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
dilate = cv2.dilate(thresh, kernel, iterations=2)

# Find contours, filter using contour threshold area, draw ellipse
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area > 5000:
        ellipse = cv2.fitEllipse(c)
        cv2.ellipse(image, ellipse, (36,255,12), 2)

cv2.imshow('thresh', thresh)
cv2.imshow('dilate', dilate)
cv2.imshow('image', image)
cv2.waitKey()

非常感谢,我很欣赏你的解决方案。我已经测试过了,它可以正常工作,但是区域从一个图像变成了另一个图像,这是我仍然存在的问题,最大区域适用于某些情况,而其他情况则不适用。因此,我必须找到一种方法来在最大区域和最大区域之前进行切换。 - ELOUTMADI

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接