数值运算
代码:
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
|
# -*- coding=gbk -*- import cv2 as cv # 数值运算:加减乘除 def shu_image(src11, src22): src = cv.add(src11, src22) # 加 : 图像对应像素值相加,> 255 时取255 cv.namedwindow( "add" , 0 ) cv.resizewindow( "add" , 300 , 300 ) # 设置播放窗口长和宽 cv.imshow( "add" , src) src = cv.subtract(src11, src22) # 减 : 图像对应像素值相减,< 0 时取0 cv.namedwindow( "subtract" , 0 ) cv.resizewindow( "subtract" , 300 , 300 ) cv.imshow( "subtract" , src) src = cv.multiply(src11, src22) # 乘 : 图像对应像素值相乘,> 255 时取255 cv.namedwindow( "multiply" , 0 ) cv.resizewindow( "multiply" , 300 , 300 ) cv.imshow( "multiply" , src) src = cv.divide(src11, src22) # 除 : 图像对应像素值相除,a( > 0) / 0 = 255 cv.namedwindow( "divide" , 0 ) cv.resizewindow( "divide" , 300 , 300 ) cv.imshow( "divide" , src) src1 = cv.imread( "jueduisiwang.bmp" ) src2 = cv.imread( "fuchoutianshi.bmp" ) cv.namedwindow( "src1" , 0 ) cv.resizewindow( "src1" , 300 , 300 ) cv.imshow( "src1" , src1) src2 = src2[:,: 30 ,:] # 两张图片尺寸大小要一样,不然就需要调整 cv.namedwindow( "src2" , 0 ) cv.resizewindow( "src2" , 300 , 300 ) cv.imshow( "src2" , src2) shu_image(src1, src2) cv.waitkey( 0 ) cv.destroyallwindows() |
结果:
逻辑运算
代码:
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
|
# -*- coding=gbk -*- import cv2 as cv # 逻辑运算:与或非的操作 def luo_image(src1, src2): src = cv.bitwise_and(src1, src2) cv.namedwindow( "and" , 0 ) cv.resizewindow( "and" , 300 , 300 ) cv.imshow( "and" , src) src = cv.bitwise_or(src1, src2) cv.namedwindow( "or" , 0 ) cv.resizewindow( "or" , 300 , 300 ) cv.imshow( "or" , src) src = cv.bitwise_not(src1) cv.namedwindow( "nor" , 0 ) cv.resizewindow( "nor" , 300 , 300 ) cv.imshow( "nor" , src) src = cv.bitwise_xor(src1, src2) cv.namedwindow( "xor" , 0 ) cv.resizewindow( "xor" , 300 , 300 ) cv.imshow( "xor" , src) src1 = cv.imread( "jueduisiwang.bmp" ) src2 = cv.imread( "fuchoutianshi.bmp" ) cv.namedwindow( "src1" , 0 ) cv.resizewindow( "src1" , 300 , 300 ) cv.imshow( "src1" , src1) src2 = src2[:,: 30 ,:] # 两张图片尺寸大小要一样,不然就需要调整 cv.namedwindow( "src2" , 0 ) cv.resizewindow( "src2" , 300 , 300 ) cv.imshow( "src2" , src2) luo_image(src1, src2) cv.waitkey( 0 ) cv.destroyallwindows() |
结果(不太具有参考价值):
备注:
图像的逻辑或运算就是255-对应通道像素值,也就是“取反”操作(黑白图就是黑变白,白变黑)
黑白图和同尺寸任意图的逻辑或运算就是加法运算
黑白图和同尺寸任意图的逻辑与运算就是黑白图的黑白像素值互换然后与同尺寸任意图做加法运算
而彩色图之间的逻辑运算关系感觉太复杂了,没搞懂 : (
到此这篇关于浅谈python实现opencv之图片色素的数值运算和逻辑运算的文章就介绍到这了,更多相关图片色素的数值运算和逻辑运算内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_40338379/article/details/117886684