opencv seamlessClone() 报错

版权声明:原创文章,转载请标明来源 https://blog.csdn.net/ngy321/article/details/89454546

在调用seamlessClone()的时候报错:error: (-215:Assertion failed) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function 'Mat'

出错代码:

seamlessClone(src, dst, mask, center, output, NORMAL_CLONE);

报错原因:可以看seamlessClone源码(opencv/modules/photo/src/seamless_cloning.cpp),在执行seamlessClone的时候,会先求mask内物体的boundingRect,然后会把这个最小框矩形复制到dst上,矩形中心对齐center,这个过程中可能矩形会超出dst的边界范围,就会报上面的roi边界错误

避免报错可以加判断:

Rect roi_s = boundingRect(mask);
Rect roi_d(center.x - roi_s.width / 2, center.y - roi_s.height / 2, roi_s.width, roi_s.height);

if(0 <= roi_d.x && 0 <= roi_d.width && roi_d.x + roi_d.width <= dst.cols
     && 0 <= roi_d.y && 0 <= roi_d.height && roi_d.y + roi_d.height <= dst.rows)
{
    seamlessClone(src, dst, mask, center, output, NORMAL_CLONE);
}

猜你喜欢

转载自blog.csdn.net/ngy321/article/details/89454546
今日推荐