OPENCV python的阈值程序
import cv2
import matplotlib.pyplot as plt
import numpy as np
img=cv2.imread('d:\picture\cat.jpg')
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
rows, cols,channel = img.shape[:3]
print(rows)
print(cols)
print(channel)
cv2.imwrite('d:\picture\mycat.jpg',img)
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
b,g,r=cv2.split(img)
rows, cols = r.shape[:2]
print(rows)
print(cols)
cv_show('red',r)
GRAY = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv_show('GRAY',GRAY)
ret,thresh1 = cv2.threshold(GRAY, 127, 255, cv2.THRESH_BINARY)
plt.subplot(3,3,1)
plt.imshow(thresh1,'gray');plt.axis('off');plt.title('THRESH_BINARY')
cv_show('thresh1',thresh1)
ret, thresh2 = cv2.threshold(GRAY, 127, 255, cv2.THRESH_BINARY_INV)
plt.subplot(2,3,2)
plt.imshow(thresh2);plt.axis('off');plt.title('THRESH_BINARY_INV')
ret, thresh3 = cv2.threshold(GRAY, 127, 255, cv2.THRESH_TRUNC)
plt.subplot(2,3,3)
plt.imshow(thresh3);plt.axis('off');plt.title('THRESH_TRUNC')
ret, thresh4 = cv2.threshold(GRAY, 127, 255, cv2.THRESH_TOZERO)
plt.subplot(2,3,4)
plt.imshow(thresh4);plt.axis('off');plt.title('THRESH_TOZERO')
ret, thresh5 = cv2.threshold(GRAY, 127, 255, cv2.THRESH_TOZERO_INV)
plt.subplot(2,3,5)
plt.imshow(thresh5);plt.axis('off');plt.title('THRESH_TOZERO_INV')
plt.show()