Home > Back-end >  What data type should I use in cv::Mat.at<data_type> when using CV_8SC3, CV_16FC3, CV_16FC1?
What data type should I use in cv::Mat.at<data_type> when using CV_8SC3, CV_16FC3, CV_16FC1?

Time:01-21

When looing through the image and to index the (i, j) , OpenCV provides the method at in cv::Mat. In order to get the correct value of that pixel we need to specify data type carefully ,or we might get some unexpected value.

For example, if you use CV_16SC3 to create cv::Mat like below :

cv::Mat img(h, w, CV_16SC3, cv::Scalar(-32, -64, -64));

When indexing the value you should use cv::Vec3s like :

img.at<cv::Vec3s>(i, j)[c]

If you use soem other type like img.at<cv::Vec3f> you would get some wrong value.

Now we know the importance of filling right data type, the table below is all I know what data type to put in to get correct value .

    CV_8UC3  -> cv::Vec3b   CV_8UC1  -> uchar 
    CV_8SC3                 CV_8SC1  -> char
    CV_16UC3 -> cv::Vec3w   CV_16UC1 -> ushort
    CV_16SC3 -> cv::Vec3s   CV_16SC1 -> short  
    CV_32SC3 -> cv::Vec3i   CV_32SC3 -> int

    CV_64FC3 -> cv::Vec3d   CV_64FC1 -> double
    CV_32FC3 -> cv::Vec3f   CV_32FC1 -> float
    CV_16FC3 ->             CV_16FC1 -> 

I would like to know for CV_8SC3, CV_16FC3, CV_16FC1, what data type should I write in order to get the correct value ? Thanks in advance !

CodePudding user response:

You can define your own specialization on your demand. cv::Vecxx are just a bunch of shorter aliases for for the most popular specializations of Vec<T,n>

So:

  • for CV_8SC3, you might use cv::Vec<char, 3>,
  • for CV_16FC3, you might use cv::Vec<cv::float16_t, 3>.
  • for CV_16FC1, you might use cv::Vec<cv::float16_t, 1>.

Besides, if you just want to iterate through a cv::Mat, there are actually many ways to do so, for example, the at method have this overloaded version:

template<typename _Tp >
const _Tp& cv::Mat::at (int i0, int i1, int i2) const

And i hope this official document will help you more: https://docs.opencv.org/4.x/db/da5/tutorial_how_to_scan_images.html

  •  Tags:  
  • Related