02 Computer Vision-opencv Threshold and Filtering Processing

1 grayscale

# opencv reads in BGR format
# The format read by matplotlib is RGB
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

img = cv2.imread("cat.jpg")
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_gray.shape
(414, 500)
cv2.imshow("img_gray",img_gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.imshow(img_gray)
<matplotlib.image.AxesImage at 0x1cc481f5128>

2 HSV

  • H-tone (main wavelength)
  • S-saturation (shadows of purity/color)
  • V - strength
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow("hsv",hsv)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.imshow(hsv)
<matplotlib.image.AxesImage at 0x1cc34feada0>

3 Image Threshold

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

  • src: Input image, which can only be input into a single channel image, usually a grayscale image

  • dst: output chart

  • Threshold: Threshold

  • maxval: The value given when the pixel value exceeds (or is less than) the threshold, depending on the type.

  • type: two types of value operation, including the following 5 types: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO; cv2.THRESH_TOZERO_INV

  • cv2.THRESH_BINARY exceeds the threshold by maxval (maximum), otherwise 0.

  • Inversion of cv2.THRESH_BINARY_INV THRESH_BINARY

  • The part of cv2.THRESH_TRUNC larger than the threshold is set as the threshold, otherwise it will not change.

  • If the value of cv2.THRESH_TOZERO is greater than the threshold value, it will not change, otherwise it will be set to 0.

  • Inversion of cv2.THRESH_TOZERO_INV THRESH_TOZERO

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

titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [img, 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()

4 image smoothing

img = cv2.imread("lenaNoise.png")

cv2.imshow("img",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

plt.imshow(img)
<matplotlib.image.AxesImage at 0x1cc44d79080>

Mean filtering

  • Simple average convolution operation
blur = cv2.blur(img,(3,3))

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

plt.imshow(blur)
<matplotlib.image.AxesImage at 0x1cc35cbc748>

box filter

  • Basically the same as the mean, you can choose normalization.
box = cv2.boxFilter(img,-1,(3,3), normalize=True)  

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

plt.imshow(box)
<matplotlib.image.AxesImage at 0x1cc48671400>

box = cv2.boxFilter(img,-1,(3,3), normalize=False)  

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

plt.imshow(box)
<matplotlib.image.AxesImage at 0x1cc44e33a90>

Pseudo filtering

  • The value in the fuzzy convolution kernel satisfies the distribution of the value, which is more important than the middle one.
aussian = cv2.GaussianBlur(img, (5, 5), 1)  

cv2.imshow("aussian", aussian)
cv2.waitKey(0)
cv2.destroyAllWindows()

plt.imshow(aussian)
<matplotlib.image.AxesImage at 0x1cc35020f60>

median filtering

median = cv2.medianBlur(img,5)

cv2.imshow("median", median)
cv2.waitKey(0)
cv2.destroyAllWindows()

plt.imshow(median)
<matplotlib.image.AxesImage at 0x1cc42c25b00>

# Show all
res = np.hstack((blur,aussian,median))
cv2.imshow("median vs average",res)
cv2.waitKey(0)
cv2.destroyAllWindows()

plt.imshow(res)
<matplotlib.image.AxesImage at 0x1cc486ae470>

Tags: OpenCV less

Posted on Sun, 06 Oct 2019 15:47:35 -0700 by refined