rcv
Rand's OpenCV Utilities
|
Classes | |
struct | rcv::type2cv< T > |
Get the CV_ flag for a given numeric type. More... | |
Defines | |
#define | RCV_DISPATCH(type, function_name,...) |
Dispatch an image to a function which takes the underlying image data type as a template parameter. | |
#define | RCV_DISPATCH_NO_RETURN(type, function_name,...) |
Dispatch an image to a function which takes the underlying image data type as a template parameter and has no return type. | |
Functions | |
std::string | rcv::type2string (int imgTypeInt) |
Convert a CV_* type (from cv::Mat.type()) to a human readable string. | |
double constexpr | rcv::white_value (int mat_type) |
Get the standard "white" value for a given OpenCV data type. | |
cv::Mat | rcv::convert_and_scale (cv::Mat const &image, int const rtype) |
Convert a cv::Mat, and scale it to the typical range for the destination type. |
Various utilities to help out with the inherent type unsafety of OpenCV
#define RCV_DISPATCH | ( | type, | |
function_name, | |||
... | |||
) |
[&]() { \ switch(CV_MAT_TYPE(type)) \ { \ case CV_8U: return function_name<uint8_t>(__VA_ARGS__); \ case CV_8S: return function_name<int8_t>(__VA_ARGS__); \ case CV_16U: return function_name<uint16_t>(__VA_ARGS__); \ case CV_16S: return function_name<int16_t>(__VA_ARGS__); \ case CV_32S: return function_name<int32_t>(__VA_ARGS__); \ case CV_32F: return function_name<float>(__VA_ARGS__); \ case CV_64F: return function_name<double>(__VA_ARGS__); \ default: throw std::runtime_error("Unsupported data type: " + rcv::type2string(type)); \ }; \ }()
Dispatch an image to a function which takes the underlying image data type as a template parameter.
This is helpful e.g. when you need to access the pixel values of an image, but you don't know that image's type. Using the .at() method requires a template parameter, so RCV_DISPATCH can be used to call the proper templated method.
Example:
template<class T> bool my_function(float param1, float param2, cv::Mat image, float param3) { assert(image.channels() == 1); return (image.at<T>(0,0) * param1 + param2 < param3); } cv::Mat my_unknown_matrix = getMatrixFromSomewhere(); bool result = RCV_DISPATCH(my_unknown_matrix.type(), my_function, 1.0, 2.0, my_unknown_matrix, 3.0);
#define RCV_DISPATCH_NO_RETURN | ( | type, | |
function_name, | |||
... | |||
) |
[&]() { \ switch(CV_MAT_TYPE(type)) \ { \ case CV_8U: function_name<uint8_t>(__VA_ARGS__); break; \ case CV_8S: function_name<int8_t>(__VA_ARGS__); break; \ case CV_16U: function_name<uint16_t>(__VA_ARGS__); break; \ case CV_16S: function_name<int16_t>(__VA_ARGS__); break; \ case CV_32S: function_name<int32_t>(__VA_ARGS__); break; \ case CV_32F: function_name<float>(__VA_ARGS__); break; \ case CV_64F: function_name<double>(__VA_ARGS__); break; \ default: throw std::runtime_error("Unsupported data type: " + rcv::type2string(type)); \ }; \ }()
Dispatch an image to a function which takes the underlying image data type as a template parameter and has no return type.
cv::Mat rcv::convert_and_scale | ( | cv::Mat const & | image, |
int const | rtype | ||
) |
Convert a cv::Mat, and scale it to the typical range for the destination type.
std::string rcv::type2string | ( | int | imgTypeInt | ) |
Convert a CV_* type (from cv::Mat.type()) to a human readable string.
For example:
cv::Mat mat = poorly_documented_function(); std::cout << "My matrix is of type: " << rcv::type2string(mat.type()) << std::endl;
double constexpr rcv::white_value | ( | int | mat_type | ) |
Get the standard "white" value for a given OpenCV data type.
For example, CV_8U images typically use 255 as their white value, while CV32F images typically use 1.0
double white = rcv::white_value(myMat.type());