opencv study notes two - image basic operation

1. POI area: area of ​​interest

2. Edge padding

3. Numerical operations

import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from PIL import Image

image1 = mpimg.imread('1.jpg')
image2 = mpimg.imread('2.jpg')
plt.imshow(image1)

Please add a picture description

plt.imshow(image2)

Please add a picture description

image addition
image3 = image1+image2
plt.imshow(image3)

Please add a picture description

image cropping
plt.imshow(image1[100:-100, 100:-100, :]);

Please add a picture description

image scaling

fx and fy represent the zoom factor

image4 = cv2.resize(image1, (0, 0), fx=3, fy=1)
plt.imshow(image4);

Please add a picture description

4. Image Thresholding

ret, dst = cv2.threshold(src, thresh, maxval, type)

  • src: input image, only single channel image
  • dst: output map
  • thresh: threshold
  • maxval: the value when the pixel value exceeds the threshold or is less than the threshold
  • type: the type of binarization
    • cv2.THRESH_BINARY takes maxval if it exceeds the threshold, otherwise takes 0
    • cv2.THRESH_BINARY_INV Inversion of THRESH_BINARY
    • cv2.THRESH_TRUNC is greater than the threshold setting threshold, the rest remain unchanged
    • cv2.THRESH_TOZERO is greater than the threshold unchanged, and the rest are set to 0
    • cv2.THRESH_TOZERO_INV Inversion of THRESH_TOZERO
image1_gray = image1[:,:,0]
res, thresh1 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_BINARY)
res, thresh2 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_BINARY_INV)
res, thresh3 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_TRUNC)
res, thresh4 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_TOZERO)
res, thresh5 = cv2.threshold(image1_gray, 127, 255, cv2.THRESH_TOZERO_INV)

titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [image1_gray, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
    plt.subplot(2,3,i+1)
    plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([])
    plt.yticks([])
plt.show()

Please add a picture description

Guess you like

Origin blog.csdn.net/weixin_44669966/article/details/125634909