1 图像加减乘除位运算
1.1 加法 img = cv2.add(img1, img2)
import cv2
import numpy as np
import matplotlib.pyplot as plt
lena = cv2.imread('lenacolor.png',-1)
noise = np.random.randint(0,255,lena.shape,dtype=np.uint8)
img_add = lena+noise
img_cv_add = cv2.add(lena,noise)
plt.subplot(221)
plt.title('lena')
plt.imshow(lena[...,::-1])
plt.subplot(222)
plt.title('noise')
plt.imshow(noise[...,::-1])
plt.subplot(223)
plt.title('img_add')
plt.imshow(img_add[...,::-1])
plt.subplot(224)
plt.title('img_cv_add')
plt.imshow(img_cv_add[...,::-1])
plt.show()
1.2 减法 img = cv2.subtract(img1, img2)
import cv2
import numpy as np
import matplotlib.pyplot as plt
img_0 = cv2.imread('34.jpeg',-1)
img_1 = cv2.imread('35.jpeg',-1)
img_sub = cv2.subtract(img_0, img_1)
plt.subplot(131)
plt.title('img_0')
plt.imshow(img_0[...,::-1])
plt.subplot(132)
plt.title('img_1')
plt.imshow(img_1[...,::-1])
plt.subplot(133)
plt.title('img_sub')
plt.imshow(img_sub[...,::-1])
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img_0 = cv2.imread('img_no.png',0)
img_1 = cv2.imread('sub.png',0)
img_sub = cv2.subtract(img_0, img_1)
plt.subplot(131)
plt.title('img_0')
plt.imshow(img_0,cmap='gray')
plt.subplot(132)
plt.title('img_1')
plt.imshow(img_1,cmap='gray')
plt.subplot(133)
plt.title('img_sub')
plt.imshow(img_sub,cmap='gray')
plt.show()
1.3 乘法 img = cv2.multiply(img1, img2)
import cv2
import numpy as np
import matplotlib.pyplot as plt
lena = cv2.imread('lenacolor.png',-1)
mask = np.zeros_like(lena,np.uint8)
mask[204:392,213:354] = 1
img_mul = cv2.multiply(lena, mask)
plt.subplot(131)
plt.title('lena')
plt.imshow(lena[...,::-1])
plt.subplot(132)
plt.title('mask')
plt.imshow(mask[...,::-1])
plt.subplot(133)
plt.title('img_mul')
plt.imshow(img_mul[...,::-1])
plt.show()
1.4 除法 img = cv2.divide(img1, img2)
import cv2
import numpy as np
import matplotlib.pyplot as plt
lena = cv2.imread('lenacolor.png',0)
img_noise = cv2.circle(lena.copy(),(280,300),150,(0,255,0),10)
img_div = cv2.divide(img_noise,lena)
plt.subplot(131)
plt.title('lena')
plt.imshow(lena,cmap='gray')
plt.subplot(132)
plt.title('img_noise')
plt.imshow(img_noise,cmap='gray')
plt.subplot(133)
plt.title('img_div')
plt.imshow(img_div,cmap='gray')
plt.show()
1.5 位运算 cv2.bitwise_and()
import cv2
import numpy as np
import matplotlib.pyplot as plt
lena = cv2.imread('lenacolor.png',1)
mask = np.zeros_like(lena,dtype=np.uint8)
mask = cv2.circle(mask,(280,280),111,(255,255,255),-1)
re = cv2.bitwise_and(lena,mask)
plt.subplot(131)
plt.title('lena')
plt.imshow(lena[...,::-1])
plt.subplot(132)
plt.title('mask')
plt.imshow(mask[...,::-1])
plt.subplot(133)
plt.title('re')
plt.imshow(re[...,::-1])
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
lena = cv2.imread('lenacolor.png',1)
mask = np.zeros(lena.shape[:2],dtype=np.uint8)
mask = cv2.circle(mask,(280,280),111,(255,255,255),-1)
re = cv2.bitwise_and(lena,lena,mask=mask)
plt.subplot(131)
plt.title('lena')
plt.imshow(lena[...,::-1])
plt.subplot(132)
plt.title('mask')
plt.imshow(mask,'gray')
plt.subplot(133)
plt.title('re')
plt.imshow(re[...,::-1])
plt.show()
2 图像增强
2.1 线性变换
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('lianhua.png',1)
re = img*2+10
re = re.astype(np.uint8)
re1 = cv2.convertScaleAbs(img, alpha=2, beta=10)
plt.subplot(131)
plt.title('img')
plt.imshow(img[...,::-1])
plt.subplot(132)
plt.title('re0')
plt.imshow(re0[...,::-1])
plt.subplot(133)
plt.title('re1')
plt.imshow(re1[...,::-1])
plt.show()
2.2 非线性变换
import cv2
import numpy as np
import matplotlib.pyplot as plt
## 1 gamma
def gamma_aug(img,c,gamma):
gamma_table=[c*np.power(x/255.0,gamma)*255.0 for x in range(256)]
gamma_table=np.round(np.array(gamma_table)).astype(np.uint8)
return cv2.LUT(img,gamma_table)
## 2 log
def log_aug(img,c,r):
gamma_table=[c*np.log10(1+x/255.0*r)*255.0 for x in range(256)]
gamma_table=np.round(np.array(gamma_table)).astype(np.uint8)
return cv2.LUT(img,gamma_table)
if __name__ == '__main__':
img = cv2.imread('lianhua.png',1)
img11 = gamma_aug(img,c=1,gamma=0.1)
img12 = gamma_aug(img, c=1, gamma=0.8)
img21 = log_aug(img, c=1, r=10)
img22 = log_aug(img, c=2, r=10)
plt.subplot(231)
plt.title('img')
plt.imshow(img[...,::-1])
plt.subplot(232)
plt.title('img11')
plt.imshow(img11[..., ::-1])
plt.subplot(233)
plt.title('img12')
plt.imshow(img12[..., ::-1])
plt.subplot(234)
plt.title('img')
plt.imshow(img[...,::-1])
plt.subplot(235)
plt.title('img21')
plt.imshow(img21[..., ::-1])
plt.subplot(236)
plt.title('img22')
plt.imshow(img22[..., ::-1])
plt.show()
3 图像几何变换
3.1 裁剪、放大、缩小
(1) 公式缩放
'''
dst = cv2.resize(src,dsize,fx=0,fy=0,interpolation=cv2.INTER_LINEAR)
参数:
src : 输入图像
dsize: 绝对尺寸,直接指定调整后图像的大小
fx,fy: 相对尺寸,将dsize设置为None,然后将fx和fy设置为比例因子即可
interpolation:插值方法(INTER_NEAREST,INTER_LINEAR)
'''
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('lenacolor.png',1)
img1 = cv2.resize(img,(100,100)) # dsize
img2 = cv2.resize(img,None,fx=0.5,fy=0.5) # fx,fy
plt.subplot(131)
plt.title(f'img.shape:{format(img.shape[:2])}')
plt.imshow(img[..., ::-1])
plt.subplot(132)
plt.title(f'img1.shape:{format(img1.shape[:2])}')
plt.imshow(img1[..., ::-1])
plt.subplot(133)
plt.title(f'img2.shape:{format(img2.shape[:2])}')
plt.imshow(img2[..., ::-1])
plt.show()
(2) 最近邻源码缩放
'''
img[100,100,3] --> img1 [10,10,3] scale = 10/100 (5,5)-->5/scale -->(50,50)
'''
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('lenacolor.png',1)
h,w,c = img.shape
h1,w1,d = 100,200,c
h_scale = h1*1.0/h
w_scale = w1*1.0/w
img_new = np.zeros([h1,w1,d],np.uint8)
for i in range(h1):
for j in range(w1):
img_new[i,j] = img[int(i/h_scale),int(j/w_scale)]
plt.subplot(121)
plt.title(f'img.shape:{format(img.shape[:2])}')
plt.imshow(img[..., ::-1])
plt.subplot(122)
plt.title(f'img_new.shape:{format(img_new.shape[:2])}')
plt.imshow(img_new[..., ::-1])
文章来源:https://www.toymoban.com/news/detail-608271.html
(3) 最近邻文章来源地址https://www.toymoban.com/news/detail-608271.html
3.2 平移变换
3.3 错切变换
3.4 镜像变换
3.5 旋转变换
3.6 透视变换
3.7 最近邻插值、双线性插值
到了这里,关于OpenCV系列__chapter2的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!