使用 Image.ptr<double>(0);在 OpenCV 中使用彩色图像

Using Image.ptr<double>(0); with color image in OpenCV

本文关键字:OpenCV 彩色图像 ptr Image lt double 使用 gt      更新时间:2023-10-16

我正在OpenCV(c++)中学习GrabCut,我坚持使用这一行

coefs = model.ptr<double>(0);
mean = coefs + componentsCount;
cov = mean + 3*componentsCount;

其中
模型是矩阵
coeffesmeancov是双*
componentsCount是一个常量整数

我想知道系数是多少?是RGB值还是其他什么?(以下代码的结果)

model.ptr<double>(0);

行中发生了什么

cov = mean + 3*componentsCount;

谢谢你的回答!!:)

Mat model不是彩色图像,而是高斯混合模型的模型。

GMM有以下私人成员:

Mat model;     
double* coefs; 
double* mean;  
double* cov;

model—单通道双矩阵(CV64FC1),1行,列数等于modelSize*componentsCount,其中

componentsCount = 5 // line 60
modelSize = 13 // 3/*mean*/ + 9/*covariance*/ + 1/*component weight*/    // line 89

该数据存储在具有以下布局的矩阵model中:

cw0, cw1, cw4, m00, m01, m02, m10, ..., m42, c00, c11, ...
^              ^                             ^
coefs = model.ptr<double>(0)                 ^
                                             mean + 3*componentsCount
               ^                             
               ^                            
               coefs + componentsCount

其中:

cw<i>    is the weight of the i-th component 
m<i><j>  is the j-th dimension mean of the i-th component
c<i><j>  is the j-th dimension covariance of the i-th component 

因此:

coefs = model.ptr<double>(0);   // points at the start of the weights into model
mean = coefs + componentsCount; // points at the start of the means into model
cov = mean + 3*componentsCount; // points at the start of the covariances into model

ptr<double>()只是在第0行的开头返回一个double*,即数据进入矩阵的开头。它相当于(double*)model.data

基本上,coefsmeancov只是将适当的数据放入model的快捷方式。