Here are few examples:
- P.s stackoverflow sometimes doesnt allow to use >< characters so I will use } and { instead
.
// Char single channel matrix
Mat M1 = mat::ones(3,5,CV_8UC1);
int Val = M1{unsigned char}.at(2,3);
// int 16 matrix with 3 channels (like RGB pixel)
Mat M2 = mat::ones(3,5,CV_16UC(3));
__int16* Pix = &M2.at<__int16>(2,3);
Val = Pix[0] + Pix[1] + Pix[2];
// Example of how to find the size of each element in matrix
switch ( (M.dataend-M.datastart) / (M.cols*M.rows*M.channels())){
case sizeof(char):
printf("This matrix has 8 bit depth\n");
break;
case sizeof(double):
printf("This matrix has 64 bit depth\n");
break;
}
//Example of how to build dynamically a Matrix with desired depth
int inline CV_BUILD_MATRIX_TYPE(int elementSize, int nChannels){
// Determine type of the matrix
switch (elementSize){
case sizeof(char):
return CV_8UC(nChannels);
break;
case (2*sizeof(char)):
return CV_16UC(nChannels);
break;
case sizeof(float):
return CV_32FC(nChannels);
break;
case sizeof(double):
return CV_64FC(nChannels);
break;
}
return -1;
}
Mat M = Mat::zeros(2,3,CV_BUILD_MATRIX_TYPE(4,2));
// builds matrix with 2 channels where each channel has depth of 4 bytes (float or __int32). As you wish
CV_8UC1
is anunsigned char
. Just cast it. – B. Decoster