opencv-图像算术运算

参考:

1、http://docs.opencv.org/3.3.0/  官方文档api

2、http://docs.opencv.org/3.3.0/d6/d00/tutorial_py_root.html 官方英文教程

3、https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html

4、https://github.com/makelove/OpenCV-Python-Tutorial# 进阶教程

5、https://docs.opencv.org/3.3.0/index.html  官方英文教程

6、https://github.com/abidrahmank/OpenCV2-Python-Tutorials

7、https://www.learnopencv.com/

8、http://answers.opencv.org/questions/ OpenCV论坛


注:安装的版本 opencv_python-3.3.0-cp36-cp36m-win_amd64.whl



参考:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html



图像算术运算

目标

  • Learn several arithmetic operations on images like addition, subtraction, bitwise operations etc.
  • You will learn these functions : cv2.add()cv2.addWeighted() etc.

图像加法

You can add two images by OpenCV function,  cv2.add()  or simply by numpy operation, res = img1 + img2 . Both images should be of same depth and type(两张图像的形状和数据类型要一致), or second image can just be a scalar value.

import cv2
import numpy as np

# uint8 取值范围 0~255
x = np.uint8([250])
y = np.uint8([10])

print (cv2.add(x,y)) # 250+10 = 260 => 255
# [[255]]

print (x+y)          # 250+10 = 260 % 256 = 4
# [4]

import cv2
import numpy as np
import matplotlib.pyplot as plt

img1=cv2.imread('lenna.png',1)
img2=cv2.imread('messi5.jpg',1)

# 调整尺寸 保证2张图像形状一致
res1 = cv2.resize(img1,(256, 256), interpolation = cv2.INTER_CUBIC)
res2 = cv2.resize(img2,(256, 256), interpolation = cv2.INTER_CUBIC)

res=cv2.add(res1,res2)


plt.subplot(131),plt.imshow(res1,'gray'),plt.title('res1')
plt.subplot(132),plt.imshow(res2,'gray'),plt.title('res2')
plt.subplot(133),plt.imshow(res,'gray'),plt.title('res')

plt.show()



图像混合

Images are added as per the equation below:

g(x) = (1 - \alpha)f_{0}(x) + \alpha f_{1}(x)

Here I took two images to blend them together. First image is given a weight of 0.7 and second image is given 0.3. cv2.addWeighted() applies following equation on the image.

dst = \alpha \cdot img1 + \beta \cdot img2 + \gamma

Here \gamma is taken as zero.

import cv2
import numpy as np
import matplotlib.pyplot as plt

img1=cv2.imread('lenna.png',1)
img2=cv2.imread('messi5.jpg',1)

# 调整尺寸 保证2张图像形状一致
res1 = cv2.resize(img1,(256, 256), interpolation = cv2.INTER_CUBIC)
res2 = cv2.resize(img2,(256, 256), interpolation = cv2.INTER_CUBIC)

res=cv2.add(res1,res2)
dst = cv2.addWeighted(res1,0.7,res2,0.3,0)


plt.subplot(141),plt.imshow(res1,'gray'),plt.title('res1')
plt.subplot(142),plt.imshow(res2,'gray'),plt.title('res2')
plt.subplot(143),plt.imshow(res,'gray'),plt.title('res')
plt.subplot(144),plt.imshow(dst,'gray'),plt.title('dst')

plt.show()




按位操作

This includes bitwise AND, OR, NOT and XOR operations.


import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load two images
img1 = cv2.imread('messi5.jpg')
img2 = cv2.imread('opencv_logo.png')

# I want to put logo on top-left corner, So I create a ROI
img2 = cv2.resize(img2,(200, 200), interpolation = cv2.INTER_CUBIC)
rows,cols,channels = img2.shape
roi = img1[0:rows, 0:cols ]

# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst

cv2.imshow('res',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()


or

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load two images
img1 = cv2.imread('messi5.jpg')
img2 = cv2.imread('opencv_logo.png')

# I want to put logo on top-left corner, So I create a ROI
img2 = cv2.resize(img2,(200, 200), interpolation = cv2.INTER_CUBIC)
rows,cols,channels = img2.shape
roi = img1[0:rows, 0:cols ]

img3=cv2.cvtColor(img2,cv2.COLOR_RGB2GRAY)

mask=(img3!=255).astype(np.uint8)
b=img2[:,:,0]*mask
g=img2[:,:,1]*mask
r=img2[:,:,2]*mask
img2=cv2.merge([b,g,r])

mask=(img3==255).astype(np.uint8)
b=roi[:,:,0]*mask
g=roi[:,:,1]*mask
r=roi[:,:,2]*mask
roi=cv2.merge([b,g,r])

dst = cv2.add(roi,img2)
img1[0:rows, 0:cols ] = dst

cv2.imshow('res',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值