首页 > 解决方案 > 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()

最后我想得到相同的结果(这是我的结果,但我需要新视频的新代码)

在此处输入图像描述

标签: pythonopencvrealsense

解决方案


推荐阅读