如何在OpenCv c ++中将图像添加到图像中

时间:2017-06-14 15:33:09

标签: c++ image opencv add

我加载小图片。

 Mat extra;
 extra = imread("Korona.jpg");

我从相机加载图像,然后尝试添加图像。

VideoCapture cap;
Mat frame;
cap >> frame;
cv::Rect roi(cv::Point(0, 0), cv::Size(110, 110));
cv::Mat destinationROI = img(roi);
extra.copyTo(destinationROI(cv::Rect(0, 0, extra.cols, extra.rows)));

但没有成功并且有这个错误:

  

OpenCV错误:断言失败(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 cv :: Mat :: Mat,file   C:\构建\ master_winpack-集结Win64上-VCl 4 \的OpenCV \模块\芯\ SRC \ matrix.cpp,   第522行

有什么想法吗? THX。

1 个答案:

答案 0 :(得分:2)

if(roi.x >= 0 && roi.y >= 0 && roi.width + roi.x < input_frame.cols && roi.height + roi.y < input_frame.rows)
{
    // your code

}
else
    return -1;

发现类似问题并从herehere

中提取代码

查看您的代码,看起来您没有打开捕获

VideoCapture cap(0); //for a webcam
Mat frame;
if(cap.isOpened())
   cap >> frame;
else
   throw;
cv::Rect roi(cv::Point(0, 0), cv::Size(110, 110));
cv::Mat destinationROI = frame(roi);
extra.copyTo(destinationROI(cv::Rect(0, 0, extra.cols, extra.rows)));

在VideoCapture上查看OpenCV的documentation,确保您正确完成此操作。