【问题标题】:Track objects in OpenCV from incoming MJPEG Stream从传入的 MJPEG 流跟踪 OpenCV 中的对象
【发布时间】:2014-07-18 20:19:26
【问题描述】:

我已经成功地使用 mjpeg-streamer 从我的设备捕获了一个 mjpeg 流。以下代码是我如何在 OpenCV-python 中检索此流:

import cv2
import numpy as np
import urllib

stream=urllib.urlopen('http://@192.168.7.2:8090/?action=stream/frame.mjpg')
bytes=''
while True:
    bytes+=stream.read(1024)
    a = bytes.find('\xff\xd8')
    b = bytes.find('\xff\xd9')
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
        i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
        cv2.imshow('i',i)
        if cv2.waitKey(1) ==27:
            exit(0) 

我还有根据颜色范围跟踪移动对象的代码。这段代码的视频源是直接从 OpenCV 中的网络摄像头中提取的。这是代码:

import cv2.cv as cv
import time
import sys

capture = CaptureFROMCAM(0)

while True:
img = cv.QueryFrame(capture)


cv.Smooth(img,img,cv.CV_BLUR,3)
hue_img = cv.CreateImage(cv.GetSize(img),8, 3)
cv.CvtColor(img,hue_img, cv.CV_BGR2HSV)

# Remove all the pixels that don't match
threshold_img = cv.CreateImage(cv.GetSize(hue_img), 8, 1)
cv.InRangeS(hue_img, (100,180,80), (225,160,80), threshold_img)

# Find all the areas of color out there
storage = cv.CreateMemStorage(0)
contour = cv.FindContours(threshold_img, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)

# Step through all the areas
points = []
while contour:
    # Get the info about this area
    rect = cv.BoundingRect(list(contour))
    contour = contour.h_next()
    # Check to make sure the area is big enough to be of concern
    size = (rect[2] * rect[3])
    if size > 25:
        pt1 = (rect[0], rect[1])
        pt2 = (rect[0] + rect[2], rect[1]+rect[3])
        # Add a rectangle to the initial image
        cv.Rectangle(img, pt1, pt2, (15,15,255))    

threshold_img = cv.CreateImage(cv.GetSize(hue_img),8,1)
cv.InRangeS(hue_img, (16,82,19), (30,255,255), threshold_img)

cv.ShowImage("Color Tracking", img)
cv.ShowImage("threshold", threshold_img)    

if cv.WaitKey(10) == 27:
    success, frame = videoCapture.read()
    while success:
        videoWriter.write(frame)
        success, frame = videoCapture.read()
    break

我的问题是:如何结合这两个过程,以便我可以使用第一个程序完成的解码 jpeg 图像作为第二个代码中图像处理的输入?我已经尝试了各种组合,但我仍然没有任何运气。我一直收到错误

cv.QueryFrame 没有有效的参数'capture'

这告诉我它不喜欢我尝试提供的 jpeg 格式。有没有人有什么建议?谢谢!!!

【问题讨论】:

    标签: python opencv image-processing mjpeg


    【解决方案1】:

    连接两种算法的关键思想:

    #infinite loop
    #...stream reading operations
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
        img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
        if cv2.waitKey(1) ==27:
            exit(0) 
    else:
        continue
    cv.Smooth(img,img,cv.CV_BLUR,3)
    #... other tracker operations ...
    

    imdecode 函数“从内存中的缓冲区读取图像”。 QueryFrame 从视频捕获设备读取数据并返回图像。 (“抓取、解码并返回下一个视频帧。”)因此,这两种方法都为您提供了一个图像对象,但它们中的每一个都来自不同的源(相机与缓冲区)。图像是您在跟踪器中进一步处理所需的正确东西!大多数 OpenCV 方法使用图像作为其固有的栅格数据格式。

    管道是这样的:从图像文件或视频中捕获帧/加载图片-> [图像]-> 对图像进行处理、计算、阈值、轮廓等-> 显示结果或修改后的图像-> 重复这个(永远:-))。请参阅http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html 了解更多信息。

    【讨论】:

    • OP & @karlphilip 您好,谢谢您的回复。我尝试了这种方法,openCV 告诉我 img 需要和 openCVMat 并使用 cv.fromarray() 来执行此操作。所以这就是我最终得到的结果:cv.fromarray((img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)), allowND=False) 这仅显示一张图像然后冻结。我认为它会在捕获图像时不断转换图像,但它要么似乎只捕获一个图像,要么冻结。有什么建议???
    【解决方案2】:

    检查第一个源代码,当执行cv2.imshow('i',i) 时,网络摄像头图像显示在窗口上。

    此时,您应该使用第二个代码中的算法处理它,而不是显示图像,该算法从img = cv.QueryFrame(capture) 开始。这意味着您不再需要这些行了:

    capture = CaptureFROMCAM(0)
    
    while True:
    img = cv.QueryFrame(capture)
    

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 1970-01-01
      • 2014-01-09
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多