gpt4 book ai didi

python - 在图像python中找到对象的边界?

转载 作者:行者123 更新时间:2023-12-02 17:37:44 24 4
gpt4 key购买 nike

我试图在图片中找到对象的边界,并在图像上显示轮廓,这是我的问题。我在这里有以下图片:

Rice

我执行了以下操作以提取轮廓并将其绘制在图像上:

import cv2
import numpy as np
img = cv2.imread('/home/rama/Downloads/rice.jpg')
rsz_img = cv2.resize(img, None, fx=0.25, fy=0.25) # resize since image is huge
gray = cv2.cvtColor(rsz_img, cv2.COLOR_BGR2GRAY) # convert to grayscale
plt.imshow(gray)
# threshold to get just the signature
cnts, hierarchy= cnts, hierarchy= cv2.findContours(gray.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
print(cv2.boundingRect(contour))
cv2.imshow('img',img)
cv2.imshow('contour', cv2.boundingRect(contour))
cv2.waitKey(0)
cv2.destroyAllWindows()

这给我的只是上面的图像,没有边界。

如何绘制在图像上找到的边界?

编辑解决方案:

我做了以下事情:
cnts, hierarchy= cv2.findContours(gray.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
print(cv2.boundingRect(contour))
cv2.drawContours(img,cnts,-1,(125,125,0),3 )
cv2.imshow('contours',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

我第一次看到它,第二次运行时我不再看到图像窗口了?我第一次确实看到了正确的边界!

最佳答案

您需要使用drawContours()函数。

看到这里:https://docs.opencv.org/3.3.1/d4/d73/tutorial_py_contours_begin.html

关于python - 在图像python中找到对象的边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48647379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com