python - OpenCV 视频处理和链接组件
问题描述
我有这个视频(见彩色),处理后我得到这个(见黑白)
颜色越红,离相机越近,我使用英特尔实感获得图像
我可以使用这段代码来获取每个对象的 id:
output = cv2.connectedComponentsWithStats(image, 4, cv2.CV_32S)
(numLabels, labels, stats, centroids) = output
但是现在它不起作用,我需要以其他方式处理视频有人有什么想法吗?
import cv2
import numpy as np
import glob
from matplotlib.colors import Normalize
import matplotlib.pyplot as plt
from dataclasses import dataclass
@dataclass
class Foo:
lower_, upper_ = 500, 530
se1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 9))
se2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (13, 13))
def filter(self):
norm = Normalize(vmin=500, vmax=600)
for frame in glob.glob('\*npy'):
frame = np.load(frame)[:, 210:-290]
threshold1 = cv2.threshold(frame, self.lower_, 65535, cv2.THRESH_BINARY_INV)[1]
threshold2 = cv2.threshold(frame, self.upper_, 65535, cv2.THRESH_BINARY)[1]
threshold = cv2.bitwise_or(threshold1, threshold2)
mask = cv2.morphologyEx(threshold, cv2.MORPH_OPEN, self.se1)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, self.se2)
cv2.imshow('test1', cv2.threshold(mask, 0, 65535, cv2.THRESH_BINARY_INV)[1])
cv2.imshow('test2', plt.cm.jet(norm(frame)))
if __name__ == '__main__':
Foo().filter()
最后我想得到相同的结果(这是我的结果,但我需要新视频的新代码)
解决方案
推荐阅读
- mysql - 如何将sql查询中的值存储到节点js中的变量?
- oracle - 尝试创建对象表 ERROR:Error(50,57): PLS-00765: COLUMNS or TABLE is not allowed in this context
- python - 在python中将变量复制到数据框中所有行的快速方法
- ruby - Jekyll 图像破坏 - 不会加载新图像
- linux - 使用 sed 时日期文件之间的转义分隔符
- c# - 在 Xamarin 中使用来自 AndroidKeyStore 的密钥解包封装的 CMS 数据
- vue.js - 如何在 VueJS 模板中声明具有动态名称和动态组件的插槽列表?
- c++ - 将类重新定义为不同类型的符号
- java - log4j 从哪里选择保存日志的驱动器
- c# - 两个 azure web api 如何在 azure 中通信