Ask Your Question

Revision history [back]

i want to share here the code below

i want to share here the code below

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat frame,back;
    VideoCapture cap(argv[1]);
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open video file\n";
        return -1;
    }

    cap.read(back); // i used blurred first frame as background 
    blur(back, back, Size(20, 20));
    back(Rect(0, back.rows / 8, back.cols, back.rows / 8 * 6)).setTo(Scalar(0, 0, 0));
    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            break;
        }

        Mat roi(back, Rect(back.cols / 8, back.rows / 8, back.cols / 8 * 6, back.rows / 8 * 6));
        resize(frame, roi, roi.size());
        imshow("video", back);
        if (waitKey(1) >= 0)
            break;
    }
    return 0;
}

i want to share here the code below

basically you can do the job using only cv:resize() function. ( you don't need to process border part for all frames)

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat frame,back;
    VideoCapture cap(argv[1]);
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open video file\n";
        return -1;
    }

    cap.read(back); // i used blurred first frame as background 
    blur(back, back, Size(20, 20));
    back(Rect(0, back.rows / 8, back.cols, back.rows / 8 * 6)).setTo(Scalar(0, 0, 0));
    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            break;
        }

        Mat roi(back, Rect(back.cols / 8, back.rows / 8, back.cols / 8 * 6, back.rows / 8 * 6));
        resize(frame, roi, roi.size());
        imshow("video", back);
        if (waitKey(1) >= 0)
            break;
    }
    return 0;
}

i want to share here the code below

basically you can do the job using only

  1. prepare background image
    1. define a roi
    2. use cv:resize() function. ( function to resize frame in background image

you don't need can try sample c++ or java code below to process border part for all frames)get something like

image description

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat frame,back;
    VideoCapture cap(argv[1]);
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open video file\n";
        return -1;
    }

    cap.read(back); // i used blurred first frame as background      
    cap.read(back);
    blur(back, back, Size(20, 20));

    // set black area in the background     
    back(Rect(0, back.rows / 8, back.cols, back.rows / 8 * 6)).setTo(Scalar(0, 0, 0));
    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            break;
        }

    
    // define a roi
    Mat roi(back, Rect(back.cols / 8, back.rows / 8, back.cols / 8 * 6, back.rows / 8 * 6));

    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            break;
        }

        resize(frame, roi, roi.size());
        imshow("video", back);
        if (waitKey(1) >= 0)
            break;
    }
    return 0;
}

java

package test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.highgui.HighGui;
import org.opencv.videoio.VideoCapture;

public class test_cv
{
    public static void main( String[] args )
    {
        System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
        VideoCapture capture = new VideoCapture("C:\\build\\opencv4.0.1\\bin\\Release\\vtest.avi");
        if (!capture.isOpened())
        {
            System.err.println("Unable to open  video ");
            System.exit(0);
        }

        Mat frame = new Mat();
        Mat back = new Mat();

        capture.read(back);

        Imgproc.blur(back, back, new Size(20,20));

        Mat roi =back.submat(new Rect(0,back.rows()/8,back.cols(),back.rows()/8*6));
        roi.setTo(new Scalar(0,0,0));

        roi = back.submat(new Rect(back.cols()/8,back.rows()/8,back.cols()/8*6,back.rows()/8*6));

        while (true)
        {
            capture.read(frame);
            if (frame.empty())
            {
                break;
            }

            Imgproc.resize(frame, roi, roi.size());

            HighGui.imshow("Frame", back);
            HighGui.waitKey(1);

        }    
    }
}
  1. prepare background image
    1. define a roi
    2. use cv:resize() function to resize frame in background image

you can try sample c++ or java code below to get something like

image description

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat frame,back;
    VideoCapture cap(argv[1]);
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open video file\n";
        return -1;
    }

   // i used blurred first frame as background     
    cap.read(back);
    blur(back, back, Size(20, 20));

    // set black area in the background     
    back(Rect(0, back.rows / 8, back.cols, back.rows / 8 * 6)).setTo(Scalar(0, 0, 0));

    // define a roi
    Mat roi(back, Rect(back.cols / 8, back.rows / 8, back.cols / 8 * 6, back.rows / 8 * 6));

    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            break;
        }

        resize(frame, roi, roi.size());
        imshow("video", back);
        if (waitKey(1) >= 0)
            break;
    }
    return 0;
}

java

package test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.highgui.HighGui;
import org.opencv.videoio.VideoCapture;

