opencv background substraction

前端 未结 2 1666
鱼传尺愫
鱼传尺愫 2020-12-09 21:49

I have an image of the background scene and an image of the same scene with objects in front. Now I want to create a mask of the object in the foreground with background sub

2条回答
  •  不知归路
    2020-12-09 22:14

    I think when I'm doing it like this I get the right results: (in the YCrCb colorspace) but accessing each px is slow so I need to find another algorithm

        cv::Mat mask(image.rows, image.cols, CV_8U, cv::Scalar(0,0,0));
    
        cv::Mat_::const_iterator itImage= image.begin();
        cv::Mat_::const_iterator itend= image.end();
        cv::Mat_::iterator itRef= refRoi.begin();
        cv::Mat_::iterator itMask= mask.begin();
    
        for ( ; itImage!= itend; ++itImage, ++itRef, ++itMask) {
            int distance = abs((*itImage)[0]-(*itRef)[0])+
                            abs((*itImage)[1]-(*itRef)[1])+
                            abs((*itImage)[2]-(*itRef)[2]);
    
            if(distance < 30)
                *itMask = 0;
            else
                *itMask = 255;
        }
    

提交回复
热议问题