OpenCV4入门教程019:像素逻辑操作

索引地址:系列索引

根据上一篇的算术操作,OpenCV的像素操作就是简单的赋值,而赋的值就是算术操作的结果。赋值的对象就是像素对象或者像素的某一个像素的某一个通道。

逻辑运算包括:&|~!&&||等等。

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(){
Mat src = Mat(3,3,CV_8UC1,Scalar(100,100,100));//创建空白对象
cout<<src<<endl;

//图像的宽高
int nCol = src.cols;
int nRow = src.rows;

Mat temp1 = ~src;//图像取反
cout<<temp1<<endl;

Mat temp2 = src&0x0f;//图像与操作
cout<<temp2<<endl;

Mat temp3=src|0x0f;
cout<<temp3<<endl;

Mat temp4=src;
for(int i=0;i<nCol;i++){
for(int j=0;j<nRow;j++){
temp4.at<uchar>(i,j)=temp4.at<uchar>(i,j)&&0x0f;//像素并操作
}
}
cout<<temp4<<endl;

// Mat temp5=src&&0x0f;
// cout<<temp5<<endl;

Mat temp6=src;
for(int i=0;i<nCol;i++){
for(int j=0;j<nRow;j++){
temp6.at<uchar>(i,j)=temp6.at<uchar>(i,j)||0x80;
}
}
cout<<temp6<<endl;

// Mat temp7=src||0x80;
// cout<<temp7<<endl;

Mat temp8=src;
for(int i=0;i<nCol;i++){
for(int j=0;j<nRow;j++){
temp8.at<uchar>(i,j)=!temp8.at<uchar>(i,j);
}
}
cout<<temp8<<endl;

// Mat temp9=!src;
// cout<<temp9<<endl;

return 0;
}

测试输出为:

1
2
3
4
5
6
7
[100, 100, 100; 100, 100, 100; 100, 100, 100]
[155, 155, 155; 155, 155, 155; 155, 155, 155]
[ 4, 4, 4; 4, 4, 4; 4, 4, 4]
[111, 111, 111; 111, 111, 111; 111, 111, 111]
[ 1, 1, 1; 1, 1, 1; 1, 1, 1]
[ 1, 1, 1; 1, 1, 1; 1, 1, 1]
[ 0, 0, 0; 0, 0, 0; 0, 0, 0]

&|~直接在Mat对象操作,默认是对每个像素进行操作。
&& || ~需要我们对每个像素进行操作。


OpenCV4入门教程019:像素逻辑操作
https://feater.top/opencv/opencv-pixel-logical-operation/
作者
JackeyLea
发布于
2020年6月7日
许可协议