Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

slow when calculate moment

Hi everyone, I try to write my own function to calc the moments as below:

double getMoment(Mat roi, uint8_t mode) { double mm = 0; for (int x = 0; x < roi.size().width; ++x) { for (int y = 0; y < roi.size().height; ++y) { switch (mode) { case 00: mm += roi.at<uchar>(y, x); break; case 01: mm += yroi.at<uchar>(y, x); break; case 10: mm += xroi.at<uchar>(y, x); break; case 11: mm += xyroi.at<uchar>(y, x); break; case 02: mm += yyroi.at<uchar>(y, x); break; case 20: mm += xxroi.at<uchar>(y, x); break; default: break; } } } return mm; }

My problem is my own function is quite slow than the moments of OpenCV library. So can you help me to improve my code to make it faster?

slow when calculate moment

Hi everyone, I try to write my own function to calc the moments as below:below: image description

double getMoment(Mat roi, uint8_t mode) { double mm = 0; for (int x = 0; x < roi.size().width; ++x) { for (int y = 0; y < roi.size().height; ++y) { switch (mode) { case 00: mm += roi.at<uchar>(y, x); break; case 01: mm += yroi.at<uchar>(y, x); break; case 10: mm += xroi.at<uchar>(y, x); break; case 11: mm += xyroi.at<uchar>(y, x); break; case 02: mm += yyroi.at<uchar>(y, x); break; case 20: mm += xxroi.at<uchar>(y, x); break; default: break; } } } return mm; }

My problem is my own function is quite slow than the moments of OpenCV library. So can you help me to improve my code to make it faster?

slow when calculate moment

Hi everyone, I try to write my own function to calc the moments as below: image description

double getMoment(Mat roi, uint8_t mode) { double mm = 0; for (int x = 0; x < roi.size().width; ++x) { for (int y = 0; y < roi.size().height; ++y) { switch (mode) { case 00: mm += roi.at<uchar>(y, x); break; case 01: mm += yroi.at<uchar>(y, x); break; case 10: mm += xroi.at<uchar>(y, x); break; case 11: mm += xyroi.at<uchar>(y, x); break; case 02: mm += yyroi.at<uchar>(y, x); break; case 20: mm += xxroi.at<uchar>(y, x); break; default: break; } } } return mm; }

My problem is my own function is quite slow slower than the moments of OpenCV library. So can you help me to improve my code to make it faster?

click to hide/show revision 4
No.4 Revision

updated 2016-11-29 09:18:59 -0600

berak gravatar image

slow when calculate moment

Hi everyone, I try to write my own function to calc the moments as below: image descriptionbelow:

double getMoment(Mat roi, uint8_t mode) {
 double mm = 0;
  for (int x = 0; x < roi.size().width; ++x) {
 for (int y = 0; y < roi.size().height; ++y) {
 switch (mode)
 {
  case 00:
  mm += roi.at<uchar>(y, x);
 break;
break;
 case 01:
 mm += yroi.at<uchar>(y, y*roi.at<uchar>(y, x);
 break;
break;
 case 10:
 mm += xroi.at<uchar>(y, x*roi.at<uchar>(y, x);
 break;
break;
 case 11:
 mm += xyroi.at<uchar>(y, x*y*roi.at<uchar>(y, x);
 break;
break;
 case 02:
 mm += yyroi.at<uchar>(y, y*y*roi.at<uchar>(y, x);
 break;
break;
 case 20:
 mm += xxroi.at<uchar>(y, x*x*roi.at<uchar>(y, x);
 break;
break;
 default:
 break;
break;
 }
 }
 }
  return mm;
 }

}

My problem is my own function is quite slower than the moments of OpenCV library. So can you help me to improve my code to make it faster?