Opencv基础入门笔记04

Opencv基础入门笔记04:图像操作

今天复习图像操作,包括图像的取反,二值化,作与运算等!

涉及的函数(bitwise,bitwise_not,bitwise_and等)

1.简述
我们知道灰度图由0~255表示,0为黑,255为白,则从位操作的角度出发,纯黑色为0,不是纯黑色为1,所以在一些纯白色,或者纯黑色背景里,先转为灰度图,利用阈值将非背景色的内容(灰度值不是0和255)抠出来作为模板,再与原图做位操作,进行抠图。

(1)Cv2.bitwise_not(图片文件),将图片里像素值按位反向。

(2)Cv2.bitwise_and (目标文件,源文件,mask),将图片里的像素值按位与

(3)Cv2.add(目标文件,源文件),将图片里的像素值按位加

(4)Cv2.bitwise_xor (目标文件,源文件,mask),将图片里的像素值按位异或
举出一个函数的定义
CV_EXPORTS_W void bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask = noArray());
参数一:第一个输入数组或标量。(输入图片1)
参数二:第二个输入数组或一个标量。(输入图片2)
参数三:输出与输入具有相同大小和类型的数组。(输出图片)
参数四:操作掩码;8位单通道数组,用于指定要更改的输出数组的元素。
注意!!!上面的函数要求的两张图片深度,像素行列必须一致才能进行相加相减,而取反运算只要求输入一张图片,所以没有上面的要求。
2.两种方法实现以上操作
first:调用API.

int main() {
	//1.读取图片
	Mat src1_image = imread("E:\\360downloads\\hl.png");
	Mat src2_image = imread("E:\\360downloads\\1.jpg");
	//2.判断图片是否读入
	if ( (!src1_image.data) || (!src2_image.data)) {
		printf("can't load image ,please checkout your path!");
		return 0;
	}
	//2.二值化图片
	Mat dst1_image_not, dst2_image_not;
	Mat dst1,dst2;
	cout<<src1_image.rows<<" "<<src2_image.cols;
	cvtColor(src1_image, dst1, COLOR_BGR2GRAY);
	cvtColor(src2_image, dst2, COLOR_BGR2GRAY);
	//调用API实现图像取反运算
	bitwise_not(dst1,dst1_image_not);
	bitwise_not(dst2, dst2_image_not);
	imshow("src1_image", src1_image);
	imshow("src2_image", src2_image);
	imshow("dst1", dst1);
	imshow("dst2", dst2);
	imshow("dst1_image_not", dst1_image_not);
	imshow("dst2_image_not", dst2_image_not);
	waitKey(0);
	return 0;
}

在这里插入图片描述
second:C++不用API,对像素进行操作

int main() {
	//1.读取图片
	Mat src1_image = imread("E:\\360downloads\\hl.png");
	Mat src2_image = imread("E:\\360downloads\\1.jpg");
	//2.判断图片是否读入
	if ( (!src1_image.data) || (!src2_image.data)) {
		printf("can't load image ,please checkout your path!");
		return 0;
	}
	//2.二值化图片
	Mat dst1_image_not, dst2_image_not;
	Mat dst1,dst2;
	cout<<src1_image.rows<<" "<<src2_image.cols;
	cvtColor(src1_image, dst1, COLOR_BGR2GRAY);
	cvtColor(src2_image, dst2, COLOR_BGR2GRAY);
	//调用API实现图像取反运算
	//bitwise_not(dst1,dst1_image_not);
	//bitwise_not(dst2, dst2_image_not);
	int rows1 = dst1.rows;
	int cols1 = dst1.cols;
	imshow("dst1", dst1);
	imshow("dst2", dst2);
	for (int row1 = 0; row1< rows1; row1++) {
		for (int col1=0; col1< cols1; col1++) {
			int V1 = dst1.at<uchar>(row1, col1);
			dst1.at<uchar>(row1, col1) = 255 - V1;//反差处理
		}
	}
	int rows2 = dst2.rows;
	int cols2 = dst2.cols;
	for (int row2 = 0; row2 < rows2; row2++) {
		for (int col2=0; col2 < cols2; col2++) {
			int V2 = dst2.at<uchar>(row2, col2);
			dst2.at<uchar>(row2, col2) = 255 - V2;//反差处理
		}
	}
	dst1_image_not = dst1;
	dst2_image_not = dst2;
	imshow("src1_image", src1_image);
	imshow("src2_image", src2_image);
	
	imshow("dst1_image_not", dst1_image_not);
	imshow("dst2_image_not", dst2_image_not);
	waitKey(0);
	return 0;
}

在这里插入图片描述
以上!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨夜※繁华

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值