C++,OpenCV图像像素运算(6)
  Jk5625xsZPHl 2023年11月02日 48 0

参与算术运算图像的数据类型、通道数目、大小必须相同

算术运算

加法:add

void add(InputArray src1, InputArray src2, OutputArray dst,InputArray mask = noArray(), int dtype = -1);
/*******************************************************************
*			src1: 	输入图1
*			src2:	输入图2
*			dst:	输出图
*			mask:	遮罩像素  可有可无
*			dtype:	输出图深度 可有可无
*********************************************************************/

减法:subtract

void subtract(InputArray src1,InputArray src2,OutputArray dst,InputArray mask = noArray(),int dtype = -1);
/*******************************************************************
*			src1: 	输入图1
*			src2:	输入图2
*			dst:	输出图
*			mask:	遮罩像素  可有可无
*			dtype:	输出图深度 可有可无
*********************************************************************/

乘法:multiply

void multiply(InputArray src1, InputArray src2,OutputArray dst, double scale = 1, int dtype = -1);
/*******************************************************************
*			src1: 	输入图1
*			src2:	输入图2
*			dst:	输出图
*			scale:	缩放因子,即在src1*src2的基础上再乘scale
*			dtype:	输出图深度 可有可无
*********************************************************************/

除法:divide

void divide(InputArray src1, InputArray src2, OutputArray dst,double scale = 1, int dtype = -1);
/*******************************************************************
*			src1: 	输入图1
*			src2:	输入图2
*			dst:	输出图
*			scale:	缩放因子,即在src1*src2的基础上再乘scale
*			dtype:	输出图深度 可有可无
*********************************************************************/

混合运算:addWeighted

dst = α · img1 + β · img2 + γ

void addWeighted(InputArray s1,double a,InputArray s2,double b,double gamma,OutputArray d,int dtype = -1);
/*******************************************************************
*			s1: 	输入图1
*			a:		 输入图占比  α
*			s2:		输入图2
*			b:      输入图2占比  β
*			gamma:	附加值,结果基础上再加+gamma   γ  
*			dst:	输出图
*			dtype:	输出图深度 可有可无
*********************************************************************/

综合代码

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
class ImgOpreations 
{
public:
	ImgOpreations() :img1(imread("mm.jpg")), img2(imread("text.jpg")) 
	{
		result = Mat::zeros(img1.size(), img2.type());		//存储的图像一定要初始化
	}
	void testAdd(string wName="Add")
	{
		add(img1, img2, result);
		imshow(wName, result);
		waitKey(0);
	}
	void testSub(string wName="Sub")
	{
		subtract(img1, img2, result);
		imshow(wName, result);
		waitKey(0);
	}
	void testMul(string wName="Mul")
	{
		multiply(img1, img2, result);
		imshow(wName, result);
		waitKey(0);
	}
	void testDivide(string wName = "Div") 
	{
		divide(img1, img2, result);
		imshow(wName, result);
		waitKey(0);
	}
	//dst = α · img1 + β · img2 + γ  
	void testAddWeighted(string wName = "AddWeighted") 
	{
		addWeighted(img1, 0.6, img2, 0.4, 0, result);
		imshow(wName, result);
		waitKey(0);
	}

protected:
	Mat img1;
	Mat img2;
	Mat result;
};


int main() 
{
	ImgOpreations* pImg = new ImgOpreations;
	pImg->testAdd();
	pImg->testSub();
	pImg->testMul();
	pImg->testDivide();
	pImg->testAddWeighted();
	return 0;
}

C++,OpenCV图像像素运算(6)_#include

位运算

位与

void bitwise_and(InputArray src1, InputArray src2,,OutputArray dst, InputArray mask = noArray());
/*******************************************************************
*			src1: 	输入图1
*			src2:	输入图2
*			dst:	输出图
*			mask:	遮罩像素  可有可无
*********************************************************************/

位或

void bitwise_or(InputArray src1, InputArray src2, OutputArray dst, InputArray mask = noArray());
/*******************************************************************
*			src1: 	输入图1
*			src2:	输入图2
*			dst:	输出图
*			mask:	遮罩像素  可有可无
*********************************************************************/

非运算

void bitwise_not(InputArray src, OutputArray dst,InputArray mask = noArray());
/*******************************************************************
*			src1: 	输入图1
*			dst:	输出图
*			mask:	遮罩像素  可有可无
*********************************************************************/

异或运算

void bitwise_xor(InputArray src1, InputArray src2,OutputArray dst, InputArray mask = noArray());
/*******************************************************************
*			src1: 	输入图1
*			src2:	输入图2
*			dst:	输出图
*			mask:	遮罩像素  可有可无
*********************************************************************/

综合代码

图像位运算用图片

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
class BitWise 
{
public:
	BitWise() :img1(imread("mm.jpg")), img2(imread("text.jpg")) {}
	void Show() 
	{
		imshow("mm", img1);
		imshow("text", img2);
	}
	void BitOpreations() 
	{
		Mat myand, myor, myxor, mynot;
		bitwise_and(img1, img2, myand);
		bitwise_or(img1, img2, myor);
		bitwise_not(img1, mynot);
		bitwise_xor(img1, img2, myxor);
			
		imshow("and", myand);
		imshow("or", myor);
		imshow("xor", myxor);
		imshow("not", mynot);
		waitKey(0);
	}
protected:
	Mat img1;
	Mat img2;
};
int main() 
{
	BitWise* pImg = new BitWise;
	pImg->Show();
	pImg->BitOpreations();
	return 0;
}

C++,OpenCV图像像素运算(6)_C++_02

图像位运算用自绘图图形

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
class BitWise 
{
public:
	BitWise() :img1(Mat(200,200,CV_8UC1)), img2(Mat(200,200,CV_8UC1)) 
	{
		//Rect绘制矩形(左上角坐标 和绘制矩形宽度和高度)
		img1(Rect(50, 50, 100, 100)) = Scalar(255);
		img2(Rect(100, 100, 100, 100)) = Scalar(255);
	}
	void Show() 
	{
		imshow("mm", img1);
		imshow("text", img2);
		waitKey(0);
	}
	void BitOpreations() 
	{
		Mat myand, myor, myxor, mynot;
		bitwise_and(img1, img2, myand);
		bitwise_or(img1, img2, myor);
		bitwise_not(img1, mynot);
		bitwise_xor(img1, img2, myxor);
			
		imshow("and", myand);
		imshow("or", myor);
		imshow("xor", myxor);
		imshow("not", mynot);
		waitKey(0);
	}
protected:
	Mat img1;
	Mat img2;
};
int main() 
{
	BitWise* pImg = new BitWise;
	pImg->Show();
	pImg->BitOpreations();
	return 0;
}

C++,OpenCV图像像素运算(6)_ide_03

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  gBkHYLY8jvYd   2023年12月06日   50   0   0 #includecii++
  ZXL8ALkrtBPG   2023年12月06日   23   0   0 字面量c++
  gBkHYLY8jvYd   2023年12月10日   18   0   0 #include邻域灰度图像
  gBkHYLY8jvYd   2023年12月10日   22   0   0 #include数组i++
  gBkHYLY8jvYd   2023年12月06日   19   0   0 #includeios数据
  gBkHYLY8jvYd   2023年12月08日   20   0   0 #includecii++
  gBkHYLY8jvYd   2023年12月11日   20   0   0 cic++最小值
Jk5625xsZPHl