使用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]: ...
随机推荐
- node-sass下载失败
在angular项目中下载依赖npm install时提示node-sass安装失败,解决方法如下: 1.下载win32-x64-57_binding.node文件至指定目录 2.添加环境变量: 变量 ...
- 《一头扎进》系列之Python+Selenium框架设计篇3- 价值好几K的框架,狼来了,狼来了....,狼没来,框架真的来了
1. 简介 前边宏哥一边一边的喊框架,就如同一边一边的喊狼来了!狼来了!.....这回是狼没有来,框架真的来了.从本文开始宏哥将会一步一步介绍,如何从无到有地创建自己的第一个自动化测试框架.这一篇,我 ...
- C语言每日一练——第3题
一.题目要求 程序功能:计算100以内满足以下条件的所有整数i的个数cnt以及这些i之和sum.条件:i, i+4 ,i+10都是素数,同时i+10小于100.最后电影函数writeDAT()函数把结 ...
- 在 ASP.NET Core 中使用 AutoMapper 使 Entity 和 Resource 之间进行映射
目录 从 NuGet 安装 AutoMapper 添加 Entity类 和 Resource类 添加一个 Profile文件,配置映射关系 在Startup中对AutoMapper进行注册 在项目中使 ...
- NLP标注工具brat 配置文件说明
快速搭建brat 通过docker: docker run --name=brat -d -p 38080:80 -e BRAT_USERNAME=brat -e BRAT_PASSWORD=brat ...
- 一起学Spring之Web基础篇
概述 在日常的开发中Web项目集成Spring框架,已经越来越重要,而Spring框架已经成为web开发的主流框架之一.本文主要讲解Java开发Web项目集成Spring框架的简单使用,以及使用Spr ...
- 前后端分离及Element的使用
1. 前后端分离 1.1 什么是前后端分离 在传统的web应用开发中,大多数的程序员会将浏览器作为前后端的分界线.将浏览器中为用户进行页面展示的部分称之为前端,而将运行在服务器,为前端提供业务逻辑和数 ...
- 【代码审计】ESPCMSP8(易思企业建站管理系统)漏洞报告
0x00简介 项目名称:ESPCMS-P8(易思企业建站管理系统) 测试平台:Windwos 版本信息:P8.19082801稳定版 更新时间:2019-08-30 00:56:32 网站官网:htt ...
- sql server的简单分页
--显示前条数据 select top(4) * from students; --pageSize:每页显示的条数 --pageNow:当前页 )) sno from students); --带条 ...
- Sqlite—查询语句(Select)
基本语法如下 sqlite> select * from tb_user; sqlite> select userid,username from tb_user; 格式化的查询输出 sq ...