[英]OpenCV C++ to calculate vignette effect using gaussian kernel function
I try to convert this reply from python to C++ and I'm stuck on the first call to multiply... small is my input Mat with 2 dims and about 600x400 cols/rows. 我尝试将这个回复从python转换为C ++,我被困在第一个调用乘法的地方…… 小是我的输入Mat带有2个暗点和大约600x400列/行。
Mat a = getGaussianKernel(small.cols, .3);
Mat b = getGaussianKernel(small.rows, .3);
Mat ta;
transpose(a, ta);
Mat c = *new Mat(ta.rows, ta.cols, ta.type());
cv::multiply(ta, b, c);
Mat d;
cv::max(c, d);
d = c / d;
Mat e;
multiply(small, d, e);
The error message reads: 错误消息显示为:
The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function arithm_op
该操作既不是函数arithm_op中的“数组运算数组”(数组具有相同的大小和相同的通道数),也不是“数组运算标量”,也不是“标量运算数组”
I'm not familiar with either numpy or matplotlib but I'm constraint to C++ for reasons to lengthy to explain... 我对numpy或matplotlib都不熟悉,但由于冗长的解释原因,我对C ++有所限制。
If you read the documentation for cv::multiply
you will see that it expects the first two inputs to be the same size and type. 如果您阅读
cv::multiply
的文档 ,您会发现它期望前两个输入的大小和类型相同。 This is because it is attempting to do element-wise multiplication . 这是因为它正在尝试进行逐元素乘法 。
Your Gaussian kernel a
is defined to be 600 x 1 (which getGaussianKernel
recommends the first input to be odd ) so the transpose ( ta
) is going to be 1 x 600. 您的高斯核
a
定义为600 x 1( getGaussianKernel
建议将第一个输入设为奇数 ),因此转置( ta
)将为1 x 600。
The second input, b
is defined to be 400 x 1 (again, should really be an odd size). 第二个输入
b
定义为400 x 1(同样,实际上应该是奇数大小)。
Obviously these two are not the same size dimensions/size. 显然,这两个尺寸/尺寸不相同。
Instead of using cv::multiply
which is element-wise multiplication, you'll want to just use the *
operator which is the matrix product that you likely want. 而不是使用
cv::multiply
(逐元素乘法),您只需要使用*
运算符即可,它是您可能想要的矩阵乘积 。
Mat C = b * ta
Which should yield a 400 x 600 matrix. 这应该产生一个400 x 600的矩阵。
Also, I switched the order of b
and ta
so that the matrix dimensions worked out for multiplication. 另外,我切换了
b
和ta
的顺序,以便计算出矩阵尺寸以进行乘法运算。
Note you also have some other issues with your code. 请注意 ,您的代码还存在其他一些问题。 You're using
cv::max()
to compare an empty matrix with c
. 您正在使用
cv::max()
将空矩阵与c
进行比较。 You should likely use cv::minMaxLoc
. 您可能应该使用
cv::minMaxLoc
。
The multiplication on your last line looks like it should be fine since you want to apply the combination of Gaussians image directly to the input image on a pixel-by-pixel or element-by-element basis. 最后一行的乘法看起来应该没问题,因为您希望将高斯图像的组合直接按像素或逐个元素应用于输入图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.