OpenCV对象跟踪:Python&;urllib到C++;
问题很多:连接ip摄像头,即raspberry pi摄像头,它从raspberry pi流到我的pc。使用openCV;试图进行光学跟踪。我已经找到了一些在线源代码,我已经修改过,它是在C++中,而以前我在Python中编码更多的资源。p> 这是一个有效的块,注意,在python中,jpg图像的字节是被解析的。注意urllibOpenCV对象跟踪:Python&;urllib到C++;,python,c++,opencv,Python,C++,Opencv,问题很多:连接ip摄像头,即raspberry pi摄像头,它从raspberry pi流到我的pc。使用openCV;试图进行光学跟踪。我已经找到了一些在线源代码,我已经修改过,它是在C++中,而以前我在Python中编码更多的资源。p> 这是一个有效的块,注意,在python中,jpg图像的字节是被解析的。注意urllib import cv2 import urllib import numpy as np stream=urllib.urlopen('http://<ip>
import cv2
import urllib
import numpy as np
stream=urllib.urlopen('http://<ip>:<port>/?action=stream')
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('Robo-View',i)
if cv2.waitKey(1) == 27:
exit(0)
导入cv2
导入URL库
将numpy作为np导入
stream=urllib.urlopen('http://://?action=stream')
字节=“”
尽管如此:
字节+=流读取(1024)
a=字节。查找('\xff\xd8')
b=字节。查找('\xff\xd9')
如果一个=-1和b=-1:
jpg=字节[a:b+2]
字节=字节[b+2:]
i=cv2.imdecode(np.fromstring(jpg,dtype=np.uint8),cv2.CV\u LOAD\u IMAGE\u COLOR)
cv2.imshow(“机器人视图”,i)
如果cv2.waitKey(1)==27:
出口(0)
现在,对于C++代码片段:我相信从指定的IP地址捕获流。我需要找到\xff\xd8&\xff\xd9来识别jpg帧
int main(int argc, char* argv[])
{
//some boolean variables for different functionality within this
//program
bool trackObjects = true;
bool useMorphOps = true;
calibrationMode = true;
//Matrix to store each frame of the webcam feed
Mat cameraFeed;
//matrix storage for HSV image
Mat HSV;
//matrix storage for binary threshold image
Mat threshold;
//x and y values for the location of the object
int x = 0, y = 0;
//video capture object to acquire webcam feed
VideoCapture capture;
//Video located at this address:
const std::string vidStreamAddress = "http://<ip>:<port>/?action=stream";
//open capture object at vidstreamaddress
capture.open(vidStreamAddress);
//set height and width of capture frame
capture.set(CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
intmain(intargc,char*argv[])
{
//一些布尔变量用于此函数中的不同功能
//节目
bool trackObjects=true;
bool-useMorphOps=true;
校准模式=真;
//矩阵来存储网络摄像头提要的每个帧
Mat-cameraFeed;
//HSV图像的矩阵存储
Mat-HSV;
//二值阈值图像的矩阵存储
Mat阈值;
//对象位置的x和y值
int x=0,y=0;
//用于获取网络摄像头提要的视频捕获对象
视频捕获;
//位于此地址的视频:
const std::string vidStreamAddress=“http://:/?action=stream”;
//在vidstreamaddress处打开捕获对象
捕获。打开(vidStreamAddress);
//设置捕获帧的高度和宽度
捕获.设置(CV\U CAP\U PROP\U FRAME\U WITH,FRAME\U WITH);
捕获.设置(CV\U CAP\U PROP\U FRAME\U HEIGHT,FRAME\U HEIGHT);
感谢您的帮助。无论是在确定解析代码段的放置位置还是是否有可用的方法。谢谢