针对C ++类型测试opencv mat数据类型的最佳方法是什么? [英] What's the best way to test opencv mat datatype against c++ types?

查看:102
本文介绍了针对C ++类型测试opencv mat数据类型的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在尝试自己在opencv lib中实现一些功能,例如将RGB图像转换为灰度图像.

I am now trying to implement some functions in opencv lib by my self, such that convert an rgb image to gray scale.

为了使该函数尽可能通用,我希望它能够处理打开的cv中的所有数据类型(CV_8U,CV_8S,... CV64F),即,输出图像数据类型应与输入匹配图像数据类型.这涉及到定义一些具有保存类型作为输入图像数据类型的指针.

In order to make the function as general as possible, I would to let it be able to handle all the datatypes in open cv (CV_8U, CV_8S, ... CV64F), i.e., the output image datatype should match the input image datatype. This involves defining some pointers that have the save type as the input image datatype.

我了解找出输入的数据类型并初始化相应的指针无法在运行时完成,因此我必须使用某种开关来在代码中确定它.

I understand that figuring out datatype of input and initializing the corresponding pointer cannot be done in runtime, so I have to use some sort of switch to determining it in the code.

在代码中,我定义了一个模板功能t_convert_RGB_to_Gray<T>(...)用于执行实际工作.然后,我有了一个多功能convert_RGB_to_Gray(...),它能够确定数据类型并调用正确的转换函数.

In the code, I have a template function t_convert_RGB_to_Gray<T>(...) defined for do the actual job. Then I have a poly-function convert_RGB_to_Gray(...) to be able to determine the data type and call the right conversion function.

我目前有以下两种数据类型测试方法:

I currently have the following two approaches for the datatype test:

方法1

     switch(image.depth())
    {
        case CV_8U: gray = t_convert_RGB_to_Gray<unsigned char>(image, coeff_R, coeff_G, coeff_B); break;
        case CV_8S: gray = t_convert_RGB_to_Gray<char>(image, coeff_R, coeff_G, coeff_B); break;
        case CV_16U:gray = t_convert_RGB_to_Gray<uint16_t>(image, coeff_R, coeff_G, coeff_B); break;
        case CV_16S:gray = t_convert_RGB_to_Gray<int16_t>(image, coeff_R, coeff_G, coeff_B); break;
        case CV_32S:gray = t_convert_RGB_to_Gray<int32_t>(image, coeff_R, coeff_G, coeff_B); break;
        case CV_32F:gray = t_convert_RGB_to_Gray<float>(image, coeff_R, coeff_G, coeff_B); break;
        case CV_64F:gray = t_convert_RGB_to_Gray<double>(image, coeff_R, coeff_G, coeff_B); break;
        default:    gray = t_convert_RGB_to_Gray<unsigned char>(image, coeff_R, coeff_G, coeff_B); break;
    }

方法2

if (cv::DataType<unsigned char>::depth == image.depth())
{
    // variation 1.
    //gray = t_convert_RGB_to_Gray<unsigned char>(image, coeff_R, coeff_G, coeff_B);
    // variation 2.
    gray = t_convert_RGB_to_Gray<cv::DataType<unsigned char>::channel_type>(image, coeff_R, coeff_G, coeff_B);
}
else if
 ....
}

我的问题是,这样做有什么缺点吗?有什么更好的办法吗?

My question is, is there any draw back of doing this? Is there any better way for this?

谢谢!

推荐答案

要完成Miki的答案,您可以执行与第一种方法类似的操作:

To complete Miki's answer, you can do something similar to your first approach:

switch(image.depth()) {
  case DataType<unsigned char>::type:
    gray = t_convert_RGB_to_Gray<unsigned char>(image, coeff_R, coeff_G, coeff_B);
    break;
  case DataType<int>::type:
    ...
}

这篇关于针对C ++类型测试opencv mat数据类型的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