Opencv study notes (2) basic image operations

Basic image operations

1. Boundary filling

2. Image fusion

3. Image threshold

4. Image smoothing

5. Morphological budget

1. Corrosion operation

2. Expansion operation

3. Opening and closing operations

4. Gradient operation

5. Top hat operation

6. Black hat operation


1. Boundary filling

  • cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=?)

(1)cv2.BORDER_REPLICATE, copy method, assign the edge pixel

(2)cv2.BORDER_REFLECT, reflection method, copies the pixels in the image of interest on both sides, such as hgfedcba|abcdefgh|hgfedcba

(3)cv2.BORDER_REFLECT101, reflection method, axially symmetrical with the most edge level pixel, hgfedcb|abcdefgh|gfedcba

(4)cv2.BORDER_WRAP, outer packaging method, abcdefgh|abcdefgh|abcdefgh

(5)cv2.BORDER_CONSTANT, constant value filling.

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE)

top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)

replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
relect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REFLECT)
relect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REFLECT101)
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_CONSTANT, value=0)

plt.subplot(231), plt.imshow(img, cmap='gray'), plt.title('ORIGINAL')
plt.subplot(232), plt.imshow(replicate, cmap='gray'), plt.title('REPLICATE')
plt.subplot(233), plt.imshow(relect, cmap='gray'), plt.title('REFLECT')
plt.subplot(234), plt.imshow(relect101, cmap='gray'), plt.title('REFLECT101')
plt.subplot(235), plt.imshow(wrap, cmap='gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, cmap='gray'), plt.title('CONSTANT')
plt.show()

2. Image fusion

  • cv2.addWeighted(img1, alpha, img2, 1-alpha, gamma)
import cv2

img1 = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('dog.jpg', cv2.IMREAD_GRAYSCALE)
print(img1.shape)
print(img2.shape)
img_dog = cv2.resize(img2, (256, 256))
res = cv2.addWeighted(img1, 0.4, img_dog, 0.6, 0)

cv2.imshow('res', res)

cv2.waitKey(0)
cv2.destroyAllWindows()

3. Image threshold

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

  • src: input image, only single-channel images can be input, usually grayscale images
  • dst: output graph
  • threshold: threshold
  • maxval: The value assigned when the pixel value exceeds the threshold (or is less than the threshold, depending on the type)
  • type: The types of binarization operations include the following five types:
    • cv2.THRESH_BINARY takes maxval for the part exceeding the threshold
    • cv2.THRESH_BINARY_INV, the inversion of cv2.THRESH_BINARY
    • cv2.THRESH_TRUNC, the part greater than the threshold is set to maxval, otherwise it remains unchanged
    • cv2.THRESH_TOZERO, the part greater than the threshold remains unchanged, otherwise it is 0
    • cv2.THRESH_TOZERO_INV, the inversion of cv2.THRESH_TOZERO
import cv2
import matplotlib.pyplot as plt

img1 = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE)

ret, thresh1 = cv2.threshold(img1, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img1, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img1, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img1, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img1, 127, 255, cv2.THRESH_TOZERO_INV)

titles = ['Origin', 'binary', 'binary_inv', 'trunc', 'tozero', 'tozero_inv']
images = [img1, thresh1, thresh2, thresh3, thresh4, thresh5]

for i in range(6):
    plt.subplot(2, 3, i+1), plt.imshow(images[i], cmap='gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])

plt.show()

4. Image smoothing

  • Mean filter, cv2.blur(img, (kernel_size, kernel_size))
  • Box filter, cv2.boxFilter(img, -1, (kernel_size, kernel_size), normaliza=True), whether to average. Basically the same as mean filtering
  • Gaussian filter: cv2.GaussianBlur(img, (kernel_size, kernel_size), sigma)
  • Median filter: cv2.medianBlur(img, 5)
import cv2
import numpy as np

img = cv2.imread('lena.jpg')
cv2.waitKey(0)
cv2.destroyAllWindows()

blur = cv2.blur(img, (3, 3))

cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

median = cv2.medianBlur(img, 5)
cv2.imshow('median', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

gaussian = cv2.GaussianBlur(img, (5, 5), 1)
cv2.imshow('gaussian', gaussian)
cv2.waitKey(0)
cv2.destroyAllWindows()

res = np.hstack((blur, median, gaussian))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

5. Morphological operations

As far as the picture above is concerned, both expansion and corrosion are targeted at the white part rather than the black part. Expansion is the expansion of the white area, and corrosion is the shrinkage of the white area.

1. Corrosion operation, cv2.erode(src, kernel, interation=?)

import cv2
import numpy as np

img = cv2.imread('zhinian.png')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

kernel = np.ones((5, 5), np.uint8)

erosion1  = cv2.erode(img, kernel, iterations=1)
erosion2  = cv2.erode(img, kernel, iterations=2)
erosion3  = cv2.erode(img, kernel, iterations=3)

res = np.hstack((erosion1, erosion2, erosion3))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. Dilation operation, cv2.dilate(src, kernel, interation=?)

import cv2
import numpy as np

img = cv2.imread('zhinian.png')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

kernel = np.ones((5, 5), np.uint8)

dilate1  = cv2.dilate(img, kernel, iterations=1)
dilate2  = cv2.dilate(img, kernel, iterations=2)
dilate3  = cv2.dilate(img, kernel, iterations=3)

res = np.hstack((dilate1, dilate2, dilate3))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. Opening and closing operations, cv2.morphologyEx(img, type, kernel, iteration=?)

Open operation: corrode first and then expand, type=cv2.MORPH_OPEN

Closed operation: first expansion and then erosion, type=cv2.MORPH_CLOSED

import cv2
import numpy as np

img = cv2.imread('zhinian.png')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

kernel = np.ones((5, 5), np.uint8)
open1 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=1)
open2 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=2)
open3 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel, iterations=3)

res1 = np.hstack((open1, open2, open3))
cv2.imshow('res', res1)
cv2.waitKey(0)
cv2.destroyAllWindows()

closed1 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, iterations=1)
closed2 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, iterations=2)
closed3 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, iterations=3)

res2 = np.hstack((closed1, closed1, closed1))
cv2.imshow('res', res2)
cv2.waitKey(0)
cv2.destroyAllWindows()

4. Gradient operation = expansion - corrosion, cv2.morphologyEx(img, 2v.MORPH_GRADIENT, kernel, iteration=?)

gradient1 = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel, iterations=1)
gradient2 = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel, iterations=2)
gradient3 = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel, iterations=3)

res3 = np.hstack((gradient1, gradient2, gradient3))
cv2.imshow('res', res3)
cv2.waitKey(0)
cv2.destroyAllWindows()

5. Top hat operation = original input - open operation, cv2.morphologyEx(img, 2v.MORPH_TOPHAT, kernel, iteration=?)

tophat1 = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel, iterations=1)
tophat2 = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel, iterations=2)
tophat3 = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel, iterations=3)

res4 = np.hstack((tophat1, tophat2, tophat3))
cv2.imshow('res', res4)
cv2.waitKey(0)
cv2.destroyAllWindows()

6. Black hat operation = closed operation - original input, cv2.morphologyEx(img, 2v.MORPH_BLACKHAT, kernel, iteration=?)

blackhat1 = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, iterations=1)
blackhat2 = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, iterations=2)
blackhat3 = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel, iterations=3)

res5 = np.hstack((blackhat1, blackhat2, blackhat3))
cv2.imshow('res', res5)
cv2.waitKey(0)
cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/qq_53545309/article/details/135205167