OpenCV系列__chapter2

这里写目录标题

    • 1 图像加减乘除位运算
      • 1.1 加法 img = cv2.add(img1, img2)
      • 1.2 减法 img = cv2.subtract(img1, img2)
      • 1.3 乘法 img = cv2.multiply(img1, img2)
      • 1.4 除法 img = cv2.divide(img1, img2)
      • 1.5 位运算 cv2.bitwise_and()
    • 2 图像增强
      • 2.1 线性变换
      • 2.2 非线性变换
    • 3 图像几何变换
      • 3.1 裁剪、放大、缩小
      • 3.2 平移变换
      • 3.3 错切变换
      • 3.4 镜像变换
      • 3.5 旋转变换
      • 3.6 透视变换
      • 3.7 最近邻插值、双线性插值

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()

OpenCV系列__chapter2_第1张图片

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()

OpenCV系列__chapter2_第2张图片

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()

OpenCV系列__chapter2_第3张图片

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()

OpenCV系列__chapter2_第4张图片

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()

OpenCV系列__chapter2_第5张图片

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()

OpenCV系列__chapter2_第6张图片

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()

OpenCV系列__chapter2_第7张图片

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()

OpenCV系列__chapter2_第8张图片

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()

OpenCV系列__chapter2_第9张图片

3 图像几何变换

3.1 裁剪、放大、缩小


3.2 平移变换


3.3 错切变换


3.4 镜像变换


3.5 旋转变换


3.6 透视变换


3.7 最近邻插值、双线性插值


你可能感兴趣的:(OpenCV,计算机视觉,Python,opencv,人工智能,计算机视觉)