Why can't I modify an image by setting ROI (OPENCV)

Source: Internet
Author: User
Tags assert

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.