1

I want to divide a matrix cv::Mat src of type CV_8UC1 with a scalar of type float and the result stored in a new matrix srcN of type CV_32FC1: I'm doing this at the moment:

for(unsigned j=0;j<src.rows;j++)
    for(unsigned i=0;i<src.cols;i++)
        srcN.at<float>(j,i) = ((int) src.at<uchar>(j,i))/A;

Which is not very fast. I want to do it like this: srcN=src/A; but I don't get the right values in srcN. Is there any way to do that?

Another question: MATLAB (which literally means MATrix LABoratory) is very fast in operations with matrices, how can I make my code as fast as matlab with c++/opencv?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Ja_cpp
  • 2,426
  • 7
  • 27
  • 49
  • 1
    [This is why MATLAB is very fast in matrix operations](https://stackoverflow.com/questions/6058139/why-is-matlab-so-fast-in-matrix-multiplication). They use BLAS/MAGMA Fortran libraries. – Adriaan Jul 04 '17 at 08:02
  • @DanMašek I think it's the same !? the issue is with uchar in src which should be int before dividing or multiplying – Ja_cpp Jul 04 '17 at 12:00
  • 1
    @ROS_OPENCV Oh, right, that was irrelevant -- for some reason I though there wasn't an overload for Mat/scalar. | The cast to `int` in your loop body is redundant -- since `A` is float, the other operand of the division is implicitly converted to float. So, what you really want is `srcN = cv::Mat_(src) / A;` – Dan Mašek Jul 04 '17 at 12:25
  • @DanMašek Thanks that what I was looking for.. However it does take the same time as with loops – Ja_cpp Jul 04 '17 at 13:15
  • @ROS_OPENCV Thinking about this more, what you're actually doing is converting the `Mat` to a different type, while scaling at the same time. This is exactly what `convertTo` can do. `src.convertTo(srcN, CV_32FC1, (1 / A));` – Dan Mašek Jul 04 '17 at 20:17

0 Answers0