我试图确定一张照片中新出现的物体列表。该计划是从原始图像中获得多个裁剪图像,并将其输入到用于目标检测的神经网络。现在,我在提取出现在帧中的对象时遇到问题
import cv2 as cv
import matplotlib.pyplot as plt
def mdisp(image):
plt.imshow(image)
plt.show()
im1 = cv.imread('images/litter-before.jpg')
mdisp(im1)
print(im1.shape)
im2 = cv.imread('images/litter-after.jpg')
mdisp(im2)
print(im2.shape)
backsub1=cv.createBackgroundSubtractorMOG2()
backsub2=cv.createBackgroundSubtractorKNN()
fgmask = backsub1.apply(im1)
fgmask = backsub1.apply(im2)
print(fgmask.shape)
mdisp(fgmask)
new_image = im2 * (fgmask[:,:,None].astype(im2.dtype))
mdisp(new_image)
理想情况下,我想得到一个红色圆圈内的项目裁剪图片。我如何使用OpenCv实现它
这是一种直接减去两帧的方法。其思想是首先将图像转换为
grayscale
,然后稍微转换blur
以忽略噪声Subtract
这两个帧,threshold
计算差异并查找高于某个区域阈值的最大的blob让我们看看:
现在有了灰度模糊的帧。接下来,我们需要计算这些帧之间的差异。我不想丢失数据,所以我必须小心这里的数据类型。请记住,这些是灰度矩阵,但差异可能会产生负值。让我们将矩阵转换为
float
s,取差,然后将此矩阵转换为uint8:这将为您提供帧差异:
让我们对其设置阈值以获得二值图像。我使用的阈值是
127
,因为它是8位范围的中心:这是二进制图像:
我们在这里寻找最大的blob,让我们找到
blob/contours
并过滤小的blob。让我们将最小面积设置为10像素:查看结果:
相关问题 更多 >
编程相关推荐