原文:http://blog.csdn.net/mjlsuccess/article/details/12400787 //#include "blending.h" #include <opencv2/core.hpp> //#include <iostream> #include <opencv2/opencv.hpp> #include <QString> using namespace std; using namespace cv; void blending_test() { Mat src1, src2, dst; double alpha = 0.5; double beta = 1-alpha; src1 = imread("11.jpg"); src2 = imread("12.jpg"); if(!src1.data) cout<<"error loading src1"<<endl; if(!src2.data) cout<<"Error loading src2"<<endl; addWeighted(src1, alpha, src2, beta, 0.0, dst); imshow("output1", dst); // waitKey(0); } //C语言自己实现 void blending() { Mat src1, src2, dst; double alpha = 0.5; double beta = 1-alpha; double gama = 0; src1 = imread("11.jpg"); src2 = imread("12.jpg"); //判断两幅图片是否相同 CV_Assert(src1.depth() == CV_8U); CV_Assert(src1.depth() == src2.depth()); CV_Assert(src1.size() == src2.size()); //为dst申请内存 dst.create(src1.size(), src1.type()); const int nChannels = src1.channels(); if(!src1.data) cout<<"error loading src1"<<endl; if(!src2.data) cout<<"Error loading src2"<<endl; for (int i=0; i<src1.rows; i++) { const uchar* src1_ptr = src1.ptr<uchar>(i); const uchar* src2_ptr = src2.ptr<uchar>(i); uchar* dst_ptr = dst.ptr<uchar>(i); for (int j=0; j<src1.cols*nChannels; j++) { dst_ptr[j] = src1_ptr[j]*alpha + src2_ptr[j]*beta + gama; } } imshow("output2",dst); } //下面是main函数部分 int main(int argc, char* argv[]) { double t; t = (double)getTickCount(); blending_test(); t = 1000*((double)getTickCount() - t)/getTickFrequency(); printf("opencv time: %f ms\n",t); cout<<"opencv time:"<<t<<endl; t = (double)getTickCount(); blending(); t = 1000*((double)getTickCount() - t)/getTickFrequency(); cout<<"c language time:"<<t<<"ms"<<endl; cvWaitKey(0); return 0; } imread()需要0.7ms左右 imshow特别需要时间,几百
addWeighted需要时间是2ms,连续三次需要3ms
但是c语言遍历版的需要3ms