A small feature in the project that needs to implement a background substitution for a specific area of the video, write down a function like this:
void Bgreplace (mat& dst, MAT&BG, Rect rec)
{
assert (Dst.size () ==bg.size ());
ASSERT (dst.depth () = = Bg.depth ());
DST (REC) = BG (rec). Clone ();//Can be deep and shallow
}
function, use rec to specify the area to be replaced, replacing DST's value for that region with the value of this zone for BG.
But, unexpectedly, this function did nothing, did not achieve the expected, as I think, all the operation of the ROI area is the direct operation of the source image, because the two are sharing the same block of memory area Ah, and I also used a deep copy, however, I really think more.
For specific reasons, we can break the program down and look at:
void Bgreplace (mat& dst, MAT&BG, Rect rec)
{
assert (Dst.size () ==bg.size ());//1
assert (dst.depth () = = Bg.depth ());//2
Mat tmp=dst (rec);//3
tmp= BG (REC);//Can be deep and shallow//4
}
After the execution of the 3 sentences, TMP and DST share the same block of data, but at the end of the 4 sentences, TMP is sharing the same block of data with BG instead of DST, where the matrix header information is changed only once, and the original data is not changed.
The modified program is:
void Bgreplace (mat& dst, MAT&BG, Rect rec)
{
assert (Dst.size () ==bg.size ());
ASSERT (dst.depth () = = Bg.depth ());
BG (REC). CopyTo (DST);//Can be deep and shallow
}
The problem is simple, because you made a mistake.