计算机视觉系列之1-图片读取

文章目录

1.用Skimage读取tif

复制代码
channel默认顺序是RGB/RGBA,读取得到的img是数组ndarray,type=unit32, 像素值默认范围:0-65536
io.imread()可以读取的tif波段数:
gray --MN
RGB --MN3
RGBA --MN*4 (RGBA: red-green-blud-alpha, 除了tif,png也可以保存此格式)
python 复制代码
from skimage import io
imgpath='image/file/name.tif'
img=io.imread(imgpath)
r=img[:,:,0]
g=img[:,:,1]
b=img[:,:,2]
nir=img[:,:,3]

2.PIL读取

复制代码
 channel默认顺序是RGB,数据类型 PIL.JpegImagePlugin.JpegImageFile,通过 numpy.asarray(image_pil) 可转成 numpy.ndarray
 像素值默认范围为0-255
python 复制代码
from PIL import Image
imgpath='image/file/name.tif'
img=Image.open(imgpath)
print('%s, %s, %s' % (img.mode, img.size, img.format))
img.show()

3.OpenCV读取

复制代码
channel默认顺序H×W×C,BGR,数据类型numpy.ndarray,支持 bmp、jpg、png、tiff 等常用格式
通过 cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB) 可转成 RGB
python 复制代码
import cv2 as cv
img = cv.imread("sancun.tif", 1)

4.Scipy读取

复制代码
Notes:
imread uses the Python Imaging Library (PIL) to read an image. The following notes are from the PIL documentation.使用Python图像库(PIL)读取图像

mode can be one of the following strings:
'L' (8-bit pixels, black and white)(8位像素,黑白)
'P' (8-bit pixels, mapped to any other mode using a color palette)(8位像素,使用调色板映射到任何其他模式)
'RGB' (3x8-bit pixels, true color)(3x8位像素,真彩色)
'RGBA' (4x8-bit pixels, true color with transparency mask)(4x8位像素,带透明蒙版的真彩色)
'CMYK' (4x8-bit pixels, color separation)(4x8位像素,分色)
'YCbCr' (3x8-bit pixels, color video format)(3x8位像素,彩色视频格式)
'I' (32-bit signed integer pixels)(32位有符号整数像素)
'F' (32-bit floating point pixels)(32位浮点像素)

PIL also provides limited support for a few special modes, including 'LA' ('L' with alpha), 'RGBX' (true color with padding) and 'RGBa' (true color with premultiplied alpha).
PIL还提供了对某些特殊模式的有限支持,包括" LA"(带L的" L")," RGBX"(带填充的真彩色)和" RGBa"(带预乘alpha的真彩色)。

When translating a color image to black and white (mode 'L', 'I' or 'F'), the library uses the ITU-R 601-2 luma transform:
将彩色图像转换为黑白图像(模式" L"," I"或" F")时,该库使用ITU-R 601-2亮度转换:
L = R * 299/1000 + G * 587/1000 + B * 114/1000

When flatten is True, the image is converted using mode 'F'. When mode is not None and flatten is True, the image is first converted according to mode, and the result is then flattened using mode 'F'.
展平为True时,将使用" F"模式转换图像。当mode不为None且flatten为True时,将首先根据mode转换图像,然后使用" F"模式将结果展平。
python 复制代码
import scipy
from scipy import misc
imgpath='image/file/name.tif'
img=misc.imread(imgpath)
#or
from scipy import ndimage
img = ndimage.imread(imgpath, mode='RGB')
相关推荐
feng14562 分钟前
OpenSREClaw - 故障复盘和变更评审双 Agent 案例
运维·人工智能
普马萨特5 分钟前
室内外定位导航的最新趋势(基于国际大会观察)
人工智能
Black蜡笔小新6 分钟前
私有化本地化AI模型训推工作站/AI大模型训练工作站DLTM赋能安全监控迈入智能时代
人工智能
HackTwoHub12 分钟前
全新 AI 赋能网安平台 基于 Mitmproxy 流量分析自动化资产挖、轻量化综合渗透工具箱
人工智能·web安全·网络安全·系统安全·安全架构·sql注入
LaughingZhu13 分钟前
Product Hunt 每日热榜 | 2026-04-27
人工智能·经验分享·深度学习·产品运营
MATLAB代码顾问15 分钟前
Python实现蜂群算法优化TSP问题
开发语言·python·算法
代码飞天20 分钟前
机器学习算法和函数整理——助力快速查阅
人工智能·算法·机器学习
yaodong51828 分钟前
不会Python也能数据分析:Gemini 3.1 Pro解决办公问题的SQL自动生成
python·sql·数据分析
jinanwuhuaguo36 分钟前
(第三十三篇)五月的文明奠基:OpenClaw 2026.5.2版本的文明级解读
android·java·开发语言·人工智能·github·拓扑学·openclaw
BU摆烂会噶41 分钟前
【LangGraph】持久化实现的三大能力——时间旅行
数据库·人工智能·python·postgresql·langchain