Assign bytes within of Mat with float values
I need assign values in M (a variable type Mat) with values CV_32FC1 (float), but is long time at size of 10000x10000. i.e:
for (i=0 ; i < rows; i++)
for (j=0 ; j < cols; j++)
...build variable NEW_VALUE for indexes i, j
M.at<float>(i,j) = NEW_VALUE
}
the code above required 1 second aprox. Other form I see, is defining a union (copy bytes):
typedef union{float _float; uchar _uchar[4];} Bits;
...
Bits bits;
float new_value;
for (i=0 ; i<rows; i++)
for (j=0 ; j<cols; j+=4){
...//build variable new_value for indexes i, j
bits._float = new_value;
M.data[i ** cols + j] = bits._uchar[0];
M.data[i ** cols + j+1] = bits._uchar[1];
M.data[i ** cols + j+2] = bits._uchar[2];
M.data[i ** cols + j+3] = bits._uchar[3];
}
That is much faster that first. But not working. I tried doing:
memcpy(&M.data[i ** cols + j], bits._uchar[0], 1);
memcpy(&M.data[i ** cols + j+1], bits._uchar[1], 1);
...
But not working. And:
memcpy(&M.at<float>(i,j), bits._uchar, 4);
is very slow also. I need to know how to copy the bytes of the new_value correctly within of M.
wait, what on gods earth are you doing there ? there's no ** operator in c++
imho, you just say : Mat M(h, w, CV_32F, Scalar(NEW_VALUE)); // to initialize it