OpenCV入门 | 使用Python实现计算机视觉的第一步
import cv2
import numpy as np
import matplotlib.pyplot as plt
image = cv2.imread('img.jpg')
cv2.imshow('Some title', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
image = cv2.imread('img.jpg')
plt.imshow(image)
> print(type(image))
> print(image.shape)
<class 'numpy.ndarray'>
(776, 960, 3)
image = cv2.imread('img.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image)
def show(img):
fig, ax = plt.subplots(1, figsize=(12,8))
ax.axis('off')
plt.imshow(img, cmap='Greys')
image = cv2.imread('img2.jpeg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
show(image)
image = cv2.imread('img2.jpeg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
show(image)
img = cv2.imread('img2.jpeg')
B, G, R = cv2.split(img)
img = cv2.merge([B*0, G, R*0])
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
show(img)
img = cv2.merge([np.ones_like(B)*255, G, R])
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
show(img)
img = cv2.imread('img2.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
H, S, V = cv2.split(img)
img = cv2.merge([np.ones_like(H)*30, S+10, V-20])
img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB)
show(img)
# read img and convert to HSV
img = cv2.imread('img2.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# split dimensions
H, S, V = cv2.split(img)
# upper and lower boundaries
lower = np.array([80, 0, 0])
upper = np.array([120, 255, 255])
# build mask
mask = cv2.inRange(img, lower, upper)
# apply mask to saturation
S = cv2.bitwise_and(S, S, mask=mask)
# assemble image
img = cv2.merge([H, S, V])
# convert to RGB and display
img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB)
show(img)
img = cv2.imread('img2.jpeg')
B, G, R = cv2.split(img)
show(B)
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
H, S, V = cv2.split(img)
show(S)
资源:
OpenCV读取图像; https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html OpenCV颜色转换; https://docs.opencv.org/3.4/de/d25/imgproc_color_conversions.html Matplotlib显示图像; https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html 数组上的OpenCV操作; https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html OpenCV基本操作; https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_basic_ops/py_basic_ops.html
求分享 求点赞 求在看!!!
评论