Ask Your Question

Revision history [back]

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++)<br=""> for (j=0 ; j<cols; j+="4){&lt;br"> ...//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.

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++)
j++) ...build variable NEW_VALUE for indexes i, j
j M.at<float>(i,j) = NEW_VALUE
}

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 bits;
bits; float new_value;
new_value; for (i=0 ; i<rows; i++)<br=""> i++) for (j=0 ; j<cols; j+="4){&lt;br"> j+=4){ ...//build variable new_value for indexes i, j
j bits._float = new_value;
M.data[i *
new_value; M.data[i ** cols + j] = bits._uchar[0];
M.data[i *
bits._uchar[0]; M.data[i ** cols + j+1] = bits._uchar[1];
M.data[i *
bits._uchar[1]; M.data[i ** cols + j+2] = bits._uchar[2];
M.data[i *
bits._uchar[2]; M.data[i ** cols + j+3] = bits._uchar[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);
1); memcpy(&M.data[i *
** cols + j+1], bits._uchar[1], 1);
...

1); ...

But not working. And:

memcpy(&M.at<float>(i,j), bits._uchar, 4);

4);

is very slow also. I need to know how to copy the bytes of the new_value correctly within of M.