【问题标题】:How to detect edge points of an object using OpenCV Python?如何使用 OpenCV Python 检测对象的边缘点?
【发布时间】:2019-02-08 09:40:51
【问题描述】:

所以,我已经检测到了一个物体的所有边缘,但问题是我找不到每个边缘的两个点,即起点和终点及其坐标。

实际上我正在尝试找到一个对象的测量值,但我遇到了这个问题。The image is regarding the ROI of the image.

import cv2
import numpy as np
from matplotlib import pyplot as plt 

#Read Image of the Object
img = cv2.imread("C:\\Users\\Desktop\\Project\\captured.jpg")
cv2.imshow('Original Image', img)
cv2.waitKey(0)



#Convert Image To GrayScale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray', gray)
cv2.waitKey(0)


#Binary Thresholding
ret, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('Binary Image', thresh)
cv2.waitKey(0)

#Crop Image
cropped = thresh[150:640, 150:500]
cv2.imshow('Cropped Image', cropped)
cv2.waitKey(0)

#Edge Detection
edges = cv2.Canny(cropped, 100, 200)
cv2.imshow('Edges', edges)
cv2.waitKey(0)

#find contours
ctrs, hier = cv2.findContours(cropped, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

#Sort Contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0] + cv2.boundingRect(ctr)[1] * cropped.shape[1])


#ROI
for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = cropped[y:y + h, x:x + w]
    # show ROI
    # cv2.imshow('segment no:'+str(i),roi)
    cv2.rectangle(cropped , (x, y), (x + w, y + h), (150, 0, 255), 2)
cv2.imshow('marked areas', cropped)
cv2.waitKey(0)

Original Image

These are 5 points and the five edges that I need with coordinates so I can calculate the distance between them for the measurement.

Harris Corner Output.

【问题讨论】:

  • 您能否将示例输入图像与预期输出一起附加?
  • 我已通过更改编辑了问题。现在可以看了吗?
  • @JimitVaghela hii 我已经计算了图像边长。现在,我想知道如何知道如何像imgur.com/a/e2CVGxA 一样表示它,知道如何实现这一目标

标签: python opencv oop


【解决方案1】:

尝试改用Harris Corner Detection

import cv2
import numpy as np

def find_centroids(dst):
    ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
    dst = np.uint8(dst)

    # find centroids
    ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
    # define the criteria to stop and refine the corners
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 
                0.001)
    corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5), 
              (-1,-1),criteria)
    return corners

image = cv2.imread("corner.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)

dst = cv2.cornerHarris(gray, 2, 3, 0.04)

dst = cv2.dilate(dst, None)

# Threshold for an optimal value, it may vary depending on the image.
# image[dst > 0.01*dst.max()] = [0, 0, 255]

# Get coordinates
corners= find_centroids(dst)
# To draw the corners
for corner in corners:
    image[int(corner[1]), int(corner[0])] = [0, 0, 255]
cv2.imshow('dst', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

您可能需要微调cornerHarris 的参数。

【讨论】:

  • 感谢您的回复。我真的可以将这些点值存储到一个变量中,以便我可以使用它们来计算它们之间的距离吗?
  • 是的,你可以。刚刚编辑了答案。它们只是对找到的角的近似估计,因为它在同一个角找到多个角
  • 另外,您提供的建议给了我一个错误:IndexError: boolean index did not match indexed array along dimension 0;尺寸为 330,但对应的布尔尺寸为 480
  • 我已经尝试了代码并且它有效。你遇到了什么错误?
  • 在第 32 行:NameError: name 'corners' is not defined 我有这个错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
相关资源
最近更新 更多