【OpenCV 4】图像像素的位运算:与、或、非、异或

一、编程环境:

OpenCV 4.1.0
IDEVisual Studio 2017 Enterprise (15.9.13)
操作系统Windows 10 x64 中文专业版 (1903)

二、图像像素位运算:

  1. 与:bitwise_and()
  2. 或:bitwise_or()
  3. 非(取反):bitwise_not()
  4. 异或:bitwise_xor()

三、示例代码:

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

using namespace cv;
using namespace std;

int main(int argc, const char *argv[])
{
	//创建第1张矩形图
	Mat src1 = Mat::zeros(Size(300, 300), CV_8UC3);
	Rect rect(50, 50, 100, 100);
	src1(rect) = Scalar(0, 100, 100);
	imshow("1--图1", src1);
	

	//创建第2张矩形图(显示坐标与上一个错开)
	Mat src2 = Mat::zeros(Size(300, 300), CV_8UC3);
	rect.x = 100;
	rect.y = 100;
	src2(rect) = Scalar(0, 0, 255);
	imshow("2--图2", src2);

	//两张图像素逻辑操作
	Mat dst1, dst2, dst3;
	bitwise_or(src1, src2, dst1);
	bitwise_xor(src1, src2, dst2);
	bitwise_and(src1, src2, dst3);

	imshow("3--或(bitwise_or)", dst1);
	imshow("4--异或(bitwise_xor)", dst2);
	imshow("5--与(bitwise_and)", dst3);

	//使用实际图像演示像素取反
	Mat src = imread("../images/test.png");
	namedWindow("6--原图", WINDOW_AUTOSIZE);
	imshow("6--原图", src);

	//取反操作
	Mat dst;
	bitwise_not(src, dst);
	imshow("7--取反(bitwise_not)", dst);

	waitKey(0);
	return 0;
}

四、运行效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值