0

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.

ShaulF
  • 841
  • 10
  • 17
  • 1
    No, but you can load the image with `IMREAD_COLOR`, and it will always be CV_8UC3. Or you can just switch over the image `type` – Miki Nov 04 '15 at 19:21
  • It will be CV_8UC3, even if the picture is 255 gray scale? – ShaulF Nov 04 '15 at 20:20
  • Yes, the 3 channels will be equal. – Miki Nov 04 '15 at 20:21
  • You should probably better explain what you want to do inside `FindFeature`. because there are a few solution to this. – Miki Nov 04 '15 at 20:22
  • At the end, I need to process the image, and its all boil down to iterate the pixels. To do it in an efficient manner, I have to work with `cv:Mat_` so I have the actual type. this is what I get inside these handlers. – ShaulF Nov 04 '15 at 22:10
  • I edited the question to make it clear, the difference between the image types, and their handlers. – ShaulF Nov 04 '15 at 22:45
  • Have a look [here](http://stackoverflow.com/a/33154287/5008845) – Miki Nov 05 '15 at 10:36

0 Answers0