niting
niting

Reputation: 2552

OpenCV accumulatedWeight error (assertion fails on comparison of channels and size)

I am running the following code to calculated the Running Average of a collection of images read from a video on OpenCV.

EDIT: (Code updated)

#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/video.hpp"

using namespace std;
using namespace cv;
int main(int argc, char *argv[]) {
    if(argc < 2) {
        printf("Quitting. Insufficient parameters\n");
        return 0;
    }
    char c;
    int frameNum = -1;

    const char* WIN_MAIN = "Main Window";
    namedWindow(WIN_MAIN, CV_WINDOW_AUTOSIZE);
    VideoCapture capture;
    capture.open(argv[1]);
    Mat acc, img;
    capture.retrieve(img, 3);
    acc = Mat::zeros(img.size(), CV_32FC3);
    for(;;) {
        if(!capture.grab()) {
            printf("End of frame\n");
            break;
        }

        capture.retrieve(img, 3);
        Mat floating;
        img.convertTo(floating, CV_32FC3);
        accumulateWeighted(floating, acc, 0.01);
        imshow(WIN_MAIN, img);
        waitKey(10);
    }
    return 0;
}

On running the code with a sample video the following error pops up

OpenCV Error: Assertion failed (dst.size == src.size && dst.channels() == cn) in accumulateWeighted, file /usr/lib/opencv/modules/imgproc/src/accum.cpp, line 430
terminate called after throwing an instance of 'cv::Exception'
  what():  /usr/lib/opencv/modules/imgproc/src/accum.cpp:430: error: (-215) dst.size == src.size && dst.channels() == cn in function accumulateWeighted

Aborted (core dumped)

What could be the possible reason for the error? Could you please guide me in the right direction?

Compiler used : g++ OpenCV version : 2.4.5

Thanks!

Upvotes: 3

Views: 3457

Answers (3)

Moirae
Moirae

Reputation: 139

I had the same problem problem and here's my solution.

Mat frame, acc;
// little hack, read the first frame
capture >> frame;
acc = Mat::zeros(frame.size(), CV_32FC3);

for(;;) {
...

Upvotes: 0

berak
berak

Reputation: 39796

from the refman:

src – Input image as 1- or 3-channel, 8-bit or 32-bit floating point.
dst – Accumulator image with the same number of channels as input image, 32-bit or 64-bit
floating-point.

so, your camera input img CV_8UC3, and your acc img is (currently) CV_32F. that's a misfit.

you want 3channel floating point for acc, so that's :

acc = Mat::zeros(img.size(), CV_8UC3);`

for more precision, you want to change your img to float type, too, so it would be:

   acc = Mat::zeros(img.size(), CV_32FC3);  // note: 3channel now

    for (;;) {

        if(!capture.grab()) {
            printf("End of frame\n");
            break;
        }

        capture.retrieve(img); // video probably has 1 stream only
        Mat floatimg;
        img.convertTo(floatimg, CV_32FC3);
        accumulateWeighted(floatimg, acc, 0.01);

EDIT:

try to replace your grab/retrieve sequence by:

for(;;) {
    capture >> img;
    if ( img.empty() )
        break;

Upvotes: 3

Marcassin
Marcassin

Reputation: 1405

As I can read in the OpenCV documentation of retrieve

C++: bool VideoCapture::retrieve(Mat& image, int channel=0);

second argument is the channel and in the documentation of accumulateWeighted it says :

C++: void accumulateWeighted(InputArray src, InputOutputArray dst, double alpha, InputArray mask=noArray() )

Parameters:src – Input image as 1- or 3-channel, 8-bit or 32-bit floating point.

But in your code :

capture.retrieve(img, 2);

I guess you have the wrong channel parameter

Upvotes: 0

Related Questions