gpt4 book ai didi

python - OpenCV中的阈值未检测到我想要获取的完整对象。我怎样才能解决这个问题?

转载 作者:行者123 更新时间:2023-12-02 17:32:42 25 4
gpt4 key购买 nike

我想从视频提要中检测出鸡蛋,当我尝试对其使用阈值时,它无法获得完整的鸡蛋。

我尝试从此https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html应用不同的阈值步骤

将阈值应用于不同的轮廓,结果如下

ret, img = cap.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

ret,th1 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
ret2,th4 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

blur = cv2.GaussianBlur(gray,(5,5),0)
ret3,th5 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

dummy,cnts,hier = cv2.findContours(th1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for c in cnts:
M = cv2.moments(c)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
cX, cY = 0, 0
cv2.drawContours(img, [c], -1, (0, 255, 0), 2)
cv2.circle(img, (cX, cY), 2, (0, 0, 0), -1)
cv2.imshow("Global",th1)
cv2.imshow("Adaptive Mean",th2)
cv2.imshow("Adaptive Gaussian",th3)
cv2.imshow("Otsu's",th4)
cv2.imshow("Otsu's after Blur",th5)

https://imgur.com/a/qgLMkj6

更新:
使用@Martin的答案后,我想出了这个

/image/2EQVM.jpg

通过获取具有最大面积的轮廓。但也有其他轮廓较大的区域。下一个问题是我该怎么做才能过滤掉下面的其他轮廓?我正在考虑确定哪个轮廓是否有角,因为鸡蛋是椭圆形的。另一种方法是裁剪图像,因为鸡蛋仅在图像的上部,但我不知道如何。

码:
dummy,cnts,hier = cv2.findContours(close.astype(np.uint8),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
#print (len(cnts))

for c in cnts:
M = cv2.moments(c)
area = cv2.contourArea(c)
print (area)
if area >46000:
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
cX, cY = 0, 0
cv2.drawContours(img, [c], -1, (0, 255, 0), 2)
cv2.circle(img, (cX, cY), 2, (0, 0, 0), -1)
cv2.imshow("th5",img)

最佳答案

您没有吃饱鸡蛋的原因是因为阈值太高。你需要降低一点

喜欢:

limit = 100 # possible lower
ret,th1 = cv2.threshold(gray,limit,255,cv2.THRESH_BINARY)

但是,您的问题很大,因为背景(鸡蛋所在的对象)的颜色与鸡蛋的颜色相同。您可能想尝试边缘检测而不是阈值化。

看一下这个:

enter image description here

在播放图像时,我能够得到边缘(仅中途):

enter image description here

码:
import cv2
import numpy as np
img = cv2.imread('eBxV8IA.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(15,15),0)
lap = cv2.Laplacian(blur,cv2.CV_64F)
blur = cv2.GaussianBlur(lap,(45,45),0)
cv2.imshow("Global",blur)

我能够准确地检测到鸡蛋,但不幸的是,也有很多噪音
enter image description here

码:
import cv2
import numpy as np
img = cv2.imread('eBxV8IA.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(15,15),0)
lap = cv2.Laplacian(blur,cv2.CV_64F)
blur = cv2.GaussianBlur(lap,(45,45),0)
blur[blur<0]=0
blur = 255.*blur/np.amax(blur)


dummy,cnts,hier = cv2.findContours(blur.astype(np.uint8),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for c in cnts:
M = cv2.moments(c)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
cX, cY = 0, 0
cv2.drawContours(img, [c], -1, (0, 255, 0), 2)
cv2.circle(img, (cX, cY), 2, (0, 0, 0), -1)
cv2.imshow("Global",img)

关于python - OpenCV中的阈值未检测到我想要获取的完整对象。我怎样才能解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54513076/

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