0
点赞
收藏
分享

微信扫一扫

opencv Mat 函数--CheckVector


学习opencv ,我们需要对opencv的函数有点了解 ,实现什么样子的功能,以及如何实现

下面我们一次来看checkVector 这个函数,

int org.opencv.core.Mat.checkVector(int elemChannels)

elemChannels  的取值

单通道

单通道:取值== 矩阵的列数

              返回结果:矩阵的行数

我们来验证一个这个理解是否正确

    Mat truth = new Mat(4, 10, CvType.CV_8UC1) {
{
put(0, 0, 01, 02, 03, 04, 05, 06, 07, 8, 9, 0, 01, 02, 03, 04, 05, 06, 07, 8, 9, 0);
put(1, 0, 11, 12, 13, 14, 15, 16, 17, 18, 19, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 10);
put(2, 0, 21, 22, 23, 24, 25, 26, 27, 28, 29, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 20);
put(3, 0, 31, 32, 33, 34, 35, 36, 37, 38, 39, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 30);

}
};
System.out.println(truth.checkVector(10));

opencv Mat 函数--CheckVector_取值

这样的一个单通道的矩阵 4*10   列数为10 elemChannel==10    ===》 truth.checkVector(10) 的结果为  4

那么我们可能会疑问 elemChannel的值可不可以为其他的值比如说:等于行数

尝试了一下:checkVector(4)===> -1

再来看这样的一个5*10的矩阵:

opencv Mat 函数--CheckVector_取值_02

这个是一个5*10的一个单通道矩阵 ,checkVector(10)=5

所以单通道的矩阵  n*m   checkVector(m)=n

那么对于多通道的呢,我们继续来看双通道的矩阵

双通道矩阵:

elemChannel==2

    Mat truth = new Mat(20, 1, CvType.CV_8UC2);    
System.out.println(truth.checkVector(2));// result:20


Mat truth = new Mat(1, 20, CvType.CV_8UC2);
System.out.println(truth.checkVector(2));// result:20

对于双通道矩阵,m*n  m和n 中必须有一个为 1的

如果m ==1 则 checkVector(2)==n

如果n ==1 则 checkVector(2)==m

否则 checkVector== -1

三通道矩阵:

elemChannel=3

    Mat truth = new Mat(1, 20, CvType.CV_8UC3);    
System.out.println(truth.checkVector(3));// result:20

Mat truth = new Mat(20, 1, CvType.CV_8UC3);
System.out.println(truth.checkVector(3));// result:20

对于双通道矩阵,m*n  m和n 中必须有一个为 1的

如果m ==1 则 checkVector(3)==n

如果n ==1 则 checkVector(3)==m

否则 checkVector== -1

多通道矩阵

    Mat truth = new Mat(20, 1, CvType.CV_8UC(10));    
System.out.println(truth.checkVector(10));// result:20

Mat truth = new Mat(1, 20, CvType.CV_8UC(10));
System.out.println(truth.checkVector(10));// result:20

类似双,三通道矩阵 elemChannel 的值为通道数  并且m*n中其中一个为1 

那么checkVector的值我们都知道了,opencv 设置这样的一个函数有什么作用

The official OpenCV's doc says:

​​cv::Mat::checkVector()​​ returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise

OpenCV considers some data types equivalent in case of some functions i.e. ​​objectPoints​​​ of ​​cv::solvePnP()​​ can be:

  • 1xN/Nx1 1-channel​​cv::Mat​
  • 3xN/Nx3 3-channel​​cv::Mat​
  • ​std::vector<cv::Point3f>​

With ​​checkVector​​ you can make sure that you are passing the correct representation of your data.

大概的意思是说,在opencv中有些函数是要求参数满足一些特定的形式,比如:​​solvePnP​​函数,checkVector 就是为了确保你传入的数据满足特定的形式

继续看:

 truth.checkVector(elemChannels, depth);

         elemChannel 通道元素数量

         depth 深度

Mat 的深度是类型决定的,不同的类型对应不同的深度,深度反应出图像颜色像素值

深度的值:

            CV_8U = 0,
            CV_8S = 1,
            CV_16U = 2,
            CV_16S = 3,
            CV_32S = 4,
            CV_32F = 5,
            CV_64F = 6,
            CV_16F = 7;

    Mat truth = new Mat(1, 20, CvType.CV_32FC2); 
// depth !=mat.depth()
System.out.println(truth.checkVector(2,5));//result 20

Mat truth = new Mat(1, 20, CvType.CV_32FC2);
// depth !=mat.depth()
System.out.println(truth.checkVector(2,0));//result -1

        truth.checkVector(int elemChannels, int depth, boolean requireContinuous);

               elemChannel 通道元素数量

               depth 深度

               requireContinuous  是否连续  (目前我知道的mat 经过submat 获取到的矩阵是不连续的,没有经过submat的矩阵是连续的)

通过上面的,我们可以看到的是checkVector 这个函数是为了检验mat 是否满足某些条件


int Mat::checkVector(int _elemChannels, int _depth, bool _requireContinuous) const
{
return data && (depth() == _depth || _depth <= 0) &&
(isContinuous() || !_requireContinuous) &&
((dims == 2 && (((rows == 1 || cols == 1) && channels() == _elemChannels) ||
(cols == _elemChannels && channels() == 1))) ||
(dims == 3 && channels() == 1 && size.p[2] == _elemChannels && (size.p[0] == 1 || size.p[1] == 1) &&
(isContinuous() || step.p[1] == step.p[2]*size.p[2])))
? (int)(total()*channels()/_elemChannels) : -1;
}

我们看checkVector的源码 也是如此,如果不满足条件则返回 -1 如果满足条件则返回一个正数

上面是我对checkVector这个函数的理解

希望对你有所帮助!

举报

相关推荐

0 条评论