【问题标题】:OpenCV - Detect points along a curveOpenCV - 沿曲线检测点
【发布时间】:2021-01-21 13:47:07
【问题描述】:

我需要检测这些曲线上的点,特别是我需要从左侧开始的两个点之一在图像上的位置:

我试图像这样检测霍夫点:

import cv2
import numpy as np

# detect circles in the image
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)
# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
    # show the output image
    cv2.imshow("output", np.hstack([image, output]))
    cv2.waitKey(0)

但它没有得到正确的点,我想这是因为这些点是沿着两条曲线定位的。有没有办法检测曲线上的点?

【问题讨论】:

  • 如果您知道这两种颜色,则对图像进行颜色阈值处理,并侵蚀生成的蒙版,使点保留,但线消失。在剩余点上找到轮廓。对两种颜色都这样做。对于重叠点的“背景”彩色线(这里:红色),这种方法可能会失败。如果是这样,请使用已从“前景”彩色线(此处:蓝色)获得的一些信息。
  • 一种非常非常简单的起点方法:按列扫描图像,并在所需颜色的第一个像素处停止。基本但防弹。
  • @HansHirse 我跟着你的步骤,它就像一个魅力。谢谢!我用结果发布了答案

标签: python opencv


【解决方案1】:

刚刚在@HansHirse 的评论之后找到了解决方案。我按照以下步骤操作:

  1. 颜色阈值
  2. 侵蚀
  3. 寻找轮廓

这是代码:

import cv2
import numpy as np

hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_image, lowerb=(85, 0, 0), upperb=(95, 255, 255))

# Masking with green
imask = mask > 0
green = np.zeros_like(hsv_image, np.uint8)
green[imask] = hsv_image[imask]

kernel = np.ones((8, 8), np.uint8)
erosion = cv2.erode(green, kernel, iterations=1)

gray = cv2.cvtColor(erosion, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Filter out large non-connecting objects
cnts = cv2.findContours(thresh, 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 < 500:
        cv2.drawContours(thresh,[c],0,0,-1)

# Morph open using elliptical shaped kernel
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)

# Find circles
cnts = cv2.findContours(opening,  cv2.RETR_LIST,
                        cv2.CHAIN_APPROX_SIMPLE)[-2]
for c in cnts:
    area = cv2.contourArea(c)
    if area > 0 and area < 50:
        ((x, y), r) = cv2.minEnclosingCircle(c)
        cv2.circle(image, (int(x), int(y)), int(r), (36, 255, 12), 2)

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

结果是下图,其中的点由绿色圆圈表示

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多