利用python opencv 提取目标轮廓坐标的代码 
时间: 2023-03-13 22:13:04 浏览: 55
要使用python opencv提取目标轮廓坐标,可以使用以下代码:import cv2img = cv2.imread('image.jpg')gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(gray, 127, 255, 0)contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)cnt = contours[0]M = cv2.moments(cnt)x,y = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
相关问题
利用python opencv 写一个提取轮廓坐标的代码
我可以给你一个简单的示例代码,可以帮助你提取图像的轮廓坐标:import cv2image = cv2.imread("image.jpg")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray_image, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print("number of objects found = ", len(contours)) for c in contours:
cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
x, y, w, h = cv2.boundingRect(c)
print("coordinates of the rectangle = ", x, y, w, h)cv2.imshow("contours", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
利用python opencv 写一个提取目标轮廓精确坐标的代码
可以参考以下代码:import cv2image=cv2.imread('图片路径')imageGray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)contours, hierarchy=cv2.findContours(imageGray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)for c in contours: M=cv2.moments(c)cx = int(M['m10']/M['m00'])cy = int(M['m01']/M['m00'])print (cx,cy)
相关推荐














