惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
The Register - Security
The Register - Security
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美
罗磊的独立博客
U
Unit 42
S
SegmentFault 最新的问题
Y
Y Combinator Blog
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
Schneier on Security
Schneier on Security
Know Your Adversary
Know Your Adversary
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
博客园 - 叶小钗
C
Cybersecurity and Infrastructure Security Agency CISA
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Latest news
Latest news
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
S
Securelist
A
Arctic Wolf
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
Scott Helme
Scott Helme
博客园 - 聂微东
博客园 - 【当耐特】
T
Tenable Blog
I
Intezer
D
DataBreaches.Net
B
Blog RSS Feed
Security Latest
Security Latest
C
Cisco Blogs
T
Tor Project blog
N
Netflix TechBlog - Medium

博客园 - 凤凰_1

多边形近似:最小周长多边形算法MPP 多阈值otsu的opencv实现 在Opencv中自定义了一个相位函数,获取复数矩阵的相位 cvIsInf与cvIsNan函数 Mandelbrot set 以parallel_for_实现 opencv的并行操作parallel_for_的问题 CV_WRAP和CV_EXPORTS_W opencv超级像素示例 opencv中自定义的双线性二次插值的图像旋转及缩放 在Qt中,如何在sheets末端添加新的sheet opencv4.x 中的plot函数绘制二维Mat C++版DNN最简主体框架 C++版全连接神经网络 C++版加载MNIST图像集合 C++版的神经网络训练用的图像集合加载 c\c++宏定义,四个参数求最大值 - 凤凰_1 - 博客园 自定义c++二维动态数组 用Cmake 编译OpenCV常见的错误 Opencv中的dft()和idft()示例
C++仿Matlab的bsxfun函数
凤凰_1 · 2021-02-17 · via 博客园 - 凤凰_1

在Opencv中实现了Matlab的bsxfun函数,只实现了加法plus、减法minus,其它的太耗时,有感兴趣的朋友也来做做,分享分享。

bsxfun函数的用法参考链接:

https://blog.csdn.net/tina_ttl/article/details/51034773

#include <iostream>
#include<string>
#include<opencv2/opencv.hpp>

using namespace cv;
using namespace std;
//bsxfun函数的说明见链接:https://blog.csdn.net/tina_ttl/article/details/51034773
/*
 1. a,b都是单行,a、b的列数必须相同
 2. a,b都是单列,a、b的行数必须相同

 3. a是单行,b是单列,a的列数与b的行数可以不同。输出为b.rows-by-a.cols矩阵。
 4. a是单列,b是单行,a的行数与b的列数可以不同。输出为b.rows-by-a.cols矩阵。

 5. a是单行,b是多行多列或者单行多列,则如果a.cols==b.cols,输出结果为b.rows-by-a.cols矩阵
 6. a是单列,b是多行多列或者单列多行,则如果a.rows==b.rows,输出结果为b.rows-by-a.cols矩阵
*/
Mat bsxfun(string operation,const Mat& a,const Mat&b)
{

    Mat temp;
    Mat ta,tb;
    /*如果b的行数、列数有一个为1,
      并且a的行数或列数不等于1,二另一个尺度>1,
      则ta=b,tb=a
      否则,ta=a,tb=b;
*/
    bool swapSign;
    if(b.rows==1||b.cols==1)
    {
        ta=b.clone(); tb=a.clone();
        swapSign=1;
    }
    else
    {
        ta=a.clone();tb=b.clone();swapSign=0;
    }
    
    int arows=ta.rows,acols=ta.cols;
    int brows=tb.rows,bcols=tb.cols;
    int cols,rows;//a、b尺度不一致时,输出结果的尺度

    if(arows==1&&bcols==1)//3. a是行矢量,b是列矢量
    {
        cols=acols;
        rows=brows;
        repeat(ta,rows,1,ta);
        repeat(tb,1,cols,tb);
    }
   else if(acols==1&&brows==1)//4. a是列矢量,b是行矢量
    {
        cols=bcols;
        rows=arows;
        repeat(ta,1,cols,ta);
        repeat(tb,rows,1,tb);
    }
    else if(arows==1&&brows>1)//5. 列数必须相等
    {
        repeat(ta,tb.rows,1,ta);
    }
    else if(acols==1&&bcols>1)
    {
        repeat(ta,1,bcols,ta);
    }
    if(swapSign)
    {
        Mat t=ta;
        ta=tb; tb=t;
    }
    if(operation=="plus")  temp=ta+tb;
    if(operation=="minus") temp=ta-tb;
    return temp;
}

int main()
{
    Mat a(5,3,CV_8U,cv::Scalar::all(1));
    Mat b(1,3,CV_8U,cv::Scalar::all(3));
    Mat c=bsxfun("minus",a,b);
    cout<<"a="<<a<<endl;
    cout<<"b="<<b<<endl;
    cout<<c<<endl;

    return 0;
}

运行结果如下: