使用opencv和numpy实现矩阵相乘和按元素相乘 matrix multiplication vs element-wise multiplication
本文首发于个人博客https://kezunlin.me/post/1e37a6/,欢迎阅读最新内容!
opencv and numpy matrix multiplication vs element-wise multiplication
Guide
opencv
Matrix multiplication is where two matrices are multiplied directly. This operation multiplies matrix A of size [a x b] with matrix B of size [b x c] to produce matrix C of size [a x c].
In OpenCV it is achieved using the simple * operator:
C = A * B // Aab * Bbc = Cac
Element-wise multiplication is where each pixel in the output matrix is formed by multiplying that pixel in matrix A by its corresponding entry in matrix B. The input matrices should be the same size, and the output will be the same size as well. This is achieved using the mul() function:
output = A.mul(B); // A B must have same size !!!
code
cv::Mat cv_matmul(const cv::Mat& A, const cv::Mat& B)
{
// matrix multipication m*k, k*n ===> m*n
cv::Mat C = A * B;
return C;
}
cv::Mat cv_mul(const cv::Mat& image, const cv::Mat& mask)
{
// element-wise multiplication output[i,j] = image[i,j] * mask[i,j]
cv::Mat output = image.mul(mask, 1.0); // m*n, m*n
return output;
}
cv::Mat cv_multiply3x1(const cv::Mat& mat3, const cv::Mat& mat1)
{
std::vector<cv::Mat> channels;
cv::split(mat3, channels);
std::vector<cv::Mat> result_channels;
for(int i = 0; i < channels.size(); i++)
{
result_channels.push_back(channels[i].mul(mat1));
}
cv::Mat result3;
cv::merge(result_channels, result3);
return result3;
}
cv::Mat cv_multiply3x3(const cv::Mat& mat3_a, const cv::Mat& mat3_b)
{
cv::Mat a;
cv::Mat b;
cv::Mat c;
std::vector<cv::Mat> a_channels;
std::vector<cv::Mat> b_channels;
std::vector<cv::Mat> c_channels;
cv::split(mat3_a, a_channels);
cv::split(mat3_b, b_channels);
for(int i = 0; i < a_channels.size() || b_channels.size(); i++)
{
c_channels.push_back(a_channels[i].mul(b_channels[i]));
}
cv::merge(c_channels, c);
return c;
}
numpy
numpy arrays are not matrices, and the standard operations
*, +, -, /work element-wise on arrays.
Instead, you could try using
numpy.matrix, and*will be treated likematrix multiplication.
code
Element-wise multiplication code
>>> img = np.array([1,2,3,4,5,6,7,8]).reshape(2,4)
>>> mask = np.array([1,1,1,1,0,0,0,0]).reshape(2,4)
>>> img * mask
array([[1, 2, 3, 4],
[0, 0, 0, 0]])
>>>
>>> np.multiply(img, mask)
array([[1, 2, 3, 4],
[0, 0, 0, 0]])
for
numpy.array,*andmultiplywork element-wise
matrix multiplication code
>>> a = np.array([1,2,3,4,5,6,7,8]).reshape(2,4)
>>> b = np.array([1,1,1,1,0,0,0,0]).reshape(4,2)
>>> np.matmul(a,b)
array([[ 3, 3],
[11, 11]])
>>> np.dot(a,b)
array([[ 3, 3],
[11, 11]])
>>> a = np.matrix([1,2,3,4,5,6,7,8]).reshape(2,4)
>>> b = np.matrix([1,1,1,1,0,0,0,0]).reshape(4,2)
>>> a
matrix([[1, 2, 3, 4],
[5, 6, 7, 8]])
>>> b
matrix([[1, 1],
[1, 1],
[0, 0],
[0, 0]])
>>> a*b
matrix([[ 3, 3],
[11, 11]])
>>> np.matmul(a,b)
matrix([[ 3, 3],
[11, 11]])
for 2-dim,
np.dotequalsnp.matmul
fornumpy.array,np.matmulmeansmatrix multiplication;
fornumpy.matrix,*andnp.matmulmeansmatrix multiplication;
Reference
History
- 20190109: created.
Copyright
- Post author: kezunlin
- Post link: https://kezunlin.me/post/4bc343c9/
- Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
使用opencv和numpy实现矩阵相乘和按元素相乘 matrix multiplication vs element-wise multiplication的更多相关文章
- opencv、numpy中矩阵转置,矩阵内的固定位置相应的坐标变换
opencv.numpy中矩阵转置,矩阵内的固定位置相应的坐标变换
- 用 opencv和numpy进行图片和字符串互转,并保存至 json
用 opencv和numpy进行图片和字符串互转,并保存至 json 转至 https://zhuanlan.zhihu.com/p/27349847 受 用 base64 进行图片和字符串互转,并保 ...
- Python numpy中矩阵的用法总结
关于Python Numpy库基础知识请参考博文:https://www.cnblogs.com/wj-1314/p/9722794.html Python矩阵的基本用法 mat()函数将目标数据的类 ...
- numpy中矩阵乘法,星乘(*)和点乘(.dot)的区别
import numpy a = numpy.array([[,], [,]]) b = numpy.array([[,], [,]]) 星乘表示矩阵内各对应位置相乘,矩阵a*b下标(0,0)=矩阵a ...
- [转]Numpy中矩阵对象(matrix)
numpy模块中的矩阵对象为numpy.matrix,包括矩阵数据的处理,矩阵的计算,以及基本的统计功能,转置,可逆性等等,包括对复数的处理,均在matrix对象中. class numpy.matr ...
- numpy创建矩阵常用方法
numpy创建矩阵常用方法 arange+reshape in: n = np.arange(0, 30, 2)# start at 0 count up by 2, stop before 30 n ...
- 编程计算2×3阶矩阵A和3×2阶矩阵B之积C。 矩阵相乘的基本方法是: 矩阵A的第i行的所有元素同矩阵B第j列的元素对应相乘, 并把相乘的结果相加,最终得到的值就是矩阵C的第i行第j列的值。 要求: (1)从键盘分别输入矩阵A和B, 输出乘积矩阵C (2) **输入提示信息为: 输入矩阵A之前提示:"Input 2*3 matrix a:\n" 输入矩阵B之前提示
编程计算2×3阶矩阵A和3×2阶矩阵B之积C. 矩阵相乘的基本方法是: 矩阵A的第i行的所有元素同矩阵B第j列的元素对应相乘, 并把相乘的结果相加,最终得到的值就是矩阵C的第i行第j列的值. 要求: ...
- Numpy中矩阵和数组的区别
矩阵(Matrix)和数组(Array)的区别主要有以下两点: 矩阵只能为2维的,而数组可以是任意维度的. 矩阵和数组在数学运算上会有不同的结构. 代码展示 1.矩阵的创建 采用mat函数创建矩阵 c ...
- numpy的通用函数:快速的元素级数组函数
通用函数(ufunc)是对ndarray中的数据执行元素级运算的函数.可看作简单函数的矢量化包装. 一元ufunc sqrt对数组中的所有元素开平方 exp对数组中的所有元素求指数 In [93]: ...
随机推荐
- 【Maven】聚合
[Maven]聚合 转载: 使用聚合一次能为多个 maven 项目执行命令,而不用到每一个项目下去执行命令. 聚合 pom 的特殊之处 1.packaging 配置 pom <packaging ...
- ceph安装
使用ceph-deploy部署部署版本ceph-luminous 一,下载安装包,制作yum源,yum将挂载到192.168.100.100 (1)下载安装包 mkdir ceph-luminou ...
- python的tqdm模块介绍
https://www.jianshu.com/p/b27318efdb7b Tqdm 是 Python 进度条库,可以在 Python 长循环中添加一个进度提示信息用法:tqdm(iterator) ...
- CouchDB学习-介绍
官方文档 CouchDB 1文档存储 CouchDB服务器主机是一个存储文档的数据库.每一个文档在数据库中都有唯一的名字.CouchDB提供RESTful HTTP API用来读取和更新(添加,编辑, ...
- DevExpress的GridControl的使用以及怎样添加列和绑定数据源
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...
- C++ lambda expression
Emerged since c++11, lambda expression/function is an unnamed function object capable of capturing v ...
- JUC-6-Callable接口
创建线程的方式 不能有返回值,且不能声明抛出异常 ...
- Retrofit-草稿
1.GSONFormat 2.动态代理 https://www.cnblogs.com/maohuidong/p/7992894.html retrofit的核心就是动态代理
- .Net Core组件化视图(部分视图)
.Net Core组件化视图(部分视图) 1.背景 1.以前我们使用.Net的时候使用部分视图的方式在,.Net Core 中能在单独处理逻辑的部分视图没有了,但是我们还是想使用现在的.Net Cor ...
- How to: Use the Entity Framework Code First in XAF 如何:在 XAF 中使用EF CodeFirst
This topic demonstrates how to create a simple XAF application with a business model in a DbContext ...