Python : 获取多张图片的特征

Python : Get features of several images

我想获得位于同一文件夹中的多个图像的功能。

我的代码如下 - 先决条件(需要库):

import numpy as np
from PIL import Image
import glob
import cv2
import os

图像所在文件夹的定义(约6000张)

images_dir = "TrainImages"

创建定义不同变量并计算它们的函数

def get_data_from_image(image_path):
    cv_img = cv2.imread(image_path)
    (means, stds) = cv2.meanStdDev(cv_img)
    stats = np.concatenate([means, stds]).flatten()
    image_features_list = [stats.tolist()]
    return image_features_list

创建扫描和分析图像的变量

image_files = [x.path for x in os.scandir(images_dir)]

循环函数的创建

i = 0
mylist =[]

for i in range (4): # I test only 4 images, could be more
    mylist.append((get_data_from_image(image_files[i])))

运行 东西

image_features_list = get_data_from_image(image_files[i])

查看输出

image_features_list

输出提供仅一张图像的特征,而不是文件夹

中的所有图像

[出局]:

[[114.31548828125001, 139.148388671875, 139.57832682291667, 50.54138521536725, 53.82290182999255, 51.946187641459595]]

如果我能有一个关于如何拥有所有图像(不仅仅是一个)的特征的解决方案,我将不胜感激。鉴于此,请毫不犹豫地更正代码。 谢谢和最诚挚的问候

在友好人士的一些评论之后,这里有一个附加信息供那些对响应感兴趣的人使用:要查看的输出是 mylist。

mylist

[出局]:

[[[144.28788548752834, 151.28145691609978, 148.6195351473923, 51.50620316379085, 53.36979275398226, 52.2493589172815]], [[56.220865079365076, 59.99653968253968, 60.28386507936508, 66.72797279655177, 65.24673515467009, 64.93141350917332]], [[125.2066064453125, 118.1168994140625, 145.0827685546875, 68.95463582009148, 52.65138276425348, 56.68269683130363]], [[114.31548828125001, 139.148388671875, 139.57832682291667, 50.54138521536725, 53.82290182999255, 51.946187641459595]]]

感谢您的帮助。这是一个很棒的论坛!

试试这个方法并告诉我它是否成功

import os, os.path
import numpy as np
from PIL import Image
import cv2


def get_data_from_image(image_path):
    cv_img = cv2.imread(image_path)
    (means, stds) = cv2.meanStdDev(cv_img)
    stats = np.concatenate([means, stds]).flatten()
    image_features_list = [stats.tolist()]
    return image_features_list

images_dir = 'C:\Users\User\Directory\TrainImages\'

images_names = []
 
with os.scandir(images_dir) as dirs:
    for entry in dirs:
        images_names.append(entry.name)


for image in images_names:

    path = images_dir + image

    image_features_list =  get_data_from_image(path))

    print(image_features_list)