10 个图像处理的Python库,超实用!

2026-06-24 09:41:35

# 强烈建议关注@公众号:数据STUDIO 更多好文定时推送

# Load one of the demo images

im = mh.demos.load( 'nuclear')

# Automatically compute a threshold

T_otsu = mh.thresholding.otsu(im)

# Label the thresholded image (thresholding is done with numpy operations

seeds,nr_regions = mh.label(im > T_otsu)

# Call seeded watershed to expand the threshold

labeled = mh.cwatershed(im.max - im, seeds)

这是一个非常简单的使用示例mahotas.distance(计算距离图):

importpylab asp

importnumpy asnp

importmahotas asmh

f = np.ones(( 256, 256), bool)

f[ 200:, 240:] = False

f[ 128: 144, 32: 48] = False

# f is basically True with the exception of two islands: one in the lower-right

# corner, another, middle-left

dmap = mh.distance(f)

p.imshow(dmap)

p.show

Scikit-Image

Scikit-Image建立在Scikit-Learn机器学习库的基础上的扩展功能,包括更高级的图像处理能力。所以如果已经在使用Scikit进行ML,那么可以考虑使用这个库。

它提供了一套完整的图像处理算法。它支持图像分割、几何变换、色彩空间操作和过滤。

与许多其他库不同,Scikit-Image支持多维图像,这对于涉及视频或医学成像的任务是很有帮助的。Scikit-Image与其他Python科学库(如NumPy和SciPy)无缝集成。

fromskimage importdata, io, filters

image = data.coins

# ... or any other NumPy array!

edges = filters.sobel(image)

io.imshow(edges)

io.show

TensorFlow Image

TensorFlow Image是TensorFlow的一个模块,它支持图像解码、编码、裁剪、调整大小和转换。还可以利用TensorFlow的GPU支持,为更大的数据集提供更快的图像处理。

也就是说如果你使用TF,那么可以使用它来作为训练Pipline的一部分。

使用 Keras 效用函数加载数据:tf.keras.utils.image_dataset_from_directory 效用函数从磁盘加载图像。

创建数据集,为加载器定义一些参数:

batch_size = 32

img_height = 180

img_width = 180

开发模型时,最好使用验证拆分。您将使用 80% 的图像进行训练,20% 的图像进行验证。

train_ds = tf.keras.utils.image_dataset_from_directory(

data_dir,

validation_split= 0.2,

subset= "training",

seed= 123,

image_size=(img_height, img_width),

batch_size=batch_size)

PyTorch Vision

与TensorFlow Image类似,PyTorch Vision是PyTorch生态系统的一部分,主要用于与图像处理相关的机器学习任务。

importtorchvision

video_path = "path to a test video"

reader = torchvision.io.VideoReader(video_path, "video")

reader_md = reader.get_metadata

print(reader_md[ "video"][ "fps"])

video.set_current_stream( "video:0")

SimpleCV

SimpleCV建立在OpenCV、PIL(Python Imaging Library)和NumPy之上,为用户提供了一组简单而强大的函数和工具,用于加载、处理和分析图像。

SimpleCV的设计目标是使计算机视觉技术对于初学者和非专业人士也能更加可靠和易于使用。它提供了一个简单的API,隐藏了底层的复杂性,使用户能够快速实现常见的计算机视觉任务。

但是目前官方维护也较少,所以这个项目很有可能会夭折。

importSimpleCV

camera = SimpleCV.Camera

image = camera.getImage

image.show

ImageIO

Imageio是一个用于读取和写入多种图像格式的Python库。它提供了一个简单而强大的API,使用户能够轻松地处理图像和视频数据。Imageio提供了一个通用的数据模型,使用户能够以多种方式存储图像数据。它可以使用NumPy数组、PIL图像对象或简单的Python字节字符串来表示图像数据。并且它提供了逐帧读取和写入视频文件的功能,这对于处理视频流或从视频中提取帧非常有用。

importimageio.v3 asiio

im = iio.imread( 'imageio:chelsea.png') # read a standard image

im.shape # im is a NumPy array of shape (300, 451, 3)

iio.imwrite( 'chelsea.jpg', im) # convert to jpg

albumentations

Albumentations是一个用于图像增强和数据增强的Python库。它专注于在机器学习和计算机视觉任务中提供高效、灵活和易于使用的数据增强方法。

我一直把这个库当成torchvision的替代,因为它不仅有很多数据增强方法,还能够直接处理掩码bbox的增强。

importalbumentations asA

importcv2

# Declare an augmentation pipeline

transform = A.Compose([

A.RandomCrop(width= 256, height= 256),

A.HorizontalFlip(p= 0.5),

A.RandomBrightnessContrast(p= 0.2),

])

# Read an image with OpenCV and convert it to the RGB colorspace

image = cv2.imread( "image.jpg")

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Augment an image

transformed = transform(image=image)

transformed_image = transformed[ "image"]

timm

timm是一个PyTorch模型库,虽然可能和图像处理没有关系,但是它提供了广泛的预训练模型和计算机视觉模型的集合,这对我们来进行深度学习的时候是非常有帮助的。现在它已经是huggingface的子项目了,这意味着这个项目有了资金的支持,所以不会担心发展的问题。

importtimm

importtorch

model = timm.create_model( 'resnet34')

x = torch.randn( 1, 3, 224, 224)

model(x).shape

总结

无论你是刚开始基本的图像处理还是探索高级机器学习模型,这些库都为广泛的图像处理任务提供了必要的工具。返回搜狐,查看更多