Simple to way to declare an image for a unit test

For build some unit test on one of my function, I need to construct some basic image (cv::Mat size around 10x10, simple pattern) to call my function and check result.

I’m looking some good example on how to do it. I want to avoid to use imread, because image are very simple and small.

Is there any good example in opencv source for test to read ?

this has some examples of building a matrix from literal values in code. or maybe you just want to start from an all-zeros matrix and draw into it?

https://docs.opencv.org/3.4/d6/d6d/tutorial_mat_the_basic_image_container.html

OK, thanks for the link.

Is-it possible in C++, to do something similar to this :
(sorry, I have forgot to precise in C++)

cv::mat Image(5,5,CV_8U);
Image = [ [0, 0, 0, 0, 0],
          [0, 1, 1, 1, 0],
          [0, 1, 0, 1, 0],
          [0, 1, 1, 1, 0],
          [0, 0, 0, 0, 0] ];

?

For my unit test, I’m a looking for a light syntax to permit to reader to easy understand which basic pattern I want to test.

use a “typed” Mat_ , and make it:

cv::Mat_<uchar> Image(5,5);
Image << 0, 0, 0, 0, 0,
         0, 1, 1, 1, 0,
         0, 1, 0, 1, 0,
         0, 1, 1, 1, 0,
         0, 0, 0, 0, 0;
2 Likes

That is exactly what I expected. Thanks.

Just one more precision : image is also describe line per line.

So this example is a little more precise :

cv::Mat_<uchar> Image(5, 7);
Image << 0, 0, 0, 0, 0, 0, 0,
         0, 1, 1, 1, 1, 1, 0,
         0, 1, 0, 0, 0, 1, 0,
         0, 1, 1, 1, 1, 1, 0,
         0, 0, 0, 0, 0, 0, 0;