Ask Your Question
0

Put two or more frames in one output window

asked 2017-01-17 08:10:14 -0600

simozz gravatar image

updated 2017-01-17 08:14:16 -0600

I have 4 frames I would like to put all them in one window..

I am following this tutorial which should be perfect for my objective .. but my code is not working (I don't know if the code of the blogger works..).

This is the code I am working on (resumed)

VideoCapture capture;
Mat currentFrame;
Mat foreground;
Mat postfgmask;
Mat resultframe;

Size imgFixedSize = Size(500, 350);

Mat roi;
Mat merged = Mat(Size(imgFixedSize*height*2, imgFixedSize*width*2), CV_8UC3);

/* initialize capture, not shown .. */ 

while (true)
{
    capture >> currentFrame;
    resize(currentFrame, currentFrame, imgFixedSize);

    /* 
        image processing tasks, not shown .. 
        all frames are Size(500, 350) ..
    */ 

    roi = Mat(merged, Rect(0, 0, 500, 350));
    currentFrame.copyTo(roi);

    roi = Mat(merged, Rect(500, 0, 500, 350));
    foreground.copyTo(roi);

    roi = Mat(merged, Rect(0, 350, 500, 350));
    postfgmask.copyTo(roi);

    roi = Mat(merged, Rect(500, 350, 500, 350));
    resultframe.copyTo(roi);

    imshow("Output", merged);
    writer.write(merged);
}

But when I execute the program, it crashes giving this errror to the output:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /home/user/opencv/modules/core/src/matrix.cpp, line 522
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/user/opencv/modules/core/src/matrix.cpp:522: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat

Aborted

It seems that the error is due to frames size .. But the dimensions are quiet clear and the output window is doubled in size so it can fit 4 frames..

So how do I have to change my code to make it works ?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2017-01-17 08:15:24 -0600

berak gravatar image

updated 2017-01-17 08:21:06 -0600

Size is Size(width, height), while the Mat constructor is Mat(H,W,...) . you simply reversed it here:

Mat merged = Mat(Size(imgFixedSize.height*2, imgFixedSize.width*2), CV_8UC3);

this would be ok:

Mat merged = Mat(imgFixedSize.height*2, imgFixedSize.width*2, CV_8UC3);

or that:

Mat merged = Mat(Size(imgFixedSize.width*2, imgFixedSize.height*2), CV_8UC3);

again, just a simple typo, you're almost there !

(oh, and also size.width, not size*width , thanks @LBerger.)

edit flag offensive delete link more

Comments

Hi, thank you for your help but changing the code as suggested (of course is correct as you write) does not change the situation. Same error.

simozz gravatar imagesimozz ( 2017-01-17 09:55:17 -0600 )edit
1

Have you opened VideoCapture? and don't forget waitKey(1) after imshow.

LBerger gravatar imageLBerger ( 2017-01-17 10:00:20 -0600 )edit
1

Yes. Problem solved for it. Thanks But, the foreground and postfgmask are shown entirely black in merged while are shown correctly in a separate imshow window.. They are respectively a binary image and a grayscale image obtained after a gaussianblur filtering. I tried to convert them to a CV_8UC3 but thse are always shown black (all 0).

simozz gravatar imagesimozz ( 2017-01-17 10:34:37 -0600 )edit

I modified the previous comment. Thank you.

simozz gravatar imagesimozz ( 2017-01-17 10:46:15 -0600 )edit

Is it solved?

LBerger gravatar imageLBerger ( 2017-01-17 10:56:04 -0600 )edit
2

answered 2017-01-18 10:19:06 -0600

Eduardo gravatar image

updated 2017-01-18 10:20:18 -0600

Another solution is to use hconcat and vconcat. Will work for trivial dimensions.

Demo code:

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

using namespace cv;

int main() {
  Mat img11(240, 320, CV_8UC3, Scalar(0,0,255));
  Mat img12(240, 320, CV_8UC3, Scalar(0,255,0));
  Mat img21(240, 320, CV_8UC3, Scalar(255,0,0));
  Mat img22(240, 320, CV_8UC3, Scalar(255,0,255));

  Mat concat1;
  hconcat(img11, img12, concat1);

  Mat concat2;
  hconcat(img21, img22, concat2);

  Mat concat;
  vconcat(concat1, concat2, concat);

  imshow("Concat", concat);
  waitKey();

  return 0;
}
edit flag offensive delete link more

Comments

Can anyone please give me the same code in python to put 2 frames or 4 frames in a single window output??

Digbijay2 gravatar imageDigbijay2 ( 2018-11-03 09:13:20 -0600 )edit

@Digbijay2 -- please do not post answers here, ifyou have a question or comment, thank you.

(and no we're not a code delivery service)

berak gravatar imageberak ( 2018-11-03 09:18:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-01-17 08:10:14 -0600

Seen: 2,962 times

Last updated: Jan 18 '17