Ask Your Question

Revision history [back]

How do i convert a Grayscale video to a Tensor?

I have written a code which converts a RGB video to Grayscale and now I want to convert it to a tensor. I just gave cout<<graymat <<"\n"; in my code and I'm getting an output but i guess it is in matrix but not in tensor. To compile this program i used the command g++ `pkg-config --cflags opencv` vid.cpp `pkg-config --libs opencv` -fopenmp and ./a.out to Run it and I'm using a 2 sec video in my code.

The following is the code:

#include "opencv2/highgui/highgui.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <omp.h>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
double total_non_parallel = omp_get_wtime();
    VideoCapture cap("/home/rmulay17/Downloads/2.mp4"); // open the video file for reading

    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the video file" << endl;
    }


    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

  while(1)
   {
        cv::Mat frame;
    cv::Mat graymat;

        bool bSuccess = cap.read(frame); // read a new frame from video

        if (!bSuccess) //if not success, break loop
        {
                   //     cout << "Cannot read the frame from video file" << endl;
                       break;
        }

         cv::cvtColor(frame,graymat,cv::COLOR_BGR2GRAY); //Converts RGB to Grayscale
         Mat float_mat;
         graymat.convertTo( float_mat,CV_8U);

         imshow("MyVideobnw", graymat);

     cout<<graymat <<"\n"; // This Prints Frames of Grayscale video on terminal in matrix format
     double fps = cap.get(CV_CAP_PROP_FPS);
         cout << "Frame per seconds : " << fps << endl;
        imshow("MyVideo", frame); //show the frame in "MyVideo" window

//FileStorage file("/home/rmulay17/Desktop/Filevid.txt",FileStorage::WRITE);
//file<<graymat; //NOT WORKING!

        if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
       {
                cout << "esc key is pressed by user" << endl;
                break;
       }

    }
    printf("\n");
    printf("%lf is the total time taken\n",total_non_parallel);
    printf("\n");
    return 0;

}

Can someone help me generate a tensor from a grayscale video?