public class test_cv
{
    public static void main( String[] args )
    {
        System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
        VideoCapture capture = new VideoCapture("C:\\build\\opencv4.0.1\\bin\\Release\\vtest.avi");
        if (!capture.isOpened())
        {
            System.err.println("Unable to open  video ");
            System.exit(0);
        }

        Mat frame = new Mat();
        Mat back = new Mat();

        capture.read(back);

        Imgproc.blur(back, back, new Size(20,20));

        Mat roi =back.submat(new Rect(0,back.rows()/8,back.cols(),back.rows()/8*6));
        roi.setTo(new Scalar(0,0,0));

        roi = back.submat(new Rect(back.cols()/8,back.rows()/8,back.cols()/8*6,back.rows()/8*6));

        while (true)
        {
            capture.read(frame);
            if (frame.empty())
            {
                break;
            }

            Imgproc.resize(frame, roi, roi.size());

            HighGui.imshow("Frame", back);
            HighGui.waitKey(1);

        }    
    }
}
    • prepare a background image
        • define a roi
        • use cv:resize() function to resize frame in background image

you can try sample c++ or java code below to get something like

image description

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat frame,back;
    VideoCapture cap(argv[1]);
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open video file\n";
        return -1;
    }

   // i used blurred first frame as background     
    cap.read(back);
    blur(back, back, Size(20, 20));

    // set black area in the background     
    back(Rect(0, back.rows / 8, back.cols, back.rows / 8 * 6)).setTo(Scalar(0, 0, 0));

    // define a roi
    Mat roi(back, Rect(back.cols / 8, back.rows / 8, back.cols / 8 * 6, back.rows / 8 * 6));

    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            break;
        }

        resize(frame, roi, roi.size());
        imshow("video", back);
        if (waitKey(1) >= 0)
            break;
    }
    return 0;
}

java

package test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.highgui.HighGui;
import org.opencv.videoio.VideoCapture;

public class test_cv
{
    public static void main( String[] args )
    {
        System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
        VideoCapture capture = new VideoCapture("C:\\build\\opencv4.0.1\\bin\\Release\\vtest.avi");
        if (!capture.isOpened())
        {
            System.err.println("Unable to open  video ");
            System.exit(0);
        }

        Mat frame = new Mat();
        Mat back = new Mat();

        capture.read(back);

        Imgproc.blur(back, back, new Size(20,20));

        Mat roi =back.submat(new Rect(0,back.rows()/8,back.cols(),back.rows()/8*6));
        roi.setTo(new Scalar(0,0,0));

        roi = back.submat(new Rect(back.cols()/8,back.rows()/8,back.cols()/8*6,back.rows()/8*6));

        while (true)
        {
            capture.read(frame);
            if (frame.empty())
            {
                break;
            }

            Imgproc.resize(frame, roi, roi.size());

            HighGui.imshow("Frame", back);
            HighGui.waitKey(1);

        }    
    }
}
  • prepare a background image
    • define a roi
    • use cv:resize() function to resize frame in background image

you can try sample c++ or java code below to get something like

image description

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat frame,back;
    VideoCapture cap(argv[1]);
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open video file\n";
        return -1;
    }

   // i used blurred first frame as background     
    cap.read(back);
    blur(back, back, Size(20, 20));

    // set black area in the background     
    back(Rect(0, back.rows / 8, back.cols, back.rows / 8 * 6)).setTo(Scalar(0, 0, 0));

    // define a roi
    Mat roi(back, Rect(back.cols / 8, back.rows / 8, back.cols / 8 * 6, back.rows / 8 * 6));

    TickMeter tm;
    tm.start();
    int counter = 0;

    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            break;
        }

        resize(frame, roi, roi.size());

        if (!(counter % 20)) // don't show all frames
        {
            imshow("video", back);
        if (waitKey(1) >= 0)
            break;
    }
    waitKey(1);
        }
        counter++;
    }

    tm.stop();
    cout << tm;
    return 0;
}

java

package test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Size;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.highgui.HighGui;
import org.opencv.videoio.VideoCapture;

public class test_cv
{
    public static void main( String[] args )
    {
        System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
        VideoCapture capture = new VideoCapture("C:\\build\\opencv4.0.1\\bin\\Release\\vtest.avi");
        if (!capture.isOpened())
        {
            System.err.println("Unable to open  video ");
            System.exit(0);
        }

        Mat frame = new Mat();
        Mat back = new Mat();

        capture.read(back);

        Imgproc.blur(back, back, new Size(20,20));

        Mat roi =back.submat(new Rect(0,back.rows()/8,back.cols(),back.rows()/8*6));
        roi.setTo(new Scalar(0,0,0));

        roi = back.submat(new Rect(back.cols()/8,back.rows()/8,back.cols()/8*6,back.rows()/8*6));

        while (true)
        {
            capture.read(frame);
            if (frame.empty())
            {
                break;
            }

            Imgproc.resize(frame, roi, roi.size());

            HighGui.imshow("Frame", back);
            HighGui.waitKey(1);

        }    
    }
}