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 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;
}
How do you create sample? What are it's type, rows, columns, and channels?
My sample size is 3* 2088960. I joined three channels of my input image to create a sample size of 3*2088960, and 2088960 is the number of pixels in my image. it's type is also CV_32F.
Just to be sure, is that 3 rows, or 3 columns? It ought to be 3 columns.
yes it has three columns. The result is correct. It just took much time. I used similar sample in matlab. 146 seconds is really long for one frame of video.
I dunno. I'm seeing a number about 3 seconds per cluster. I'm afraid I don't know any more. Nothing obvious in the code you've linked, I copied that to get the 3 seconds per cluster.
Thanks for your reply. but it is really weird. what is your sample size? is this an easy image to cluster? my complete code has nothing special after removing other parts. It is as above.
Can you share the frame.jpg? I ran the code using a randu(frame, 0, 255) and it took 8.3 seconds.
I attached one frame of my video
8.3 seconds again. Are you running in Debug instead of Release? That's 58 seconds for me, which is within the range of possibilities for different computers.
Thanks for your comment. i was running it in debug mode. Then i changed it to release mode, but the running time has reduced less than 3 seconds. Why? is there any setting i do not know about? I am running matlab and c++ on the same laptop.