Ask Your Question
3

Convert between depth values and depth types

asked 2013-03-21 07:28:01 -0600

Adi gravatar image

Does OpenCV have some compile-time way to convert between depth values and depth types?
cv::Mat has several template based methods like at<> which require a type at compile-time.
OpenCV usually uses depth types as values, e.g. CV_8U and CV_32f.
Such a utility comes in handy when using such methods to provide generic implementations.

I wrote my own, but was wondering, is there already such a tool?

Just in case, here's my code:

opencv_depth_to_type.h:

#ifndef opencv_depth_to_type_h__
#define opencv_depth_to_type_h__

#include <cv.h>

template <int Depth>
struct DepthToType
{};

// Template specializations for: CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F
template <> struct DepthToType<CV_8U > { typedef unsigned char   depth_type; };
template <> struct DepthToType<CV_8S > { typedef signed char     depth_type; };
template <> struct DepthToType<CV_16U> { typedef unsigned short  depth_type; };
template <> struct DepthToType<CV_16S> { typedef signed short    depth_type; };
template <> struct DepthToType<CV_32S> { typedef signed int      depth_type; };
template <> struct DepthToType<CV_32F> { typedef float           depth_type; };
template <> struct DepthToType<CV_64F> { typedef double          depth_type; };

#endif // opencv_depth_to_type_h__

You use it like this:

template <int Depth> // Depth is a depth value known AT COMPILE-TIME 
void myTemplateFunc()
{
   // At RUN time, allocate image of correct type using the VALUE Depth
   cv::Mat img(cv::Size(10,10), CV_MAKETYPE(Depth,1));

   // ...

   // Access pixel with correct and generic depth - specified at COMPILE time 
   // (must use typename to specify the type name depth_type)
   img.at< typename DepthToType<Depth>::depth_type >(cv::Point(5,5)) = 7;
   // ...
}
edit retag flag offensive close merge delete

Comments

It is, just forgot its name...

sammy gravatar imagesammy ( 2013-03-21 07:47:27 -0600 )edit

Looking at @Vladislav Vinogradov's answer below this can be extended to support channel number as well by using Vecxy specializations.

Adi gravatar imageAdi ( 2013-03-21 08:31:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-03-21 07:54:32 -0600

Vladislav Vinogradov gravatar image

OpenCV can convert types to depth values via cv::DataType<T>::type

template <typename T>
void myTemplateFunc()
{
    // At RUN time, allocate image of correct type using the VALUE Depth
    cv::Mat img(cv::Size(10,10), cv::DataType<T>::type);

    // ...

    // Access pixel with correct and generic depth - specified at COMPILE time 
    img.at<T>(cv::Point(5,5)) = img.at<T>(cv::Point(1,1));
   // ...
}

myTemplateFunc<uchar>();     // CV_8U
myTemplateFunc<cv::Vec3b>(); // CV_8UC3
myTemplateFunc<cv::Size>();  // CV_32SC2
edit flag offensive delete link more

Comments

Interesting. I didn't know this function either. Nice example ;-)
However, it is the opposite of my question...

Adi gravatar imageAdi ( 2013-03-21 08:23:15 -0600 )edit

Question Tools

Stats

Asked: 2013-03-21 07:28:01 -0600

Seen: 4,189 times

Last updated: Dec 03 '13