Ask Your Question
-1

How do i convert a Grayscale video to a Tensor?

asked 2017-07-13 04:40:57 -0500

rohitmulay gravatar image

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?

edit retag flag offensive close merge delete

Comments

1

To be clear, do you mean you want to stack the grayscale frames into a 3d matrix? Why not just use a vector/list of matrices?

alkasm gravatar imagealkasm ( 2017-07-13 04:54:03 -0500 )edit

Yes! i want to stack it, how do i use vector/list of matrices? I want to access the 3D matrix to perform some mathematical operations on it. @alkasm

rohitmulay gravatar imagerohitmulay ( 2017-07-13 04:58:48 -0500 )edit
1

context missing: what exactly do you need it for ?

berak gravatar imageberak ( 2017-07-13 05:12:29 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-07-13 05:11:52 -0500

alkasm gravatar image

First off, check out the OpenCV docs for the Mat class. It shows a bunch of different ways to access, copy, create Mats.

The easiest way would be to use the built-in OpenCV function merge(). Normally, this is used to merge three single channel images into a 3-channel image. For instance, if you split the red, green, and blue channels, process them, and then put them back into a single matrix. But it can actually be used on an arbitrary number of channels, and it accepts a vector of matrices to be merged. So in your for loop, you can push your grayscale image into your vector<Mat>, and then after the loop you can merge() all those matrices into a new matrix.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-07-13 04:40:57 -0500

Seen: 564 times

Last updated: Jul 13 '17