Day1:OpenCV安装及处理图像
00 安装
在Prompt中输入pip install opencv-python ,如果报错找不到匹配的版本可以在清华镜像中寻找合适版本,放到Scripts文件夹中,然后打开到该路径下用pip install xxx.whl 安装,可以通过import cv2 验证是否安装成功,若依然报错,可能是缺少MSVCP140.DLL模块,可以在官网下载。
01 读写图片
import cv2
import numpy as np
img = cv2.imread('image.png')
img1 = cv2.imread('image.png',cv2.IMREAD_UNCHANGED)
img2 = cv2.imread('image.png',cv2.IMREAD_GRAYSCALE)
img3 = cv2.imread('image.png',cv2.IMREAD_COLOR)
img4 = cv2.imread('image.png',cv2.COLOR_BGR2RGB)
cv2.imshow('pict1',img1)
cv2.imshow('pict2',img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('butterfly.jpg',img1)
cv2.imwrite('gray_butterfly.jpg',img2)
最终可以保存两个图像:
02 像素处理
import cv2
img = cv2.imread('image.png', cv2.IMREAD_UNCHANGED)
point = img[50, 60]
print(point)
wh_point = [255, 255, 255]
blue = img[50, 60, 0]
green = img[50, 60, 1]
red = img[50, 60, 2]
img[50:80, 60:90] = wh_point
cv2.imshow('pic', img)
cv2.imwrite('wh_butterfly.jpg', img)
输出图片:
|