1.图像的读取
import cv2
print(cv2.getVersionString())
image = cv2.imread(r"C:\Users\31994\Pictures\Screenshots\OIP-C.jpg")
print(image.shape)
cv2.imshow("image", image)
cv2.waitKey()
2. 图像的彩色通道BGR
import cv2
image = cv2.imread(r"C:\Users\31994\Pictures\Screenshots\OIP-C.jpg")
cv2.imshow("blue", image[:, :, 0])
cv2.imshow("green", image[:, :, 1])
cv2.imshow("red", image[:, :, 2])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)
cv2.waitKey()
3.图像的裁剪
import cv2
image = cv2.imread(r"C:\Users\31994\Pictures\Screenshots\OIP-C.jpg")
crop = image[1:100, 2:200]
cv2.imshow("crop", crop)
cv2.waitKey()
4.绘制直线、矩形、圆形、字符串
import cv2
import numpy as np
image = np.zeros([300, 300, 3], dtype=np.uint8)#创建画布
cv2.line(image, (100, 200), (250, 250), (255, 0, 0), 2)#绘制蓝色直线
cv2.rectangle(image, (30, 100), (60, 150), (0, 255, 0), 2)#绘制绿色矩形
cv2.circle(image, (150, 100), 20, (0, 0, 255), 2)#绘制红色圆形
cv2.putText(image, "hello", (100, 50), 0, 1, (255, 255, 255), 2, 1)#绘制白色字符串
cv2.imshow("image", image)
cv2.waitKey()
5.均值滤波处理图像噪点
import cv2
image = cv2.imread(r"C:\Users\31994\Pictures\Screenshots\plane.jpg")
gauss = cv2.GaussianBlur(image, (5, 5), 0)
median = cv2.medianBlur(image, 5)
cv2.imshow("image", image)
cv2.imshow("gauss", gauss)
cv2.imshow("median", median)
cv2.waitKey()
6.调用电脑摄像头
import cv2
capture = cv2.VideoCapture(0)
while True:
ret, frame = capture.read()
cv2.imshow("camera", frame)
key = cv2.waitKey(1)
if key != -1:
break
capture.release()