Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why is opencv slower than Matlab in clustering?

Hi, I have some codes in Matlab and i want to speedup the code by reimplementing that in opencv and c++. I tested clustering one image of my video by kmeans and EM. I was surprised when the speed for kmeans in matlab was two times faster. I also tested the speed for EM. It took 5 seconds in Matlab while 146 seconds in opencv to cluster my image. Is that reasonable since i heard opencv c++ is much faster than matlab? My code to test EM speed in opencv is as follows. The sample also includes image pixels in three dimensional feature space. Is there any way to speedup the code?

        clock_t init, telap;
        init = clock();
        Ptr<EM> em_model = EM::create();
        em_model->setClustersNumber(N);
        em_model->setCovarianceMatrixType(EM::COV_MAT_SPHERICAL);
        em_model->setTermCriteria(TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 300, 1));
        em_model->trainEM(sample, noArray(), labels, noArray()); 
        telap = clock() - init;

Why is opencv slower than Matlab in clustering?

Hi, I have some codes in Matlab and i want to speedup the code by reimplementing that in opencv and c++. I tested clustering one image of my video by kmeans and EM. I was surprised when the speed for kmeans in matlab was two times faster. I also tested the speed for EM. It took 5 seconds in Matlab while 146 seconds in opencv to cluster my image. Is that reasonable since i heard opencv c++ is much faster than matlab? My code to test EM speed in opencv is as follows. The sample also includes image pixels in three dimensional feature space. Is there any way to speedup the code?

 int main() {
Mat frame = imread("frame.jpg");
Mat sample;
sample = frame.reshape(1, frame.rows*frame.cols);
sample.convertTo(sample, CV_32F);
Mat labels, centers;
clock_t init, telap;
        init; init = clock();
 Ptr<EM> em_model = EM::create();
        em_model->setClustersNumber(N);
        em_model->setCovarianceMatrixType(EM::COV_MAT_SPHERICAL);
        em_model->setTermCriteria(TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, em_model->setClustersNumber(2);
em_model->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 300, 1));
        1)); 
em_model->trainEM(sample, noArray(), labels, noArray()); 
        telap = clock() cout << "time:" << (double)(clock() - init;
init) / ((double)CLOCKS_PER_SEC) << endl;
waitKey(0);
return 0;

}

Why is opencv slower than Matlab in clustering?

Hi, I have some codes in Matlab and i want to speedup the code by reimplementing that in opencv and c++. I tested clustering one image of my video by kmeans and EM. I was surprised when the speed for kmeans in matlab was two times faster. I also tested the speed for EM. It took 5 seconds in Matlab while 146 seconds in opencv to cluster my image. Is that reasonable since i heard opencv c++ is much faster than matlab? My code to test EM speed in opencv is as follows. The sample also includes image pixels in three dimensional feature space. Is there any way to speedup the code?

int main() {
Mat frame = imread("frame.jpg");
Mat sample;
sample = frame.reshape(1, frame.rows*frame.cols);
sample.convertTo(sample, CV_32F);
Mat labels, centers;
clock_t init; init = clock();
Ptr<EM> em_model = EM::create();
em_model->setClustersNumber(2);
em_model->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 300, 1)); 
em_model->trainEM(sample, noArray(), labels, noArray()); 
cout << "time:" << (double)(clock() - init) / ((double)CLOCKS_PER_SEC) << endl;
waitKey(0);
return 0;
    }

}

Why is opencv slower than Matlab in clustering?

Hi, I have some codes in Matlab and i want to speedup the code by reimplementing that in opencv and c++. I tested clustering one image of my video by kmeans and EM. I was surprised when the speed for kmeans in matlab was two times faster. I also tested the speed for EM. It took 5 seconds in Matlab while 146 seconds in opencv to cluster my image. Is that reasonable since i heard opencv c++ is much faster than matlab? My code to test EM speed in opencv is as follows. The sample also includes image pixels in three dimensional feature space. Is there any way to speedup the code?

