使用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
,*
andmultiply
work 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.dot
equalsnp.matmul
fornumpy.array
,np.matmul
meansmatrix multiplication
;
fornumpy.matrix
,*
andnp.matmul
meansmatrix 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]: ...
随机推荐
- SecureCRT安装包和破解脚本
第一步下载 SecureCRT安装包和破解脚本 下载 http://pan.baidu.com/s/1c1D5Ala 破解脚本 securecrt_mac_crack.pl安装包scrt-7.3.7- ...
- 【Visual Studio Code】插件
[Visual Studio Code]插件 转载:https://www.cnblogs.com/yangchongxing/p/10625628.html 目录 ================= ...
- Mysql数据库优化一:集群(读写分离)之主从服务器的安装与配置
Mysql数据库的集群(读写分离),说白了就是将读操作和写操作分开在不同的服务器上实现,以达到提高效率的目的. 大致原理如下: 数据库中的所有操作都是有日志记录的(前提是要打开这个日志记录功能) 1. ...
- 什么是cookie?什么是session?session和cookie有什么区别?
在技术面试中,经常被问到“说说Cookie和Session的区别”,大家都知道,Session是存储在服务器端的,Cookie是存储在客户端的,然而如果让你更详细地说明,你能说出几点?今天个推君就和大 ...
- doGet()方法和doPost()方法有什么区别?
1. 一般上,get是从服务器上获取数据,post是向服务器传送数据. 2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.pos ...
- ansible部署Tomcat
首先要准备的环境就是免密登录 这是要在ansible-playbook中所写的内容---- hosts: tomcat tasks: - name: 关闭防火墙 service: name ...
- Python 基础:入门必备知识
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:程序员野客 先看下咱们的基础目录1 标识符2 关键字3 引号4 编码5 ...
- Java生鲜电商平台-生鲜系统中商品订单系统售后系统设计
Java生鲜电商平台-生鲜系统中商品订单系统售后系统设计(服务订单履约系统) 说明: 电商之下,我们几乎能从电商平台上买到任何我们日常需要的商品,但是对于很多商品来说,用户购买发货后,只是整个交易流程 ...
- js简单动画:匀速动画、缓动动画、多物体动画以及透明度动画
主要实现以下几种简单的动画效果(其实原理基本相同): 1.匀速动画:物体的速度固定 2.缓动动画:物体速度逐渐变慢 3.多物体动画 4.透明度动画 效果实现: 1.匀速动画(以物体左右匀速运动为例) ...
- C# 扩展类与分布类
一.扩展类 //定义扩展方法 public static class ExtsionString { public static string GetTop10(this string value) ...