Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Print OpenCV single channel Mat
void _PrintMatrix(char *pMessage, cv::Mat &mat)
{
printf("%s\n", pMessage);
for (int r = 0; r < mat.rows; r++) {
for (int c = 0; c < mat.cols; c++) {
switch (mat.depth())
{
case CV_8U:
{
printf("%*u ", 3, mat.at<uchar>(r, c));
break;
}
case CV_8S:
{
printf("%*hhd ", 4, mat.at<schar>(r, c));
break;
}
case CV_16U:
{
printf("%*hu ", 5, mat.at<ushort>(r, c));
break;
}
case CV_16S:
{
printf("%*hd ", 6, mat.at<short>(r, c));
break;
}
case CV_32S:
{
printf("%*d ", 6, mat.at<int>(r, c));
break;
}
case CV_32F:
{
printf("%*.4f ", 10, mat.at<float>(r, c));
break;
}
case CV_64F:
{
printf("%*.4f ", 10, mat.at<double>(r, c));
break;
}
}
} printf("\n");
} printf("\n");
}
int main(void){
Mat a = Mat(3, 3, CV_8U, Scalar(255));
_PrintMatrix("CV_8U", a);
a = Mat(3, 3, CV_8S, Scalar(-127));
_PrintMatrix("CV_8S", a);
a = Mat(3, 3, CV_16U, Scalar(65535));
_PrintMatrix("CV_16U", a);
a = Mat(3, 3, CV_16S, Scalar(-32768));
_PrintMatrix("CV_16S", a);
a = Mat::eye(3, 3, CV_32S);
_PrintMatrix("CV_32S", a);
a = Mat::eye(3, 3, CV_32F);
_PrintMatrix("CV_32F", a);
a = Mat::eye(3, 3, CV_64F);
_PrintMatrix("CV_64F", a);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment