python - 为什么 find_objects 会给出一堆无,然后是整个图像的范围?

标签 python opencv scipy scipy.ndimage

我试图找到在不同帧中检测到的所有对象,我认为这将给出阈值中检测到的每个区域的列表,但是 find_objects给出一堆“无”和整个图像的范围?

...
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
(slice(0, 972, None), slice(0, 1296, None))
相关代码可从here 测试
import numpy as np
import cv2
from matplotlib import pyplot as plt
import pylab

from scipy import ndimage
import os

for img in os.listdir('.'):
    if img.endswith('.jpg'):
        image = cv2.imread(img)
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        gray_image = cv2.resize(gray_image, (int(gray_image.shape[1]/2), int(gray_image.shape[0]/2) ))
        ret, threshimg = cv2.threshold(gray_image,0,255,cv2.THRESH_BINARY)
        cv2.imshow('opening',threshimg)
        objects = ndimage.find_objects(threshimg)
        for ob in objects:
            print(ob)
        cv2.waitKey(0)                 # Waits forever for user to press any key   
        cv2.destroyAllWindows()

最佳答案

scipy.ndimage.find_objects函数返回

A list of tuples, with each tuple containing N slices (with N the dimension of the input array). Slices correspond to the minimal parallelepiped that contains the object. If a number is missing, None is returned instead of a slice.


我认为在您的数据中,标签不是从 1 开始的(0 是背景,被 find_objects 忽略)。不是 None 的条目的索引是对象的整数值。

关于python - 为什么 find_objects 会给出一堆无,然后是整个图像的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62734440/

相关文章:

python - 设置自定义 viewerProcess 失败

python - 验证 python 中的对象是否遵循特定结构

python - Scikit Learn 朴素贝叶斯

c++ - 添加相邻像素的问题

python - 尝试导入 scipy.stats 时出错 : No module named dual

python - 查找表示图像的屏蔽 ndarray 的中值

python - 获取支持 NA/可为空的 boolean pandas 列

OpenCV Hough 最强线

python - OpenCV:使用 Canny 和 Shi-Tomasi 检测扑克牌的圆角

python - 作为积分变量的函数