今天照着例程学习了一下,但是自己编的时候出现了出错误。
代码如下:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;int main(int argc, char *argv[])
{Mat sourceImage;Mat imageROI,imageROI1,imageROI2;Mat logo=imread("dota_logo.jpg");imshow("the logo",logo);vector<Mat> channels;sourceImage=imread("1.jpg");// imshow("the source",sourceImage);split(sourceImage,channels);imageROI=channels.at(0);imshow("ROI",imageROI);imageROI1=channels.at(1);imshow("ROI1",imageROI1);addWeighted(imageROI(Rect(15,20,logo.cols,logo.rows)),1.0,logo,0.5,0,imageROI(Rect(15,20,logo.cols,logo.rows)));merge(channels,sourceImage);imshow("the picture",sourceImage);waitKey(0);return 0;
}
经过一步步注释(没办法,人太笨),发现是主要的添加函数有问题:
addWeighted(imageROI(Rect(15,20,logo.cols,logo.rows)),1.0,logo,0.5,0,imageROI(Rect(15,20,logo.cols,logo.rows)));
其中logo是我想要叠加的图片,imageROI是单通道的图片。
得到如下错误:
OpenCV Error:
Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array')
搞了很久不知道原因,在网上查了一下,瞬间滴汗,原来是图片格式不对。
因为在上面的代码里面,我的imageROI是单通道的图像,而logo是jpg格式的,三通道,所以二者的格式大小必须一样。
修改如下:
Mat logo=imread("dota_logo.jpg");//将logo也变为灰度图Mat logoGray=Mat(logo.rows,logo.cols,CV_8UC1);cvtColor(logo,logoGray,CV_BGR2GRAY);//修改addWeighted,将logo变为logoGrayaddWeighted(imageROI(Rect(15,20,logo.cols,logo.rows)),1.0,logoGray,0.5,0,imageROI(Rect(15,20,logo.cols,logo.rows)));
运行一看,不错!