如何使用 OpenCV Python 检测对象的边缘点?

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

实际上,我正在尝试找到对象的测量值,但我被困在这个问题上。

http://img4.mukewang.com/6180faa80001fcc203620369.jpg

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)

http://img1.mukewang.com/6180fab40001477306580527.jpg

这些是我需要的 5 个点和 5 个边,它们带有坐标,因此我可以计算它们之间的距离以进行测量。

http://img1.mukewang.com/6180fabe0001ba2f03770293.jpg

http://img3.mukewang.com/6180fad10001345d03810292.jpg

人到中年有点甜
浏览 234回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python