为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

人到中年有点甜 2021-11-02 16:45:03
所以,我已经检测到一个对象的所有边缘,但问题是我找不到每个边缘的两个点,即起点和终点及其坐标。实际上,我正在尝试找到对象的测量值,但我被困在这个问题上。import cv2import numpy as npfrom matplotlib import pyplot as plt #Read Image of the Objectimg = cv2.imread("C:\\Users\\Desktop\\Project\\captured.jpg")cv2.imshow('Original Image', img)cv2.waitKey(0)#Convert Image To GrayScalegray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)cv2.imshow('Gray', gray)cv2.waitKey(0)#Binary Thresholdingret, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)cv2.imshow('Binary Image', thresh)cv2.waitKey(0)#Crop Imagecropped = thresh[150:640, 150:500]cv2.imshow('Cropped Image', cropped)cv2.waitKey(0)#Edge Detectionedges = cv2.Canny(cropped, 100, 200)cv2.imshow('Edges', edges)cv2.waitKey(0)#find contoursctrs, hier = cv2.findContours(cropped, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)#Sort Contourssorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0] + cv2.boundingRect(ctr)[1] * cropped.shape[1])#ROIfor 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)这些是我需要的 5 个点和 5 个边,它们带有坐标,因此我可以计算它们之间的距离以进行测量。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 233 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号