I open an image using imread(), getting a cv::Mat instance.
I wish to apply a function on this matrix, by its underline pixel type.
I wish to know if there is a Dispatch() function in openCV?
Here is a code sample.
FindFeature(cv::Mat_<Vec3b>& image)
{
image.forEach<Vec3b>([](Vec3b &p, const int * position) -> void
{ p[0] /= 2; p[1] /= 2; p[2] /= 2;});
}
FindFeature(cv::Mat_<uchar>& image)
{
image.forEach<uchar>([](uchar &p, const int * position) -> void
{ p /= 2; });
}
template<typename Pixel>
void FindFeature(cv::Mat_<Pixel>& image)
{
// we don't know how to work on any other image type
throw std::runtime_exception("unsupported image type");
}
void main()
{
int flags = CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH
cv::Mat image = imread("c:\image.jpg", flags);
Dispatch(FindFeature, image);
}
Here you can see I handle differently grayscale and RGB images.