gpt4 book ai didi

python-3.x - 从网络摄像头 OPENCV PYTHON 移除背景

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

我正在创建一个脚本,它将读取超市的状态并告诉我是否缺少产品。
例如,在下图中,有些地方缺少产品。我正在使用 方法来查找框架中的所有角。但有时脚本会检测到地板角落。我想做的是在找到角落之前从框架中移除地板。

enter image description here
导入简历2
将 numpy 导入为 np

image = cv2.imread('gondola_imagem.jpeg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

fast = cv2.FastFeatureDetector_create()
# Obtain Key points, by default non max suppression is On
# to turn off set fast.setBool('nonmaxSuppression', False)
keypoints = fast.detect(gray, None)
print ("Number of keypoints Detected: ", len(keypoints))

image = cv2.drawKeypoints(image, keypoints, None,
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

cv2.imshow('Feature Method - FAST', image)
cv2.waitKey()
cv2.destroyAllWindows()

最佳答案

您可以使用 mask 去除您不感兴趣的区域。例如,使用以下图像作为蒙版,您可以获得以下结果。

Mask

Result

代码如下:

import numpy as np
import cv2
image = cv2.imread('test.jpg')
mask = cv2.imread('mask.jpg', 0)
cv2.imshow('Original', image)
cv2.imshow('Mask', mask)
res = cv2.bitwise_and(image,image,mask = mask)
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)

fast = cv2.FastFeatureDetector_create()
# Obtain Key points, by default non max suppression is On
# to turn off set fast.setBool('nonmaxSuppression', False)
keypoints = fast.detect(gray, None)
print ("Number of keypoints Detected: ", len(keypoints))

image = cv2.drawKeypoints(image, keypoints, None,
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

cv2.imwrite('result.jpg', image)
cv2.imshow('Feature Method - FAST', image)
cv2.waitKey()
cv2.destroyAllWindows()

编辑:
如果您想实时执行此操作(来自网络摄像头的视频),您只需为从摄像机获取的每一帧执行此操作。只要相机不移动,您就应该能够对所有帧使用相同的蒙版。您可以将代码置于函数之上,然后使用图像作为参数调用它,如下所示:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)

while(True):
# Capture frame-by-frame
ret, frame = cap.read()

# Following function will have to be created with the previews code
CallFunctionToPreviewsCode(frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

上面的代码取自 OpenCV Python-Tutorials这是一个学习 Python 编程语言的 OpenCV 的好地方。

关于python-3.x - 从网络摄像头 OPENCV PYTHON 移除背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60077937/

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