python读取文件夹中的所有图片,并进行sobel处理和展示

楼主为了计算视频的SI (spatial information),需要对视频帧进行单独sobel处理,以下为从文件夹中读取图片,并逐一进行sobel处理的代码,希望可以帮助到有缘人。

import skimage.io as io
from skimage import data,filters,img_as_ubyte
from matplotlib import pyplot as plt
import matplotlib.pyplot as pyplot

from PIL import Image
path = 'xx\\*.jpeg'
imlist = io.ImageCollection(path)
n = len(imlist)#这里imlist 是矩阵格式的图像,可以直接进行操作

#将RGB转换为灰度图,要不然无法进行sobel运算(其要求传入的数据为二维的)
def rgb2gray(rgb):
    """
    convert rgb image into gray
    Args:
        rgb: grb image with numpy type
    Returns:
        gray: gray image
    """
    gray = rgb[:, :, 0] * 0.299 + rgb[:, :, 1] * 0.587 + rgb[:, :, 2] * 0.114
    return gray

for i in range(100):#读取单张图片
    # img = Image.open('xx\\%d.jpeg'%(i))
    # img = img.convert('L')
    img = imlist[i]
    img = rgb2gray(img)

    edges = filters.sobel(img)
    plt.imshow(edges)
    plt.show()

猜你喜欢

转载自blog.csdn.net/as812252319/article/details/115339218