How to convert CV_8UC1 to normal int type?
I'm having a problem with understanding the opencv types. I have matrix in CV_8UC1 type but how can I read value of elements in matrix?
I know that I have to use at method and then pas &l开发者_如何学运维t;here>
my type but what type is CV_8UC1? 8 unsigned bit single channel doesn't tell me much.
Can i do something like this: unsigned int a = mat->at(0,0);
From the OpenCV Reference Manual:
CV_8UC1 – unsigned 8-bit single-channel data; can be used for grayscale image or binary image – mask.
So it's just single channel 8 bit grey scale with values 0..255.
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
精彩评论