sleeping_dragon
sleeping_dragon

Reputation: 711

Why is the size and values of this OpenCV Matrix incorrect?

I use the following function to return a weight matrix of values between 0 and 1 depending on how close to the center a particular matrix position is. Also after a threshold the values are all 1 (it is like plateau with the points closer to center having value 1 and points away from center after a threshold of distance linearly fall from 1 to 0 at the edges)?

cv::Mat2f getWeightsMatrix(int N, int M, float r){
    cv::Mat2f weights = cv::Mat2f(N,M);
    int i,j;
    for(i=0;i<N;i++){
        for(j=0;j<M;j++){
            if(i<=floor(N*(1-r)/2)){
                if(j<=floor(M*(1-r)/2)){
                    weights[i][j]=((float)(i/N-j/M)/(1-r));
                }
                else{
                    weights[i][j]=(2*(float)(i/N)/(1-r));
                }
            }
            else if (i>=floor(N*(1+r)/2)){
                if(j>=floor(M*(1+r)/2)){
                    weights[i][j]=(((float)((N-i)/N))-((float)((M-j)/M)))/(1-r);
                }
                else{
                    weights[i][j]=(2*(float)((N-i)/N)/(1-r));
                }
            }
            else{
                if(j<=floor(M*(1-r)/2)){
                    weights[i][j]=(2*(float)(j/M)/(1-r));
                }
                else if(j>=floor(M*(1+r)/2)){
                    weights[i][j]=(2*(float)((M-j)/M)/(1-r));
                }
                else{
                    weights[i][j]=1;
                }
            }
        }
    }
    cout << weights << endl;
    return weights;
}

Now my problem is that I am having some casting issues and only values 0 and 1 are being returned (no floats). Also my matrix size displayed by the cout is 20x10 when I call the function with N=10, M=10 and r=0.5.

Please helP!

EDIT: This is the output

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Upvotes: 0

Views: 342

Answers (1)

alestanis
alestanis

Reputation: 21863

Your matrix consists only if integers because when you do basic operations on integers, the results gets rounded automatically.

For example: when you write

weights[i][j]=((float)(i/N-j/M)/(1-r));

The result of i/N is rounded to a integer, j/M is also rounded to an integer, and finally, the division by (1-r) is also rounded. Your (float) cast is a good idea but it's applied too late.

You can do several things:

  • Cast inside elementary operations, for instance, float(i)/N instead of i/N
  • Use float numbers instead of integers: write 1.0 instead of 1
  • Use floats inside your loops and statements

For example:

for(float i = 0; i < N-0.5; i++) {
    for(float j = 0; j < M-0.5; j++) {
        if(i <= floor(N*(1.0-r)/2.0)) {
            // ...

It's important you understand that because of floating point precision, a test such as i < N might or might not pass when float i = N. This is why I did a little trick by substracting 0.5 from your loop bounds N and M.

Upvotes: 4

Related Questions