【问题标题】:Capture live stream from camera byte by byte using Open CV (C++)使用 Opencv (C++) 逐字节捕获摄像机的实时流
【发布时间】:2020-02-27 23:18:37
【问题描述】:

我正在使用一个模拟器(它是一组代码),它从源读取数据并将其转换为信号(例如光信号等)。我正在尝试在 VS2019 中实现以下功能:

  1. 从摄像头逐字节捕获视频(我不确定是否可行)
  2. 将视频字节转换为信号并保存到缓冲区中(为此使用了“bufferPut”函数)
  3. 加密此包含视频字节的信号
  4. 将加密数据发送到另一台计算机
  5. 解密第二台计算机中的数据并播放视频

我能够使用此链接逐帧(但我需要逐字节)从相机中捕获: Read and write video in Open CV

  1. 能否请您帮助我了解如何逐字节而不是逐帧捕获?
  2. 如果我需要为此使用其他库,请告诉我。

在这里你可以看到我的编码:

bool LoadFromCamera::runBlock(void) {

int space = outputSignals[0]->space();

if (!space) return false;


// # Create a VideoCapture object and use camera to capture the video ##############################
cv::VideoCapture cap(0);


// # Check if camera opened successfully ###########################################################
if (!cap.isOpened())
{
    std::cerr << "Error opening video stream" << std::endl;
    return -1;

    //system("pause");
    //system("exit");

}

// # Default resolution of the frame ###############################################################
int frame_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);


// # Define the codec and create VideoWriter object.The output is stored in 'outcpp.avi' file. #####
cv::VideoWriter video("outcpp.avi", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 10, cv::Size(frame_width, frame_height));
while (1)
{
    cv::Mat frame;

    cap >> frame; // Capture frame-by-frame

    if (frame.empty()) // If the frame is empty, break immediately
        break;



    outputSignals[0]->bufferPut((std::byte*)frame.data, frame.rows * frame.cols * frame.channels()); // Converts the frame into signal and save it into buffer



    video.write(frame); // Writes the frame into the file 'outcpp.avi'

    imshow("Frame", frame); // Displays the resulting frame 

    char c = (char)cv::waitKey(1); // Press  ESC on keyboard to  exit
    if (c == 27)


        break;
}


// # Release the video capture and write object #################################################
cap.release();
video.release();

// # Closes all the windows #####################################################################
cv::destroyAllWindows();

return 0;

}

非常感谢!

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    字节...你在说什么字节?有在图像传感器和相机控制器/图像处理器之间传输的字节,通过相机连接(USB、以太网,...)传输的字节,在 USB 或网络堆栈和设备驱动程序,媒体 API 和应用程序之间传输的字节,...

    我认为您在某种特殊意义上并不关心字节。你得到一个图像,你想加密它并继续发送。所有图像都是下面的字节。从字面上看,没有什么特别的事情可做。使用cv::Mat.ptr获取指向图像每一行的指针,然后对其进行加密,在接收端您需要设置一个具有相同格式和大小(行数和列数)的“空白”目的地cv::Mat ,然后解密每个传入的矩阵行并将其写入该矩阵的行 - 使用相同的cv::Mat.ptr() 方法获得。

    如果有任何遗漏,您必须编辑您的问题以明确您真正想要做什么。

    【讨论】:

    • 是的,我不关心某些特殊意义上的字节。我会按照你的建议去做。谢谢。
    猜你喜欢
    • 2020-02-23
    • 2017-11-10
    • 2021-03-17
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    相关资源
    最近更新 更多