本篇文章介绍了Opencv 基础图像处理操作,Python安装Opencv、图片的读取保存 cv2.imread() cv2.imshow() cv2.imshow() 函数的使用、灰度图的加载、图像的保存等操作。
安装numpy,在cmd中输入命令:
pip install numpy
安装opencv-python,在cmd输入命令:
pip install opencv-python
安装opencv-contrib-python,在cmd输入命令:
pip install opencv-contrib-python
镜像:
清华镜像: https://pypi.tuna.tsinghua.edu.cn/simple
中国科学技术大学 : https://pypi.mirrors.ustc.edu.cn/simple
豆瓣: http://pypi.douban.com/simple/
阿里云: http://mirrors.aliyun.com/pypi/simple/
用法
pip install PyQT6 -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
pip install PyQT6-tools -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
主要有三个函数: cv2.imread() cv2.imshow() cv2.imshow()
读入方式: cv2.IMREAD_COLOR
:彩色图,默认值(1) cv2.IMREAD_COLOR
:彩色图,默认值(1) cv2.IMREAD_COLOR
:彩色图,默认值(1)
import cv2
import matplotlib.pyplot as plt
import numpy as np
def print_hi(name):
# 在下面的代码行中使用断点来调试脚本。
print(f'Hi, {name}') # 按 Ctrl+F8 切换断点。
print(cv2.__version__)
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
print_hi('PyCharm')
img = cv2.imread('D:/Jupyter_Notebooks/0.jpg', cv2.COLOR_BGR2RGB)
# 将彩色图的BGR通道顺序转成RGB
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 图像的显示,也可以创建多个窗口
cv2.imshow('image', img)
# 打印图片的形状
print(img.shape)
# height 高 ,width 宽 , channels 通道数
height, width, channels = img.shape
# 等待时间,毫秒级,0表示任意键终止
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import matplotlib.pyplot as plt
import numpy as np
def print_hi(name):
# 在下面的代码行中使用断点来调试脚本。
print(f'Hi, {name}') # 按 Ctrl+F8 切换断点。
print(cv2.__version__)
# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
img = cv2.imread('D:/Jupyter_Notebooks/1.png', cv2.IMREAD_GRAYSCALE)
# 将彩色图的BGR通道顺序转成RGB
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow('image', img)
# 打印图片的形状
print(img.shape)
# height 高 ,width 宽
height, width = img.shape
# 等待时间,毫秒级,0表示任意键终止
cv2.waitKey(0)
cv2.destroyAllWindows()
#保存图片
cv2.imwrite('outImage.jpg',img)