int main() {
Mat frame = imread("frame.jpg");
Mat sample;
sample = frame.reshape(1, frame.rows*frame.cols);
sample.convertTo(sample, CV_32F);
Mat labels, centers;
clock_t init; init = clock();
Ptr<EM> em_model = EM::create();
em_model->setClustersNumber(2);
em_model->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 300, 1)); 
em_model->trainEM(sample, noArray(), labels, noArray()); 
cout << "time:" << (double)(clock() - init) / ((double)CLOCKS_PER_SEC) << endl;
waitKey(0);
return 0;
    }
}![image description](/upfiles/15287164516525771.jpg)

Why is opencv slower than Matlab in clustering?

Hi, I have some codes in Matlab and i want to speedup the code by reimplementing that in opencv and c++. I tested clustering one image of my video by kmeans and EM. I was surprised when the speed for kmeans in matlab was two times faster. I also tested the speed for EM. It took 5 seconds in Matlab while 146 seconds in opencv to cluster my image. Is that reasonable since i heard opencv c++ is much faster than matlab? My code to test EM speed in opencv is as follows. The sample also includes image pixels in three dimensional feature space. Is there any way to speedup the code?

int main() {
Mat frame = imread("frame.jpg");
Mat sample;
sample = frame.reshape(1, frame.rows*frame.cols);
sample.convertTo(sample, CV_32F);
Mat labels, centers;
clock_t init; init = clock();
Ptr<EM> em_model = EM::create();
em_model->setClustersNumber(2);
em_model->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 300, 1)); 
em_model->trainEM(sample, noArray(), labels, noArray()); 
cout << "time:" << (double)(clock() - init) / ((double)CLOCKS_PER_SEC) << endl;
waitKey(0);
return 0;
    }![image description](/upfiles/15287164516525771.jpg)
description](/upfiles/15[287164516525771.j](/upfiles/15287165358108362.jpg)pg)

Why is opencv slower than Matlab in clustering?

Hi, I have some codes in Matlab and i want to speedup the code by reimplementing that in opencv and c++. I tested clustering one image of my video by kmeans and EM. I was surprised when the speed for kmeans in matlab was two times faster. I also tested the speed for EM. It took 5 seconds in Matlab while 146 seconds in opencv to cluster my image. Is that reasonable since i heard opencv c++ is much faster than matlab? My code to test EM speed in opencv is as follows. The sample also includes image pixels in three dimensional feature space. Is there any way to speedup the code?

int main() {
Mat frame = imread("frame.jpg");
Mat sample;
sample = frame.reshape(1, frame.rows*frame.cols);
sample.convertTo(sample, CV_32F);
Mat labels, centers;
clock_t init; init = clock();
Ptr<EM> em_model = EM::create();
em_model->setClustersNumber(2);
em_model->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 300, 1)); 
em_model->trainEM(sample, noArray(), labels, noArray()); 
cout << "time:" << (double)(clock() - init) / ((double)CLOCKS_PER_SEC) << endl;
waitKey(0);
return 0;
    }![image description](/upfiles/15[287164516525771.j](/upfiles/15287165358108362.jpg)pg)
}

C:\fakepath\frame.jpg

Why is opencv slower than Matlab in clustering?

Hi, I have some codes in Matlab and i want to speedup the code by reimplementing that in opencv and c++. I tested clustering one image of my video by kmeans and EM. I was surprised when the speed for kmeans in matlab was two times faster. I also tested the speed for EM. It took 5 seconds in Matlab while 146 100 seconds in opencv to cluster my image. Is that reasonable since i heard opencv c++ is much faster than matlab? My code to test EM speed in opencv is as follows. The sample also includes image pixels in three dimensional feature space. Is there any way to speedup the code?

int main() {
Mat frame = imread("frame.jpg");
Mat sample;
sample = frame.reshape(1, frame.rows*frame.cols);
sample.convertTo(sample, CV_32F);
Mat labels, centers;
clock_t init; init = clock();
Ptr<EM> em_model = EM::create();
em_model->setClustersNumber(2);
em_model->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 300, 1)); 
em_model->trainEM(sample, noArray(), labels, noArray()); 
cout << "time:" << (double)(clock() - init) / ((double)CLOCKS_PER_SEC) << endl;
waitKey(0);
return 0;
    }

C:\fakepath\frame.jpg