Python Opencv实践 - 基本图像IO操作

复制代码
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

#读取图像
#cv2.IMREAD_COLOR: 读取彩色图像,忽略alpha通道,也可以直接写1
#cv2.IMREAD_GRAYSCALE: 读取灰度图,也可以直接写0
#cv2.IMREAD_UNCHANGED: 读取原始图像数据,包括alpha通道,也可以直接写-1
img = cv.imread("pomeranian.png", cv.IMREAD_COLOR)

#显示图像信息
print(img.shape)

#opencv原始窗口显示图像
cv.imshow("pomeranian", img)
cv.waitKey(0)
cv.destroyAllWindows()

#matplotlib显示图像
plt.imshow(img[:,:,::-1])
plt.show()

#matplotlib显示灰度图,https://blog.csdn.net/qq_30967115/article/details/85053415
img = cv.imread("pomeranian.png", cv.IMREAD_GRAYSCALE)
plt.imshow(img, cmap="gray")
plt.show()

#写入图像
#imwrite详细参考:https://blog.csdn.net/m0_55320151/article/details/127012570
cv.imwrite("dog.png", img)
相关推荐
cup114 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi006 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵8 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf9 小时前
Agent 流程编排
后端·python·agent
copyer_xyf10 小时前
Agent RAG
后端·python·agent
copyer_xyf10 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf10 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