1

I try to subtract using bitwise_and and BackgroundSubtractor but I have this error:

OpenCV Error: Assertion failed ((mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1)) in cv::binary_op, file C:\build\master_winpack-build-win64-vc14\opencv\modules\core\src\arithm.cpp, line 241

code:

Mat frame1;
Mat frame_mask;

bool bSuccess = cap.read(frame1); 

if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
pMOG2->apply(frame1, frame_mask);
Mat kernel = Mat::ones(frame1.size(), CV_8UC1);
erode(frame1, frame_mask, kernel);
bitwise_and(frame1, frame1, frame, frame_mask);

Error occurs when I use bitwise_and(...) after erode. Separately they work fine.

I used OpenCV 3.2.0 and VS15. I'm pretty new at the OpenCV, so could you please say, what I do wrong? Thank you.

kuppy28
  • 25
  • 4
  • From the error, it would seem your source and mask are not the same size or the mask isn't 8-bit. – Khouri Giordano May 22 '17 at 01:06
  • What you're trying to do isn't clear, do you want to only keep the moving objects, like in this [picture](https://i.stack.imgur.com/GpFFg.png)? – Elouarn Laine May 22 '17 at 08:16
  • @ElouarnLaine yes, I'm trying to do something like this – kuppy28 May 22 '17 at 09:31
  • @KhouriGiordano they have the same size - 640x480 and in my code I've done something like this: Mat frame_mask(frame.size(), CV_8UC1); but I still have this error:( – kuppy28 May 22 '17 at 09:33

1 Answers1

2

The error is raised because your frame_mask is not of type CV_8U or CV_8S.

Indeed, with this line : erode(frame1, frame_mask, kernel)

frame_mask is transformed into a CV_8UC3 Mat because the input Mat (frame1) is of type CV_8UC3.


Anyways, I don't really understand what you are trying to do with the bitwise_and operation so I did a minimal example to show the proper way to do what you want:

int main(int argc, char** argv)
{
    Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2();

    VideoCapture cap = VideoCapture(0);
    Mat frame1, frame_mask;

    while (cap.isOpened())
    {
        cap.read(frame1);
        pMOG2->apply(frame1, frame_mask);

        Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3)); // erode filter's kernel
        erode(frame_mask, frame_mask, kernel);

        Mat movingAreas;
        frame1.copyTo(movingAreas, frame_mask);

        imshow("movingParts", movingAreas);

        keyCode = waitKey(1);
        if (keyCode == 27 || keyCode == 'q' || keyCode == 'Q')
            break;
    }

    return 0;
}

And here is the result: enter image description here

Hope it helps!

Elouarn Laine
  • 1,665
  • 15
  • 27
  • @kuppy28 np, consider accepting the answer if it helped you ;) – Elouarn Laine May 23 '17 at 22:44
  • excuse me, but I have one more question. I want to have an effect like in this [video](https://www.youtube.com/watch?v=CmBxUnp7XwM&feature=youtu.be) (in the left down corner). What should I do? – kuppy28 May 31 '17 at 22:04
  • @kuppy28 I don't see any effects in the bottom left window (entitled _mask_). Can you further explain? – Elouarn Laine May 31 '17 at 22:08
  • ok, background in this video is constantly black and we see only the hand, right? After starting my code, I got this [effect](https://youtu.be/gOHoR0bOPKA) – kuppy28 May 31 '17 at 22:22
  • @kuppy28, I'm a bit confused. The effect you want is exactly what I explained in my answer. Have you tried to compile my code snippet? – Elouarn Laine May 31 '17 at 22:30
  • I'm too. Yes, I've tried to compile your code and unfortunately effect is the same:( – kuppy28 May 31 '17 at 22:38
  • Hmm, actually your result seems correct besides the fact that the color is not right. What camera are you using? It seems to output **HSV** images. – Elouarn Laine May 31 '17 at 22:45
  • yes, I convert it to hsv. But when I don't move hand it immediately become black. Moreover, some part of palm stay black, is it normal? – kuppy28 May 31 '17 at 22:57
  • Both observations are normal. The background's statistic model of your `BackgroundSubtractor` is updated at each frame. So if you show a static hand it will be considered as background after some time. some part of palm staying black is probably related to the explanation above. To fix the former, you need to use a `BackgroundSubtractor` with a static background's statistic model (that you would train only once with a picture of your background). Hope it makes sense :) – Elouarn Laine May 31 '17 at 23:24