Ask Your Question
0

Handling both 16bit and 8bit images in the same program.

asked 2018-02-09 08:57:02 -0600

mmecchia gravatar image

Hello, here's my problem. I'm writing a program that shall handle both 8bit and 16 bit data. The problem is that every procedure to modify the data must use the template of the proper class. For example:

    img = cv::imread(path, CV_LOAD_IMAGE_ANYDEPTH);
    std::for_each(img.begin<?>(), img.end<?>(), cleanPixel);

? above being ushort in the case of 16bit and uchar in the case of 8bit data. I know that I can check the depth of the image using the method img.depth(), and check the equality with either CV_16U or CV_8U. Is that the only way? I was also thinking to use some preprocessor constant, and maybe compile two different version of the program (one only for 8bit images and one only for 16bit images).

Thanks in advance for your help.

edit retag flag offensive close merge delete

Comments

what do you want to do exactly ?

LBerger gravatar imageLBerger ( 2018-02-09 11:34:06 -0600 )edit

I want, if possible, avoid ifs every time I have to access the pixel data.

mmecchia gravatar imagemmecchia ( 2018-02-09 11:39:28 -0600 )edit

What does it mean img.begin ?

LBerger gravatar imageLBerger ( 2018-02-09 11:43:40 -0600 )edit

It's an iterator. Very useful things. In short, every time you do iterator++, it points at the next pixel, and *iterator gives you a reference to the pixel it points to. They're available in just about every STL or boost data type.

Tetragramm gravatar imageTetragramm ( 2018-02-09 17:51:32 -0600 )edit

@Tetragramm I didn't know iterator method begin in class Mat. Thanks. Never mind I don't think I will use it.

What is cleanPixel ?

LBerger gravatar imageLBerger ( 2018-02-10 00:21:42 -0600 )edit

The last parameter of std::for_each is the function you want to apply to each element of the range. So in this case it's their own function, which does... something.

Tetragramm gravatar imageTetragramm ( 2018-02-10 18:02:15 -0600 )edit

Yes I understand that "own function, which does... something." but I don't think @mmecchia need foreach to rewrite method like setTo , exp or 2 x+1...

LBerger gravatar imageLBerger ( 2018-02-11 03:41:06 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-02-09 17:45:11 -0600

Tetragramm gravatar image

updated 2018-02-09 17:47:57 -0600

Unfortunately, you have to use a if or a switch statement. ex:

switch(img.depth())
{
    case CV_8U:
        std::for_each(img.begin<uchar>(), img.end<uchar>(), cleanPixel);
       break;
   case CV_16U:
       ....
    default:
        //Print error or whatever
}

You can also use the built-in forEach HERE, but it still requires a template argument, and therefore an if/switch statement. However, it will parallellize your function, which is nice.

You can make a templated function for the whole processing chain and simply call that after the imread, giving you just one if/switch statement.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-02-09 08:56:11 -0600

Seen: 370 times

Last updated: Feb 09 '18