教學、程式來源:
Binary Thresholding using OpenCV and Python
Adaptive Thresholding - Mean and Gaussian Thresholding using OpenCv and Python
How to Crop and Resize Images [3] | OpenCV Python Tutorials for Beginners 2020
openCV 原本是藍、綠、紅 ,所以要轉成一般的 紅、綠、藍
grid_RGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #bgr 藍、綠、紅 轉成 紅、綠、藍
grid_HSV = cv2.cvtColor(grid_RGB,cv2.COLOR_RGB2HSV) #紅、綠、藍 轉成HSV
image_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
圖片要先轉灰階再轉黑白,不然怪怪的
ret,white_black = cv2.threshold(image_gray,127,255,cv2.THRESH_BINARY)
長寬變成1000,1000
width, height = 1000,1000
img = cv2.resize(img,(width,height))
print(img.shape)
# 假設圖片(img)高度是1000,寬度是1000
# 要裁減 左上角 高度100,寬度100。
會是:
imgCropped = img[0:100,0:100]
代表 imgCropped = img[y的起點:y的終點,x的起點:x的終點]
# cv2.ADAPTIVE_THRESH_MEAN_C
more_white_black = cv2.adaptiveThreshold(image_gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,57,8)
# cv2.ADAPTIVE_THRESH_GAUSSIAN_C
more_white_black = cv2.adaptiveThreshold(image_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,63,14)
# cv2.THRESH_BINARY_INV 可以讓黑白顛倒
more_white_black = cv2.adaptiveThreshold(image_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,85,15)