图像中的数值计算

目录

图像读取与形状

  • 使用cv2.imread函数读取图像文件。
  • 图像的形状通过shape属性获取,格式为(高度, 宽度, 颜色通道数)
python 复制代码
import cv2

img1 = cv2.imread('bg.jpg')
img2 = cv2.imread('fish.jpg')

print('img1 is shape:', img1.shape)
print('img2 is shape:', img2.shape)

图像数据展示

  • 通过切片操作[:5, :, 0]展示图像的前五行第一个颜色通道的数据。
python 复制代码
print('-' * 60)
print('img1 partial data:', img1[:5, :, 0])
print('img2 partial data:', img2[:5, :, 0])

图像数据操作

  • 直接对图像数组进行数值操作,如img1 + 10,会将图像每个像素值增加10。
python 复制代码
img1_ = img1 + 10
print('img1 + 10 partial data:', img1_[:5, :, 0])

超出范围的像素值处理

  • 当像素值超过255时,直接数组加法会导致取余操作,将值映射回0到255范围。
  • cv2.add函数在像素值超出0到255范围时,会将超出部分归一化到255。
python 复制代码
print('-' * 60)
print('img1 + img1_ partial data:', (img1 + img1_)[:5, :, 0])
print('-' * 60)
print(cv2.add(img1, img1_)[:5, :, 0])


👇 源码资料获取 · 技术与交流 👇

相关推荐
康燕1 小时前
2026 程序员 AI新范式-- 第三章:寻找新奶酪——AI辅助下的全栈转型
人工智能·ai·ai新范式
波动几何2 小时前
CAD制图编辑器cad-editor
人工智能
耿雨飞8 小时前
第三章:LangChain Classic vs. 新版 LangChain —— 架构演进与迁移指南
人工智能·架构·langchain
BizViewStudio8 小时前
甄选 2026:AI 重构新媒体代运营行业的三大核心变革与落地路径
大数据·人工智能·新媒体运营·媒体
俊哥V8 小时前
AI一周事件 · 2026年4月8日至4月14日
人工智能·ai
GitCode官方9 小时前
G-Star Gathering Day 杭州站回顾
人工智能·开源·atomgit
宇擎智脑科技9 小时前
开源 AI Agent 架构设计对比:Python 单体 vs TypeScript 插件化
人工智能·openclaw·hermes agent
冷色系里的一抹暖调10 小时前
OpenClaw Docker部署避坑指南:服务启动成功但网页打不开?
人工智能·docker·容器·openclaw
曹牧10 小时前
自动编程AI落地方案‌
人工智能
天云数据10 小时前
Harness革命:企业级AI从“失控野马”到“价值引擎”的跃迁
人工智能