Is there a way to prevent rounding in opencv matrix divison

后端 未结 2 623
感动是毒
感动是毒 2021-01-25 00:52

I have an integer matrix and I want to perform an integer division on it. But opencv always rounds the result. I know I can divide each element manually but I want to know is th

相关标签:
2条回答
  • 2021-01-25 01:03

    Similar to @GPPK's optional method, you can hack it by:

    Mat tmp, dst;
    c.convertTo(tmp, CV_64F);
    tmp = tmp / 8 - 0.5;            // simulate to prevent rounding by -0.5
    tmp.convertTo(dst, CV_32S);
    
    cout << dst;
    
    0 讨论(0)
  • 2021-01-25 01:08

    The problem is with using ints, you cant have decimal points with ints so I'm not sure how you are expecting not to get rounding.

    You really have two options here, I do not think you can this without using one of these options:

    1. You have a mathematically correct int matrix division [10, 9, 8]
    2. You spin up your own divide function in order to give you the result you want.

    Option 2:

    Pseudocode:

    Create a double matrix
    perform the division to get the output [10.0, 8.875, 8.0]
    strip away any numbers after a decimal point [10.0, 8.0, 8.0]
    (optional) write these values back to a int matrix
    (result) [10, 8, 8]
    
    0 讨论(0)
提交回复
热议问题