在OpenCV中,有一种快速创建Mat
对象的方法,其中:
例如 :
1 0 1 0 1 0
1 0 1 0 1 0
1 0 1 0 1 0
Run Code Online (Sandbox Code Playgroud)
模式总是一样的.大小Mat
可能很大,并且通过循环处理生成此模式的速度非常慢.
Mik*_*iki 10
OpenCV 重复就是这样的.
#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
int rows = 1000;
int cols = 1000;
vector<uchar> pattern = { 1, 0 }; // change with int, double, etc according to the type you want.
Mat m;
repeat(pattern, rows, cols/2, m);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
与其他方法比较
只是一个小测试来衡量建议(迄今为止)方法的性能:
以毫秒为单位的时间:
@Miki [repeat] : 0.442786
@RonaldoMessi [copyTo] : 7.26822
@Derman [merge] : 1.17588
Run Code Online (Sandbox Code Playgroud)
我用于测试的代码:
#include <opencv2\opencv.hpp>
#include <vector>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
int rows = 1000;
int cols = 1000;
{
// @Miki
double tic = (double)getTickCount();
vector<uchar> pattern = { 1, 0 };
Mat m1;
repeat(pattern, rows, cols / 2, m1);
double toc = ((double)getTickCount() - tic) * 1000 / getTickFrequency();
cout << "@Miki [repeat] \t\t: " << toc << endl;
}
{
// @RonaldoMessi
double tic = (double)getTickCount();
Mat m2(rows, cols, CV_8UC1);
Mat vZeros = Mat::zeros(rows, 1, CV_8UC1);
Mat vOnes = Mat::ones(rows, 1, CV_8UC1);
for (int i = 0; i < cols - 1; i += 2)
{
vOnes.col(0).copyTo(m2.col(i));
vZeros.col(0).copyTo(m2.col(i + 1));
}
double toc = ((double)getTickCount() - tic) * 1000 / getTickFrequency();
cout << "@RonaldoMessi [copyTo] \t: " << toc << endl;
}
{
// @Derman
// NOTE: corrected to give correct output
double tic = (double)getTickCount();
Mat myMat[2];
myMat[0] = cv::Mat::ones(rows, cols/2, CV_8UC1);
myMat[1] = cv::Mat::zeros(rows, cols/2, CV_8UC1);
Mat m3;
merge(myMat, 2, m3);
m3 = m3.reshape(1);
double toc = ((double)getTickCount() - tic) * 1000 / getTickFrequency();
cout << "@Derman [merge] \t: " << toc << endl;
}
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
679 次 |
最近记录: |