opencv-python image processing base (II)

Image thresholding

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

- src: enter, only single-channel image input, general grayscale image
- dst: FIG Output
- thresh: Threshold 0-255 typically 127
- MAXVAL: When the pixel value exceeds the threshold value (threshold value or less, according to type is determined), the maximum value given 255
- type: the type of binarization operation, comprising the following five types: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO ; cv2.THRESH_TOZERO_INV

- cv2.THRESH_BINARY takes part exceeding the threshold value MAXVAL (maximum), otherwise, it is 0
- cv2.THRESH_BINARY_INV THRESH_BINARY inversion
- cv2.THRESH_TRUNC portion is greater than the threshold value as a threshold value, otherwise unchanged
- cv2.THRESH_TOZERO portion is not greater than a threshold change, or It is set to 0
- cv2.THRESH_TOZERO_INV THRESH_TOZERO reversal

import cv2
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB


img=cv2.imread('d:/image0.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

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

 

 

 

 

 Filtering

CV2 Import 
Import numpy AS NP 
Import matplotlib.pyplot AS PLT # Matplotlib is the RGB 


IMG = cv2.imread ( ' D: /image0.jpg ' ) 
# cv2.imshow ( " Image " , IMG) 
# average filtering 
Bluer = cv2.blur (IMG, ( . 3 , . 3 ))
 
# filtering block 
# mean substantially the same and may be selected normalized 
Box = cv2.boxFilter (IMG, - . 1 , ( . 3 , . 3 ), the normalize = True)
 
# filtering block 
# essentially mean the same and can be selected normalization, easy bounds 
BOX2 = cv2.boxFilter (IMG, - . 1 , ( . 3,3),normalize=False)
res=np.hstack((bluer,box,box2))
cv2.imshow("da",res)
 cv2.waitKey(0)
cv2.destroyAllWindows()

 

 

 

 

 

 

 

# Gaussian filter 
# Gaussian filter to the value obtained in the convolution kernel satisfying a Gaussian distribution corresponding to the intermediate distribution 
Import CV2
 Import numpy AS NP 
IMG = cv2.imread ( " D: /image0.jpg " ) 
aussian = cv2.GaussianBlur (IMG , (3,3), 1 )
# Mean Filter
bluer=cv2.blur(img,(5,5))
#中值滤波 median
=cv2.medianBlur(img,5) res=np.hstack((aussian,bluer,median)) cv2.imshow("aussian vs averge",res) cv2.waitKey(0) cv2.destroyAllWindows()

 

Guess you like

Origin www.cnblogs.com/xujunjia/p/11440828.html